Pilani Campus NWLMB 14.pdfBITSPilani Pilani Campus National Workshop on LaTeXand MATLAB for...
Transcript of Pilani Campus NWLMB 14.pdfBITSPilani Pilani Campus National Workshop on LaTeXand MATLAB for...
BITS PilaniPilani Campus
National Workshop on LaTeX and MATLAB for Beginners
24 - 28 December, 2014BITS Pilani,
Partially Supported by DST, Rajasthan
BITS Pilani, Pilani Campus
3
• Solving Linear Programming Problems• Solving Quadratic Programming Problems• Determine the minimum of a function of unconstrained
problem• Determine the minimum of a function of constrained
problem• Solve a system of nonlinear equations• Determine the maximum of minimum of a function• Genetic Algorithm
– By the command line– By Optimization App: optimtool
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Solving Linear Programming Problems
4
1 2( , , ... )
T
n
f X
AX bC X b
l
M insubject to
w her X x x xb
eb X u
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
5
[x,fval] = linprog(f,A,b,C,d,lb,ub)
Input arguments:• f: coefficient vector of the objective function• A,[ ]: Matrix of inequality constraints, no
inequality constraints• b,[ ]: right hand side of the inequality constraints,
no inequality constraints
1 2
. .
( , , ... )
T
n
f X
AX bCX bl
X x x xb X ub
Mins t
Solving Linear Programming Problems
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
[x,fval] = linprog(f,A,b,C,d,lb,ub)
• C,[ ]: Matrix of equality constraints, no equality constraints
• d,[ ]: right hand side of the equality constraints, no equality constraints
• lb,[ ]: lb ≤ x : lower bounds for x, no lower bounds
• ub,[ ]: x ≤ ub : upper bounds for x, no upper bounds
6
Solving Linear Programming Problems
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
[x,fval] = linprog(f,A,b,C,d,lb,ub)
Output arguments:•x: optimal solution, i.e. optimal values of the decision variables
•fval: optimal value of the objective function
7
Solving Linear Programming Problems
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Example: Consider the LPP
8
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
2 3 5
2 2 4
Maximizesu
402 2 84 2 10
, , , 0
bject toz x x x x
x x x xx x x xx x x x
x x x x
Solving Linear Programming Problems
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
MATLAB Code
>> clear;>> f = [-2;-1;3;-5];>> A = [1,2,2,4;2,-1,1,2;4,-2,1,-1];>> b = [40;8;10];>> lb = zeros(4,1);>> [x,fval]=linprog(f,A,b,[],[],lb)
9
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
2 3 5
2 2 4 402 2 84 2
, , 0
. .
10,
M z x x x x
x x x xx x x xx x x
axs t
xx x x x
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Screen shot of the .m file
10
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
2 3 5
2 2 4 402 2 84 2
, , 0
. .
10,
M z x x x x
x x x xx x x xx x x
axs t
xx x x x
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Screen shot of the results
11
i.e.,x1 = 0, x2 = 6, x3 = 0, x4 = 7 with max. z = 41.
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Screen shot of the .m file
12
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Maxs.t
2 3 41;
2 2;3 2 4;
, ,
.
0
Z x x xx x x
x x xx x xx x x
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Screen shot of the results
13December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Solving LPPs
14
[x,fval]= linprog(f,A,b,C,d,lb,ub,x0,option)
Input arguments:• x0 : Start vector for the algorithm, if known, else [ ].• options: options are set using the optimset function,
they determine what algorithm to use, etc.
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
• To display the iterations you can try:
[x,fval] =linprog(f,A,a,B,b,lb,ub,[],optimset(’Display’,’iter’))
15
Solving LPPs
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Quadratic programming is the problem of findinga vector X that minimizes a quadratic function,subject to linear constraints:
161 2
1
...
2
( , , )
T
n
Tz X Q X q X
A X bC X
M in
s u b jec t to
w h e r X x x
dlb X u
xb
e
Solving Quadratic Programming Problems
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Input arguments:• Q: Hessian of the objective
function• q: Coefficient vector of the linear
part of the objective function• A,[ ]: Matrix of inequality
constraints, no inequality constraints 17
Solving QPP
[xsol,fval] = quadprog(Q,q,A,b,C,d,lb,ub)
1 2
. .
( , ,..
12
. )
T T
n
z X QX q X
AX bCX dlb
X x
M
X u
in
s t
w xhere xb
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
[xsol,fval] = quadprog(Q,q,A,b,C,d,lb,ub)• b,[ ]: right hand side of the inequality constraints,
no inequality constraints• C,[ ]: Matrix of equality constraints, no equality
constraints• d,[ ]: right hand side of the equality constraints, no
equality constraints• lb,[ ]: lb ≤ x : lower bounds for x, no lower bounds• ub,[ ]: x ≤ ub : upper bounds for x, no upper
bounds 18
Solving QPP
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
[xsol,fval] = quadprog(Q,q,A,b,C,d,lb,ub)
Output arguments:• xsol: optimal solution• fval: optimal value of the objective function
19
Solving QPP
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Example:
20
2 2 21 2 1 2 3 2 3 1 2 3
1 2 3
1 2 3
1 2 3
2 2 2 4 6 12
62 2
, , 0
Minsubject
z x x x x x x x x x x
x x xx x xx x x
to
2 1 01 4 2 ; [4 6 12];0 2 4
Q q
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Screen shot of the .m file
21
2 2 21 2 1 2 3 2 3
1 2 3
1 2 3
1 2 3
1 2 3
2 2 24 6 12
62 2
, 0
.
,
.
z x x x x x x xx x x
x x xx
Min
s t
x xx x x
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Screen shot of the results
22
i.e.,x1 = 3.3333, x2 = 0, x3 = 2.6667, with min. z = 70.6667.
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Determine the minimum of a function of unconstrained problem
23
( )n
Min f xx R
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
[xsol,fopt] = fminunc(@fun,x0)
Input arguments:• fun: a MATLAB function m-file that contains
the function to be minimized• x0: Start vector for the algorithmOutput arguments:• xsol: optimal solution• fopt: optimal value of the objective function;
i.e., f(xsol) 24
Determine the minimum of a function of unconstrained problem
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Example:
25
2 2 21 2 3Mini i 5m e 3z z x x x
fun.mfunction f = fun(x) f = x(1)^2+3*x(2)^2+5*x(3)^2;
>> x0=[1,1,1];>>[xsol,fopt] = fminunc(@fun,x0)
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
26
i.e.,x1 = -0.2557, x2 = 0.5323, x3 = -0.0355, with min z = 9.2158e-13.
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
Determine the minimum of a function of unconstrained problem
BITS Pilani, Pilani Campus
Determine the minimum of a function of constrained problem
27
(nonlinear constraint with inequality)(nonlinear constraint with equality)(linear constraint with inequality)(line
(
ar constraint w
)
( ) 0( ) 0
. .
ith equality)
f X
c Xceq XAX bCX dlb X u
ins t
b
M
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Input arguments:• fun: a MATLAB function m-file that
contains the function to be minimzed• x0: Start vector for the algorithm• nonlcon: a MATLAB function m-file
that contains the nonlinear inequalities and/or equalities constraints
28
( )
( ) 0(
.
0
.
)
M f Xinsc Xceq XAX bCX
X b
t
dlb u
[xsol,fopt] = fmincon(@fun,x0,A,b,C,d,lb,ub,nonlcon,options)
Determine the minimum of a function of constrained problem
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Output arguments:• xsol: optimal solution• fopt: optimal value of the objective
function; i.e., f(xsol)29
( )
( ) 0(
.
0
.
)
M f Xinsc Xceq XAX bCX
X b
t
dlb u
Determine the minimum of a function of constrained problem[xsol,fopt] = fmincon(@fun,x0,A,b,C,d,lb,ub,nonlcon,options)
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Problem
• Consider the problem of minimizing functionf (x) = 100(x2 − x1
2)2 + (1 − x1)2 ,over the unit disk, i.e., the disk of radius 1centered at the origin.
In other words, find x that minimizes the functionf(x) over the set x1
2+ x22 ≤ 1
• This problem is a minimization of a nonlinear function with a nonlinear constraint.
30December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Solution
• Create a .m file named fun asfunction f = fun(x) f = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
• Create a .m file named unitdisk asfunction [c, ceq] = unitdisk(x) c = x(1)^2 + x(2)^2 - 1;ceq = [ ];
31December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
• [xsol,fopt] = fmincon(@fun,x0,A,b,C,d,lb,ub,nonlcon,options)
• [x,fval] = fmincon(@fun,[0 0],[],[],[],[],[],[], @unitdisk)
32
Determine the minimum of a function of constrained problem
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
33
Determine the minimum of a function of constrained problem
i.e.,x1 = 0.7864, x2 = 0.6177with min. f(x) = 0.0457
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
34
Solve a system of nonlinear equations
1 2
( ) 0( , ,..., )
( ):function that returns the vector valueofn
F XX x x x
F X X
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
x = fsolve(@myfun,x0)Input arguments:• myfun: a MATLAB function m-file that
accepts a vector X and returns a vector F, thenonlinear equations evaluated at X.
• x0: Start vector for the algorithm
35
Solve a system of nonlinear equations
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Example:
36
1
2
1 2
1 2
2 0( )
2 0
x
x
x x eF X
x x e
myfun.mfunction F = myfun(x) F = [2*x(1) - x(2) - exp(-x(1));
-x(1) + 2*x(2) - exp(-x(2))];
>> x0=[-5;-5];>> [x,fval] = fsolve(@myfun,x0)
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
37
Solve a system of nonlinear equations
i.e.,x1 = x2 = 0.5671with F1 = F2 = -0.4059
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Determine the minimum of maximum of functions
38
{ }
(nonlinear constraint with inequality)(nonlinear constraint with equality)(linear constraint with inequality)(linear constraint with equalit
{ ( )}
( ) 0.
) 0
.
y)
(
iX F iF X
c Xceq XAX bC
Min
X
Max
s t
dlb X ub
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Input arguments:• fun: a MATLAB function m-file that
contains the functions • x0: Start vector for the algorithm• nonlcon: a MATLAB function m-file
that contains the nonlinear inequalities and/or equalities constraints
39
{ }{ ( )}
( ).
0( ) 0
.i
iX FF X
c Xceq XAX bC
M in M ax
s t
X dlb X ub
[x,fval] = fminimax(@fun,x0,A,b,C,d,lb,ub,nonlcon,options)
Determine the minimum of maximum of functions
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Output arguments:• x: optimal solution• fval: optimal values of the functions
40
[x,fval] = fminimax(@fun,x0,A,b,C,d,lb,ub,nonlcon,options)
Determine the minimum of maximum of functions
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Problem
• Find values of x that minimize the maximum value of [ f1(x) , f2(x) , f3(x) , f4(x) , f5(x)]
41
2 21 1 2 1 2
2 22 1 2
3 1 2
4 1 2
5 1 2
( ) 2 48 40 304
( ) 3( ) 3 18( )( ) 8
f x x x x xf x x xf x x xf x x xf x x x
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Solution
• Create a .m file named funminmax asfunction f = funminmax(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;
42December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Example:
43
>> x0=[0.1;0.1];>> [x,fval] = fminimax(@funminmax,x0)
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
[x fval] = ga(@fitnessfun, nvars, options)
Input arguments:• fitnessfun: a MATLAB function m-file that
handle to the fitness function and contains thefunction to be minimzed.
• nvars: the number of independent variables for the fitness function.
• options: a structure containing options for the genetic algorithm. If you do not pass in this argument, ga uses its default options. 44
Genetic Algorithm
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
[x fval] = ga(@fitnessfun, nvars, options)
Output arguments:• x: Point at which the final value is
attained• fval: Final value of the fitness function
45
Genetic Algorithm
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Example:
46
2 2 21 2 1Minimize 100( ) (1 )z x x x
fitness.mfunction z = fitness(x)z = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
>> fitnessfunction = @fitness; >> nov = 2; >>[x,fval] = ga(fitnessfunction,nov)
or>>[x,fval] = ga(@fitness,2)
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Screen shot of the results
47December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
i.e.,x1 = 0.9652, x2 = 0.9340, with min. z = 0.0017.
BITS Pilani, Pilani Campus
Example:
48
2 2 21 2 1
1 2 1 2
1 2
1
2
100( ) (1 )
1.5 01
. .
0 00 10 13
z x x x
x x
Minimiz
x xx x
xx
es t
Genetic Algorithm
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Solution• Create a .m file named fitness asfunction f = fitness(x) f = 100*(x(1)^2 - x(2))^2 + (1 - x(1))^2;
• Create a .m file named constraint asfunction [c, ceq] = constraint(x) c = [1.5 + x(1)*x(2) + x(1) - x(2);
-x(1)*x(2) + 10];ceq = [];
49
2 21 2
21
1 2 1 2
1 2
1
2
. .
100( )(1 )
1.5 010 00 10 13
Min
s
z x xx
x x x xx x
xx
t
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
>> ObjectiveFunction = @fitness; >> nvars = 2; % Number of variables >> LB = [0 0]; % Lower bound >> UB = [1 13]; % Upper bound >> ConstraintFunction = @constraint; >> [x,fval] =
ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction)
50
Genetic Algorithm
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Screen shot of the results
51December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
i.e.,x1 = 0.8122, x2 = 12.3122, with min. z = 13578.
BITS Pilani, Pilani Campus
Solving Linear Programming Problems
52
• By Optimization App: optimtool
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Solving Linear Programming Problems
53
• By Optimization App: optimtool
December 28, 2014 Dr. Shivi Agarwal, NWLMB-2014
BITS Pilani, Pilani Campus
Other Commands
• fgoalattain: Solve multiobjective goal attainment problem,
• fminbnd: Find a minimum of a function of one variable on a fixed interval,
• fseminf: Find a minimum of a semi-infinitely constrained multivariable nonlinear function,
• lsqnonlin: Solve nonlinear least-squares (nonlinear data-fitting) problem,
Dr. Shivi Agarwal; NWLMB-2014 54