Matlab optimization, example of function optimization

This page is an example of function minimization with the Matlab function fminsearch. The example is composed of three files:

Function minimization with MATLAB fminsearch

Main code

%% Optimization example
% This file is the main script
% Written by Philippe Lucidarme
% https://www.lucidar.me/en/matlab/matlab-optimization-function-minimization-example/
close all;
clear all;
clc;

%% Display the function fct

figure(1);
X=[-4:0.1:4]; %% The X-axis goes from the value of -5 to +5 with a step of 0.1 (100 points)
Y=[-4:0.1:4]; %% like the X-axis
[wx,wy]=meshgrid(X,Y); %% see [MATLAB documentation][1]
% Compute each point of the mesh
for i=1:size(X,2)
    for j=1:size(Y,2)
        %Z=100.*(wy - wx.^2).^2 + (1 - wx).^2;
        Z(j,i)=fct([X(i),Y(j)]);
    end
end
% Display curb
fig=surf(wx,wy,Z); %% make a surface plot with lighting
shading interp; %% optional, try to remove it.
%     hsv        - Hue-saturation-value color map.
%     hot        - Black-red-yellow-white color map.
%     gray       - Linear gray-scale color map.
%     bone       - Gray-scale with tinge of blue color map.
%     copper     - Linear copper-tone color map.
%     pink       - Pastel shades of pink color map.
%     white      - All white color map.
%     flag       - Alternating red, white, blue, and black color map.
%     lines      - Color map with the line colors.
%     colorcube  - Enhanced color-cube color map.
%     vga        - Windows colormap for 16 colors.
%     jet        - Variant of HSV.
%     prism      - Prism color map.
%     cool       - Shades of cyan and magenta color map.
%     autumn     - Shades of red and yellow color map.
%     spring     - Shades of magenta and yellow color map.
%     winter     - Shades of blue and green color map.
%     summer     - Shades of green and yellow color map.      
colormap cool; %% here you decide the colormap: hot is the one going from white to red
view(45,65);
rotate3d;
hold on;
xlabel ('x');
ylabel ('y');
title ('Optimization');

%% Optimization

% Initial point for optimization
x0 = [0,0];

% Set parameters
% optimplotfval, display the function evaluation
% iter, display algorithm status a each interation
% Call outfun at each iteration (for display)
options = optimset('PlotFcns',@optimplotfval,'Display','iter','OutputFcn', @outfun);
% Optimization algorithm
[x,FVAL,EXITFLAG,OUTPUT] = fminsearch(@fct,x0,options);

%% Display results

FVAL
OUTPUT
x
figure(1);
plot3 (x(1),x(2),fct(x),'or','MarkerSize',10,'LineWidth',5);

Function to optimize

You can uncomment alternative functions or program your own function.

function y = fct( x )
% fct  function to minimize
%   x = [x,y] input vector
%   y = function output (scalar)
% Written by Philippe Lucidarme
% http://www.lucidarme.me

% Rosenbrock's function
%y = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;

%y = (x(1)-4).^2 .* (x(2)-1.2).^2;

y = sinc(x(1)).*sinc(0.3*x(2));

end

Function called at each step

This function is called at each step, here it displays search points on the figure.

function stop = outfun(x, optimValues, state)
% fct  this function is called at each step of the optimization process
%   x :             the point computed by the algorithm at the current iteration.
%   optimValues :   is a structure containing data from the current iteration.
%   state :         current state of the algorithm.
% Written by Philippe Lucidarme
% http://www.lucidarme.me

figure (1);
plot3 (x(1),x(2),optimValues.fval,'.k','MarkerSize',10);
stop=false;

See also


Last update : 05/24/2021