0

My generic problem is illustrated by the following example:

f=@(x,y) cos(x.*y);
Yvalues = linspace(0,1,50);
W = @(x) f(x,Yvalues);

which works fine if I only want to evaluate W at one point at a time. For instance:

norm(W(pi/3)-f(pi/3,Yvalues))
ans =

      0

But how do I go about evaluating W at any number of points?

Thanks in advance.

alext87
  • 371
  • 5
  • 12
  • in your second code snippet, I believe you meant `Yvalues` when you wrote `x` ? Also, the norm of a vector is by definition scalar (it's the "length" of the vector), so the fact that your second code snippet is returning an expected value. Note that `W(pi/3)-f(pi/3, Yvalues)` returns a row of 50 zeros. As long as the input to `W` is the same length as `Yvalues`, `W` returns a vector. – Dang Khoa Sep 09 '11 at 17:18

1 Answers1

1

If you change

f=@(x,y) cos(x.*y);

to

f=@(x,y) cos(x'*y);

you can execute W([1 2 3])

For example,

>> f = @(x,y) cos(x'*y);
>> yv = linspace(0,1,5);
>> W = @(x) f(x,yv);
>> W(1)
ans =
    1.0000    0.9689    0.8776    0.7317    0.5403
>> W(2)
ans =
    1.0000    0.8776    0.5403    0.0707   -0.4161
>> W(3)
ans =
    1.0000    0.7317    0.0707   -0.6282   -0.9900
>> W([1 2 3])
ans =
    1.0000    0.9689    0.8776    0.7317    0.5403
    1.0000    0.8776    0.5403    0.0707   -0.4161
    1.0000    0.7317    0.0707   -0.6282   -0.9900
stardt
  • 1,179
  • 1
  • 9
  • 14
  • `cos(x'*y)` gives you something completely different than `cos(x.*y)` though - depending on `size(x'*y)` you'll get a scalar, a vector, or a matrix; `cos(x.*y)` is an element-by-element multiplication of `x` and `y`, so this operation returns a vector of length x (which is, incidentally, the same length as y). So I guess it depends on what the OP wanted; just saying that `f(x,y)` as defined here will return variable-sized matrices based on the input sizes. – Dang Khoa Sep 11 '11 at 08:17