Documente Academic
Documente Profesional
Documente Cultură
engineering.purdue.edu/ITE
Introduction to Optimization in
MATLAB
Updated October 2, 2010 Compiled by Amit Kumar and Sushant Sharma
Table of Contents
MATLAB BASICS ..................................................................................................................................................... 2
WHAT IS MATLAB? ...................................................................................................................................................... 2
USAGE OF MATLAB .................................................................................................................................................... 2
MATLAB GETTING STARTED ................................................................................................................................... 2
WORKING WITH MATRICES: ...................................................................................................................................... 5
CREATING LOOPS............................................................................................................................................................7
For loop .................................................................................................................................................................7
While Loop ............................................................................................................................................................8
CREATING LOGICAL OPERATIONS ....................................................................................................................................... 8
RIDING THE TIGER .................................................................................................................................................. 9
HOW TO WRITE CODE THAT RUNS FASTER .......................................................................................................................... 9
Preallocation ......................................................................................................................................................... 9
Vectorization ....................................................................................................................................................... 10
Logical Indexing .................................................................................................................................................. 10
MATLAB PROGRAMMING .................................................................................................................................... 11
WRITE YOUR OWN CODE ......................................................................................................................................... 11
WRITE SIMPLE M-FILES.................................................................................................................................................. 11
OPTIMIZATION TOOLBOX .................................................................................................................................... 13
LINEAR CONSTRAINED OPTIMIZATION PROBLEM ................................................................................................................ 13
PROBLEM FORMULATION............................................................................................................................................... 14
NON-LINEAR OPTIMIZATION PROBLEM ............................................................................................................................ 15
PRACTICE EXERCISE .............................................................................................................................................. 17
PROBLEM 1:................................................................................................................................................................17
Introduction to Optimization
Page 1 of 18
engineering.purdue.edu/ITE
MATLAB BASICS
What is MATLAB?
MATLAB is an interactive, matrix-based system for scientific and engineering numeric
computation and visualization. The word MATLAB stands for MATrix LABoratory. Each entry
is taken as a matrix in it, in particular scalar is considered as a 1 by 1 matrix. MATLAB is
available for a number of operating systems like Windows, Linux etc. MATLAB was originally
written in FORTRAN and is licensed by The Math Works, Inc,
(http:// www.mathworks.com).
USAGE OF MATLAB
MATLAB is well known software for numerical linear algebra and matrix computation. Industry
use it for research and to solve practical engineering and mathematical problems. Also, in
automatic control theory, statistics and digital signal processing (Time-Series Analysis) one can
use MATLAB. The following tool boxes make it useful in soft computing at various industrial
and scientific areas:
(i) Neural Networks
(ii) Optimization
(iii) Genetic Algorithms
(iv) Wavelets
(v) Fuzzy Logic
(vi) Control systems
(vi) Signal Processing
>>
with a blinking cursor appearing right of the prompt, telling you that MATLAB is waiting to
perform a mathematical operation you would like to give.
Introduction to Optimization
Page 2 of 18
engineering.purdue.edu/ITE
Introduction to Optimization
<ENTER>
Page 3 of 18
engineering.purdue.edu/ITE
( ii ) If you want to store the values 5 and 10 in MATLAB variables a & b and store the values
of their product and division in c and d, do as follows:
>> a =5
a=
5
<ENTER>
>>b =10
b=
10
<ENTER>
>>c= a*b
c=
50
<ENTER>
>>d = 10/5
d=
2
<ENTER>
You can exit MATLAB with the command exit or quit. A computation can be stopped with
[ctrl-c]
Introduction to Optimization
Page 4 of 18
engineering.purdue.edu/ITE
Symbol
+
*
/ or \
^
1 by 1 matrix
Row vector
Column vector -
Introduction to Optimization
Page 5 of 18
engineering.purdue.edu/ITE
The rows are separated by semicolons and elements are separated by space or by comma. To transpose
a matrix and store in b we can run following command
>> b = a
b=
1
Task
Addition
Subtraction
Multiplication
Power
Transpose
Left Division
Right Division
a1 =
2
6 10
6 10 14
10 14 18
Examples matrix multiplication:
Introduction to Optimization
Page 6 of 18
engineering.purdue.edu/ITE
>>a2= a * b
= 14 32 50
32 77 122
50 122 194
>>a3= a.*b
= 1
8 21
8 25 48
21 48 81
Creating loops
Similar to other programming language MATLAB also has two looping constructs: for loops and while
loops.
For loop
For loops are useful for repeating a process or a set of processes (or operations) a certain number of
times. Each for loop should have counter variable, starting value of the counter variable, increment
(default 1 if not specified) and upper limit of counter variable. In addition all for loops should be closed
by an end statement.
An example of for loop
for i=1:1:10
disp(i);
end
Here, initial value of counter variable i is 1, increment is defined as 1 and upper limit is 10. Hence it will
execute the loop ten times. In each loop it will display the value of the counter variable. An another loop
can be nested inside a loop as belowfor i=1:1:10
for j=1:1:3
disp(i);
end
end
Now this nested loop will execute the value of counter variable i three times.
Introduction to Optimization
Page 7 of 18
engineering.purdue.edu/ITE
While Loop
While loops are useful for repeating a process or a set of processes (or operations) until a specified
condition(s) is (are) met. Following is the example of a while loop
i=1;
while ~(i==10)
disp(i);
i=i+1;
end
In this example of while loop, the loop runs 9 times as opposed to for loop. The while loop first
evaluates the conditional statement and then executes the processes inside the loop if conditional
statement is true else the loop is terminated. So in this example as soon as the value of i will turn 10 the
loop will be terminated.
So, each while loop needs a logical test and it runs while the answer to the logical test is valid. Hence
next we discuss how to create the logical statements.
Less than
<=
>
Greater than
>=
==
Equal to
~=
Not equal to
Introduction to Optimization
Page 8 of 18
engineering.purdue.edu/ITE
Following is an example of using logical operation in an if-else statementclear; %clear all variables
clc; %clear screen
a=[1 2 3 2 4 2 5];
disp('values of vector "a" before');
disp(a);
m=length(a);
for i=1:1:m
if a(i)==2
a(i)=999;
else
a(i)=0;
end
end
disp('after modifying "a" using logical operations');
disp(a);
%clc;
clear;
tic; %start of measuring time
a=zeros(50000,1); %preallocation
sum=0;
for i=1:1:50000
a(i)=i;
sum=sum+a(i);
end
disp('sum of 1 to 50,000 is =');
disp(sum);
toc; %End of measuring time
clc;
clear;
tic; %start of measuring time
%a=zeros(50000); % No preallocation
sum=0;
for i=1:1:50000
a(i)=i;
sum=sum+a(i);
end
disp('sum of 1 to 50,000 is =');
disp(sum);
toc;%End of measuring time
Introduction to Optimization
Page 9 of 18
engineering.purdue.edu/ITE
Vectorization
Vectorization gives faster performance compared to loop. Run the following codes to see the benefit of
vectorization.
Code with vectorization
Logical Indexing
Indexing also gives faster performance compared to loop. Run the following codes to see the benefit of
indexingCode with indexing
%clc;
clear;
tic; %start of measuring time
a=1:50000;
clc;
clear;
tic; %start of measuring time
a=1:50000;
for i=1:1:50000
if rem(a(i),2)==0
a(i)=0;
end
end
s=sum(a);
disp('sum of odd integers between 1
and 50,000 is =');
disp(s);
toc;%End of measuring time
b=logical(rem(a,2)==0);
a(b)=0;
s=sum(a);
disp('sum of odd integers between 1
and 50,000 is =');
disp(s);
toc;%End of measuring time
Introduction to Optimization
Page 10 of 18
engineering.purdue.edu/ITE
MATLAB PROGRAMMING
We can also do programming in MATLAB as we are doing in FORTRAN, C & C++. To make a file in
MATLAB e ha e to li k o Ne i the file e u i
e u ar. It ill ope a e file as e are doi g
i word ; a d if e sa e this file alled -file , ill e sa ed i
i folder of MATLAB.
Such files are alled M-files e ause the ha e a e te sio of .
with MATLAB will be creating and refining M-files.
There are two types of M-files: Script Files and Function Files.
Introduction to Optimization
<ENTER>
Page 11 of 18
engineering.purdue.edu/ITE
The % symbol indicates that the rest of the line is a comment. MATLAB will ignore the rest of the line.
Introduction to Optimization
Page 12 of 18
engineering.purdue.edu/ITE
OPTIMIZATION TOOLBOX
Manufacturing base
California
Illinois
Atlanta
Profit (in hundred million USD per
million unit)
Introduction to Optimization
0
2
2
5
Capacity Available
(per million unit)
4
12
18
Page 13 of 18
engineering.purdue.edu/ITE
Problem Formulation
Objective function:
x1 = number of iphones (in million) and x2 = number of ipads (in million)
Goal : To maximize profit (Z)
Or,
Decision variable:
Constraints: See Table 1 to understand the constraints
Introduction to Optimization
Page 14 of 18
engineering.purdue.edu/ITE
1. Fun.m
function z = fun(x)
z = x(1)^2+x(2)^2+x(3)^2;
end
Introduction to Optimization
Page 15 of 18
engineering.purdue.edu/ITE
2. Constr.m
%
function [g, h] = constr(x)
g(1) = x(3)/x(2)-1;
%
h = x(1)-x(2)^2+x(2)*x(3)-4;
%
%EOF
Introduction to Optimization
Page 16 of 18
engineering.purdue.edu/ITE
PRACTICE EXERCISE
Problem 1:
Write a MATLAB code for golden section method to solve the following optimization problem. You can
use the given flow chart of the method provided in this handout as a reference.
Introduction to Optimization
aria le
as
a d fi al o fide e i ter al
to e . .
Page 17 of 18
engineering.purdue.edu/ITE
Introduction to Optimization
Page 18 of 18