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

To find the critical values and classify them as minimum, maximum or saddle points for a multi-variable function in MATLAB, you can follow these steps:

  1. Define the function f(x,y) that you want to optimize.
main.m
syms x y
f(x,y) = x^2 - y^2 + 2*x*y;
37 chars
3 lines
  1. Use the gradient function to find the gradient of the function.
main.m
grad_f = gradient(f, [x y]);
29 chars
2 lines
  1. Find the solutions for the gradient equations: grad f = [0 0].
main.m
solutions = solve(grad_f == [0 0], [x y]);
43 chars
2 lines
  1. Evaluate the Hessian matrix at each critical point.
main.m
hess_f = hessian(f, [x y]);
28 chars
2 lines
  1. Determine the nature of each critical point by analyzing the Hessian matrix.

For each critical point, perform the following steps: 5.1. Evaluate the eigenvalues of the Hessian matrix.

main.m
hess_evals = eig(eval(hess_f));
32 chars
2 lines

5.2. Determine the nature of the critical point based on the eigenvalues: * If all eigenvalues are positive, the point is a local minimum. * If all eigenvalues are negative, the point is a local maximum. * If there are both positive and negative eigenvalues, the point is a saddle point. * If any eigenvalue is zero, further analysis is needed.

Here's the complete code:

main.m
syms x y
f(x,y) = x^2 - y^2 + 2*x*y;
grad_f = gradient(f, [x y]);
solutions = solve(grad_f == [0 0], [x y]);
hess_f = hessian(f, [x y]);

for i=1:length(solutions.x)
    s = [solutions.x(i) solutions.y(i)];
    hess_evals = eig(eval(subs(hess_f, [x y], s)));
    
    if all(hess_evals > 0)
        disp(['Point (' num2str(s(1)) ', ' num2str(s(2)) ') is a local minimum.']);
    elseif all(hess_evals < 0)
        disp(['Point (' num2str(s(1)) ', ' num2str(s(2)) ') is a local maximum.']);
    elseif any(hess_evals == 0)
        disp(['Further analysis is needed at point (' num2str(s(1)) ', ' num2str(s(2)) ')']);
    else
        disp(['Point (' num2str(s(1)) ', ' num2str(s(2)) ') is a saddle point.']);
    end
end
720 chars
21 lines

Note that the function eig in MATLAB returns the eigenvalues of a matrix and eval(subs(hess_f,[x,y],s)) evaluates the Hessian matrix at a given point (x,y) = s.

gistlibby LogSnag