Optimization with MATLAB (3)
Optimization with MATLAB (3)
▪ MATLAB function linprog solves linear programming problems in the minimization form.
Minimize 𝑧 = 𝑓𝑇𝑥
s.t. 𝐴. 𝑥 ≤ 𝑏
𝐴𝑒𝑞. 𝑥 = 𝑏𝑒𝑞
𝑙𝑏 ≤ 𝑥 ≤ 𝑢𝑏
[x,fval,exitflag,output,lambda]=linprog(f,A,b,Aeq,beq,lb,ub,x0,options)
f: objective function lb, []: lower bounds on x, x: optimal solution output: number of iterations
coefficients no lower bounds algorithm used
A: matrix of inequality ub, []: upper bounds on x, fval: optimal objective
constraints no upper bounds function value
b: RHS of inequality x0: start vector for the exitflag: tells whether the lambda: contains Lagrange
constraints algorithm if known, else [] algorithm converged or not, multipliers corresponding
>0 means convergence to constraints
Aeq: matrix of equality options: options set to
constraints determine what algorithms
to use
beq: RHS of equality
constraints 2
▪ lambda Structure containing the Lagrange multipliers at the solution x (separated by constraint type). The
fields of the structure are:
lower Lower bounds lb
upper Upper bounds ub
ineqlin Linear inequalities
eqlin Linear equalities
▪ output Structure containing information about the optimization. The fields of the structure are:
algorithm Algorithm used
cgiterations The number of conjugate gradient iterations (large-scale algorithm only).
iterations Number of iterations
message Exit message
▪ exitflag Integer identifying the reason the algorithm terminated. The following lists the values of exitflag
and the corresponding reasons the algorithm terminated.
1 Function converged to a solution x.
0 Number of iterations exceeded options.MaxIter.
-2 No feasible point was found.
-3 Problem is unbounded.
-4 NaN value was encountered during execution of the algorithm.
-5 Both primal and dual problems are infeasible.
-7 Search direction became too small. No further progress could be made.
3
% Giapettos's Woodcarving Problem
% Define the problem coefficients:
%Since the original problem is maximization, the objective function coefficients are negated
for minimization:
f = [-3; -2];
%Inequality constraint coefficients are defined for <= constraints:
A = [1 1
2 1
1 0];
%RHS values for inequality constraints:
b = [80
100
40];
% Variables are non-negative:
lb = zeros(2,1);
[x,fval,exitflag,output,lambda] = linprog(f,A,b,[],[],lb);
4
Optimization terminated. >> output
>> x
output =
x=
5
▪ fzero
x = fzero(fun,x0) tries to find a zero of fun near x0
x0 is a scalar, fun is a function handle.
▪ fminsearch
x = fminsearch(fun,x0) starts at the point x0 and finds a local
minimum x of the function described in fun.
x0 can be a scalar, vector, or matrix;fun is a function handle.