Optimization Using MATLAB
Optimization Using MATLAB
Jayanand B
Optimizing a Non-linear function
function y = f(x)
y = x.^3 - 2*x - 5;
function b = three_var(v)
x = v(1); y = v(2); z = v(3);
b = x.^2 + 2.5*sin(y) - z^2*x^2*y^2;
v = [-0.6,-1.2,0.135];
a = fminsearch(@three_var,v)
Options for ‘fminbnd’
opts = optimset('Display','iter');
x = fminbnd(@humps,0.3,1,opts)
‘fmincom’
With linear inequalities:
Minimize
f(x) = 100(x2 – x12)2 + (1 – x1)2 , starting from the point [-
1,2], constrained to have x(1)+2x(2) ≤ 1
Express constraint as A*x ≤ B
fun = @(x)100*(x(2) – x(1)^2)^2 + (1 – x(1))^2;
x0 = [-1, 2];
A = [1,2];
B=1;
x = fmincon(fun,x0,A,B)
‘fmincon’
With linear inequality and equality constraints:
Minimize
f(x) = 100(x2 – x12)2 + (1 – x1)2 , starting from the point
[0.5,0], constrained to have x(1)+2x(2) ≤ 1 and 2x(1) + x(2) =
1
fun = @(x)100*(x(2) – x(1)^2)^2 + (1 – x(1))^2;
x0 = [-1, 2];
A = [1,2];B=1;
Aeq = [2,1]; Beq = 1;
x = fmincon(fun,x0,A,B,Aeq,Beq)
‘fmincon’
With nonlinear constraints:
Minimize
f(x) = 100(x2 – x12)2 + (1 – x1)2 , look in the region within
the circle centered at [1/3 1/3] and having a radius of 1/3
Create a function for nonlinear constraints :
function [c ceq] = nlconstraints(x);
c = (x(1) – 1/3)^2 + (x(2) – 1/3)^2 – (1/3)^2;
Ceq = [];
nonlin = @nlconstraints;
fun = @(x)100*(x(2) – x(1)^2)^2 + (1 – x(1))^2;
x0 = [0.25, 0.25];
A = []; B=[]; Aeq = []; Beq = [];
Lb = []; Ub = [];
x = fmincon(fun,x0,A,B,Aeq,Beq,Lb,Ub,nonlin)
‘fminimax’
Find values of x that minimize the maximum value of
[f1(x), f2(x), f3(x), f4(x), f5(x)]
where
f1(x)=2x12+x22−48x1−40x2+304,
f2(x)=−x12−3x22,
f3(x)=x1+3x2−18,
f4(x)=−x1−x2,
f5(x)=x1+x2−8.
First, write a file that computes the five functions at x.
function f = myfun(x)
f(1)= 2*x(1)^2+x(2)^2-48*x(1)-40*x(2)+304;
f(2)= -x(1)^2 - 3*x(2)^2;
f(3)= x(1) + 3*x(2) -18;
f(4)= -x(1)- x(2); f(5)= x(1) + x(2) - 8;
Next, invoke an optimization routine.
x0 = [0.1; 0.1]; % Make a starting guess at solution
[x,fval] = fminimax(@myfun,x0);