How To Find Extrema Using Matlab Symbolic Operations
How To Find Extrema Using Matlab Symbolic Operations
*sin(y) surf(x,y,z) %contour(x,y,z,20); axis square; colorbar; syms x y f = x+y+4*sin(x)*sin(y) fx = diff(f,x) fxx = diff(fx,x) fy = diff(f,y) fyy = diff(fy,y) fxy = diff(fx,y) d = fxx*fyy-fxy^2 % Solve for all critical points of f using solve [cpx,cpy] = solve(fx,fy) % Make critical points decimals cpx = double(cpx) cpy = double(cpy) % Make inline functions for f, fxx, and d F = inline(vectorize(f),'x','y') D = inline(vectorize(d),'x','y') Fxx = inline(vectorize(fxx),'x','y') % define %[0 0; 0 boundx = boundy = boundries 2*pi; 2*pi 0; 2*pi 2*pi] [0; 0; 2*pi; 2*pi] [0; 2*pi; 0; 2*pi]
% Make a table of the cp's, F(at cp's), D(at cp's), and Fxx(at cp's) T = [cpx cpy F(cpx,cpy) D(cpx,cpy) Fxx(cpx,cpy)] %Evaluate F at boundries of region [0,2pi]x[0,2pi] T = [boundx boundy F(boundx,boundy)]
fx = diff(f,x) fy = diff(f,y)
Find critical points (xc,yc) by solving fx=0 and fy=0 for x and y
[xc,yc] = solve(fx,fy,x,y); [xc,yc]
Matlab finds 4 solutions. The first solution is xc(1),yc(1),..., the 4th solution is xc(4),yc(4). Find the second partials and the discriminant D
fxx = diff(fx,x); fxy = diff(fx,y); fyy = diff(fy,y) D = fxx*fyy - fxy^2
Evaluate D at the first critical point by substituting for x and y the values xc(1) and yc(1)
subs(D,{x,y},{xc(1),yc(1)}) subs(fxx,{x,y},{xc(1),yc(1)})
Note that we have D > 0 and fxx > 0, hence this is a relative minimum. We can similarly check the other three critical points and find that two of them are saddle points and one is relative maximum. Plot the function as a surface and as contours (with 51 contours)
figure(1); ezsurf(f,[-1.5,1.5,-1.5,1.5]); figure(2); ezcontourc(f,[-1.5,1.5,-1.5,1.5],51); axis equal; axis tight
Can you see the relative maximum, relative minimum and the two saddle points in the contour plot?