How to Solve Optimization Problems Using Matlab

Hello everyone and welcome.

In this video, I’m going to show you how to solve optimization problems using Matlab.

This method is very easy to use and a minimum programming skill is required. This method is based on “fmincon” solver – a powerful solver for solving nonlinear constrained continuous optimization problems.

Here is the detail of the test function, including the objective function, constraints and global optimal solution.

Let’s see how we can solve this problem.

For more videos like this, check my YouTube channel here.

Matlab code

objective_function.m

function Output = objective_function(Input)
x = Input(1);
y = Input(2);
Output = (1 - x)^2 + 100*(y - x^2)^2;

nonlinear_constraint.m

function [C Ceq] = nonlinear_constraint(Input)
x = Input(1);
y = Input(2);
C = x^2 + y^2 - 2;  % inequality constraint 
Ceq = [];  % No equality constraint

fminconsolver.m

function [x,fval,exitflag,output,lambda,grad,hessian] = fminconsolver(x0,lb,ub)
options = optimoptions('fmincon');
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'PlotFcn', {  @optimplotfval @optimplotconstrviolation });
[x,fval,exitflag,output,lambda,grad,hessian] = ...
fmincon(@objective_function,x0,[],[],[],[],lb,ub,@nonlinear_constraint,options);

main_code.m

clc
clear all
close all
%--------------------------------------------------------------------------
% customization: lower bound lb = [1xn], and upper bound lb = [1xn]
lb = [-1.5 -1.5];
ub = [1.5 1.5];
%--------------------------------------------------------------------------
n=5; % number of starts
[x y]=size(lb);
S=zeros(n,y);
R=zeros(n,1);
% multi-start optimization solver
for j=1:n
    for i = 1:y
        x0(i)=lb(i) + (ub(i)-lb(i))*rand; % generate initial solution
    end
    [x,fval,exitflag,output,lambda,grad,hessian] = fminconsolver(x0,lb,ub);
    S(j,:)=x;
    R(j,1)=fval;
end
[x1 y1]=find(R == min(R));
objective_function_value = R(min(x1),1)
optimal_solution = S(min(x1),:)

P/s: If you find the post useful, share it to remember and to help other people as well.

8 Replies to “How to Solve Optimization Problems Using Matlab”

  1. Thank you so much. Very useful video I am new to optimization.
    How can I solve the below objective function:
    {\begin{align}\label{Problem_formulation}
    \mathbb P_1& ~~~~~~\mathop{\max}_{{ \eta, x_0 }} ~~~~{B}\log _2 \left(1+ \frac{{{P_M}\Vert {\textbf g}_d\Vert ^2}{( {\frac{c}{{4\pi {f}}}} )^2 {x_3}^{ – \alpha_N}{e^{ – K({f}){x_3}}}}} {{{\sigma_w^2}}}\right)+\sum\nolimits_{f = 1}^{[S/\eta ]} ~~
    \frac{{{f^{ – \delta }}}}{{\sum\nolimits_{f = 1}^F {{f^{ – \delta }}} }}~~{B}\log _2 \left(1+ \frac{{{P_S}\Vert {\textbf h}_1\Vert ^2}{( {\frac{c}{{4\pi {f}}}} )^2 {x_0}^{ -\alpha_L}{e^{ – K({f}){x_0}}}}} {{{\sigma_w^2}}}\right)\nonumber \\
    &\mathop{\rm{s.t.}} \;\;~\eta \in [0,1], ~~ \forall f \in F \nonumber\\
    &\;\;\qquad \sum\nolimits_{f = 1}^{[S/\eta ]} {b_f} \leq S\nonumber
    \end{align}

    Hadeel

  2. Sir i have an objective function to minimize the canal cost by using optimization, Z=A*B+C*D+E*F+G*H where df=0.43*Q^0.2;
    K=(b+2*d*SQRT(1+m1^2))^(4/3);
    J=Q^2*0.013^2;
    L=(d*(b+m1*d))^(10/3);
    S0=(K*J)/L;
    bb=1.1*d^0.7;
    A=1/(12*(S0+Sg));
    B=1.85*m3*(((10-d+Length*(Sg+S0))^4)-(10-d)^4);
    C=4*(1.85*((b/2)+m1*d+bb)+1.45*m3);
    D==(10-d+Length*(S0+Sg))^3-(10-d)^2
    E==6*(1.85*d*(b+m1*d)+1.45*(b+2*bb+2*d*m1))
    F==(10-d+Length*(S0+Sg))^2-(10-d)^2;
    G==2*1.85*d*d*(3*b+2*m1*d)+12*1.45*d*(b+m1*d)
    H=L*(Sg+S0).
    sir how can i optimize this problem in MATLAB. I’m new to optimization. Kindly help.

  3. Thankyou for the video.
    I wanted to know can this optimization be used to solve an equation that have more than two unknown variables?
    Suppose the equation is :
    A0* exp(-(((x-x0)*cos(phi) + (y-y0)*sin(phi))^2/0.5*ax^2) – (((y-y0)*cos(phi) – (x-x0)*sin(phi))^2/0.5*ay^2))
    For this equation, there are six unknowns (A0, x0, y0, phi, ax, and ay) and x,y,z dataset.
    So can we solve this or something similar to this issue using optimization in MATLAB?
    Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *