Search code examples
matlab

Having trouble with feval and function


While I have my function grad as

function g = grad(x)
    g = [4*(x(1)-4)^3 ; 2*x(2)-6 ; 16*(x(3)+5)^3];

end

Why do i have a error message "Not enough input arguments. Error in grad (line 2) g = [4*(x(1)-4)^3 ; 2x(2)-6 ; 16(x(3)+5)^3];" when I try to run the code

x = [4;2;-1];
feval(grad,x)

Any assistance would be appreciated.


Solution

  • When passed as an argument of the feval, the function name should be passed as a string or as a function handle. So:

    x = [4;2;-1];
    feval("grad", x)
    

    (Notice the quotation marks around the "grad").

    Or:

    x = [4;2;-1];
    feval(@grad, x)