find the critical values of a multi-variable function and tell ưhether each are a global minimum, maximum, or saddle point in matlab in matlab

To find the critical values of a multi-variable function in Matlab, we can use the "gradient" function. Then, we need to solve for the values in which the gradient is equal to the zero vector, which will give us the critical points. The Hessian matrix can be used to determine the nature of the critical points.

Here's an example code for a function f(x,y) with two variables:

main.m
syms x y
f(x,y) = x^3 - 3*x + y^3 - 3*y;
gradf = gradient(f);
Hf = hessian(f);

critical_values = solve(gradf(1)==0,gradf(2)==0);
for i=1:length(critical_values.x)
    X = [critical_values.x(i);critical_values.y(i)];
    H = double(subs(Hf,{x,y},{X(1),X(2)}));
    
    if det(H)>0 && H(1,1)>0 %minimum
        disp(['(',num2str(X(1)),',',num2str(X(2)),') is a global minimum']);
    elseif det(H)>0 && H(1,1)<0 %maximum
        disp(['(',num2str(X(1)),',',num2str(X(2)),') is a global maximum']);
    elseif det(H)<0 %saddle point
        disp(['(',num2str(X(1)),',',num2str(X(2)),') is a saddle point']);
    else 
        disp(['Cannot tell the nature at (',num2str(X(1)),',',num2str(X(2)),')']);
    end
end

713 chars
22 lines

This code will output the critical points and their nature (global minimum, maximum, or saddle point) in the given function. Note that if the determinant of the Hessian matrix is zero, we cannot determine the nature of the critical point.

gistlibby LogSnag