Given f(x,y), how do I solve gradient f=0?
31 views (last 30 days)
Show older comments
I have a function f(x,y) and want to find the values of x and y that make grad(f)=0. Is is possible to do the gradient symbolically inside an m file? I'm trying to use 'gradient' and 'fsolve' commands, but getting hung up on syntax.
The function is: f=-(x.^2+(y-1).^2+x).*exp( -(x.^2+y.^2)/5 )
0 Comments
Answers (2)
Star Strider
on 6 Feb 2016
Two options — choose the one that works for you:
% % ————— Numeric:
x = linspace(-10, 10);
[X,Y] = meshgrid(x);
f = @(x,y) -(x.^2+(y-1).^2+x).*exp( -(x.^2+y.^2)/5 );
fxy = f(X,Y);
dxy = x(2)-x(1);
gfxy = gradient(fxy, dxy, dxy);
figure(1)
C = contour(X, Y, gfxy, [0; 0]);
grid on
% % ————— Symbolic:
syms x y
f = symfun(-(x.^2+(y-1).^2+x).*exp( -(x.^2+y.^2)/5 ), [x, y]);
gradfxy = gradient(f, [x, y]);
grad_zero = solve(gradfxy == 0);
x_soln = grad_zero.x
y_soln = grad_zero.y
0 Comments
achraf Elqarnani
on 20 May 2021
x = linspace(-10, 10);
[X,Y] = meshgrid(x);
f = @(x,y) -(x.^2+(y-1).^2+x).*exp( -(x.^2+y.^2)/5 );
fxy = f(X,Y);
dxy = x(2)-x(1);
gfxy = gradient(fxy, dxy, dxy);
figure(1)
C = contour(X, Y, gfxy, [0; 0]);
grid on
% % ————— Symbolic:
syms x y
f = symfun(-(x.^2+(y-1).^2+x).*exp( -(x.^2+y.^2)/5 ), [x, y]);
gradfxy = gradient(f, [x, y]);
grad_zero = solve(gradfxy == 0);
x_soln = grad_zero.x
y_soln = grad_zero.y
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!