I want to create a function that outputs the gradient of a two variable function evaluated at a point (x,y) but it gives me this error message: Error using feval Function to evaluate must be represented as a string scalar, character vector, or function_handle object.
I need help :(
function [gradxy] = gradf(cord)
xcord=cord(1,1);
ycord=cord(1,2);
syms x y
fxy=@(x,y) x*(10*y+3)*exp(-x^2-y^2);
fx=diff(fxy,x);
fy=diff(fxy,y);
A(1)=feval(fx,xcord,ycord);
A(2)=feval(fy,xcord,ycord);
disp('gradient of f(x,y)');
disp(gradxy);
end

2 Kommentare

Rik
Rik am 22 Mär. 2022
In this expression
fxy=@(x,y) x*(10*y+3)*exp(-x^2-y^2);
The symbolic variables you created are ignored.
LAURA RODRIGUEZ VALVERDE
LAURA RODRIGUEZ VALVERDE am 22 Mär. 2022
what should I change?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Mär. 2022

0 Stimmen

fxy = x*(10*y+3)*exp(-x^2-y^2);
fx=diff(fxy,x);
fy=diff(fxy,y);
A(1) = subs(fx, {x, y}, {xcord,ycord});
A(2) = subs(fy, {x,y}, {xcord,ycord});
But after that you have to do something with vector A in order to create a value for gradxy in order to be able to return gradxy from the function.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by