how to find the extremum of multivariable function in matlab

Matlab has several built-in functions to find the extremum of a multivariable function. One of the popular ones is the fmincon function which finds the minimum of a multivariable function subject to constraints.

Here's an example code that finds the minimum of a function f with initial guess x0 and lower and upper bounds for x:

main.m
% Define the function
f = @(x) (x(1)-2)^2 + (x(2)-3)^2;

% Define the initial guess
x0 = [1; 1];

% Define the lower and upper bounds for x
lb = [-2; -2];
ub = [4; 4];

% Set up the optimization problem
options = optimoptions('fmincon','Display','iter');
[x,fval] = fmincon(f,x0,[],[],[],[],lb,ub,[],options);

% Print the result
disp(['Minimum value: ', num2str(fval)])
disp(['Optimal x: ', num2str(x')])
406 chars
18 lines

In this example, the function f is (x(1)-2)^2 + (x(2)-3)^2, and we are finding the minimum value of f subject to lower and upper bounds on the variables x(1) and x(2). The initial guess is [1;1], and the fmincon function is called with the options Display set to iter to display the optimization process.

The function returns the optimal value of x and the minimum value of the function f.

related categories

gistlibby LogSnag