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.

1
  • 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. Commented Sep 9, 2011 at 17:18

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.