0% found this document useful (0 votes)
42 views

How To Find Extrema Using Matlab Symbolic Operations

This document discusses how to find extrema of a multi-variable function using Matlab symbolic operations. It shows how to define the function symbolically, take derivatives to find critical points, evaluate the discriminant at critical points to determine if it is a max/min, and plot the function as a surface and contours to visualize the extrema.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

How To Find Extrema Using Matlab Symbolic Operations

This document discusses how to find extrema of a multi-variable function using Matlab symbolic operations. It shows how to define the function symbolically, take derivatives to find critical points, evaluate the discriminant at critical points to determine if it is a max/min, and plot the function as a surface and contours to visualize the extrema.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

%plotting it [x,y] = meshgrid(0 : .1 : 2*pi) z = x+y+4.*sin(x).

*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)]

How to find extrema using Matlab symbolic operations


You need to download the file ezcontourc.m. Example: Find all relative maxima and minima of f(x,y) = x3 - 3x + 3xy2 Define the symbolic variables and f
syms x y f = x^3 - 3*x + 3*x*y^2

Find the partial derivatives

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?

You might also like