MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Melissa
on 24 May 2011

Hey All, I am currently trying to make a MATLAB program that will find the critical values of a multi-variable function and tell me whether each are a minimum, maximum, or saddle point. I wrote in a function which I know has two critical points but how do I create a loop to where it will calculate all critical points? And how do I actually get the print to show up in the if statements? Here is my current Matlab code:

function [c,d] = critcalpoints(f)

%CRITCALPOINTS(f) is a function to determine the critical points of a 2D

%surface given the function f(x,y).

%The method choosen is to compute the first and second partial derivatives

%on the given function by first evaluating the Jacobian and Hessian Matrix

%and then solve by finding the eigenvalues of obtained critical points.

%Declaration of Variables

syms x y

f=x^3-3*x^2+5*x*y-7*y^2;

% First Order Partial Derivative using the Jacobian Matrix

gradf = jacobian(f,[x,y]);

% Second Order Patrial Derivative using the Hessian Matrix

hessmatf = jacobian(gradf,[x,y]);

%Solving the First Order Partial Derivative for critical points

[xcr,ycr]=solve(gradf(1),gradf(2));

%Evaluating the critical points in the Hessian Matrix

H1=subs(hessmatf,[x,y],[xcr(1),ycr(1)]…

H2=subs(hessmatf,[x,y],[xcr(2),ycr(2)]…

%Computing the eigenvalue of the evaluation of critical points

eig(H1);

eig(H2);

%Converting to numerical values

c = double(eig(H1));

d = double(eig(H2));

%Classifying and Pritning the Critical Points

if (c(1) > 0 & d(1) > 0) | (c(2) > 0 & d(2) > 0)

print( [xcr,ycr], ' is a minimum')

elseif (c(1) < 0 & d(1) < 0) | (c(2) < 0 & d(2) < 0)

print( [xcr, ycr], ' is a maximum')

elseif (c(1) < 0 & d(1) > 0) | (c(1) > 0 & d(1) < 0)

print( [xcr, ycr], ' is a saddle point')

elseif (c(2) < 0 & d(2) > 0) | (c(2) > 0 & d(2) < 0)

print( [xcr, ycr], ' is a saddle point')

elseif (c(1)==0 | d(1)==0)

print( [xcr, ycr], ' is degenerate')

elseif (c(2)==0 | d(2)==0)

print( [xcr, ycr], ' is degenerate')

end

Additional Details In order to obtain critical points it depends on the gradiant, or in this case gradf. So if I wanted to loop it then I would set n=length of gradf? then set a loop 1:n? uh...I dont know if thats correct.

*No products are associated with this question.*

## 1 Comment

## Melissa (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/8126#comment_17363

Ah apparently you cant use sysm in a function, or I did it wrong. Shoot. Help.