Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Critical Points of Multivariable function

Asked by Melissa

Melissa (view profile)

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.

1 Comment

Melissa

Melissa (view profile)

on 24 May 2011

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

Melissa

Melissa (view profile)

Products

No products are associated with this question.

1 Answer

Answer by bym

bym (view profile)

on 24 May 2011

do you mean

syms x y   %<-- note you wrote sysm

2 Comments

bym

bym (view profile)

on 24 May 2011

also, I don't know if it will help but take a look at the del2 function

Melissa

Melissa (view profile)

on 25 May 2011

ah yes I fixed that but still I get errors. thank you though for that catch!

bym

bym (view profile)

Contact us