How can calculate f?

52 Views Asked by At

I have a loop, in each iteration I want to compute $f(X)$ such that $X$ is a vector.

When I wrote f=@(x,y) (x+2)^2+(y-x)^3 and X=[1,5], I have an error in f(X).

How can write function $f(X)$ such that $X$ can be a vector?

And suppose we can't use f(X(1),X(2)).

Thanks

1

There are 1 best solutions below

2
On BEST ANSWER

Instead of

>> f=@(x,y) (x+2)^2+(y-x)^3;
X=[1,5];
f(X)
Error using  ^ 
One argument must be a square matrix and the other must be
a scalar. Use POWER (.^) for elementwise power.
Error in @(x,y)(x+2)^2+(y-x)^3 

you might try using

>> f=@(X) (X(1)+2)^2+(X(2)-X(1))^3;
X=[1,5];
f(X)
ans =
    73