Sunteți pe pagina 1din 18

Institute of Transportation Engineers, Purdue Student Chapter

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

Institute of Transportation Engineers, Purdue Student Chapter

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

MATLAB GETTING STARTED


By clicking the MATLAB shortcut icon on the desktop of your computer (or selecting from the
program menu) you can access MATLAB. This results in getting MATLAB command window
with its prompt:

>>
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

Institute of Transportation Engineers, Purdue Student Chapter

engineering.purdue.edu/ITE

Simple Math Calculations


( i ) If you want to add two numbers say 7 & 12, type as follows
>> 5+10
and press the ENTER or return key, you see the following output:
ans =
15
Here, ans stands for the answer of computation. Similarly, the following gives product and
difference of these numbers,
>> 5*10
ans =
50

Introduction to Optimization

<ENTER>

Page 3 of 18

Institute of Transportation Engineers, Purdue Student Chapter

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

Institute of Transportation Engineers, Purdue Student Chapter

engineering.purdue.edu/ITE

The basic arithmetic operations are given by:


Operation
Addition
a+b
Subtraction
a-b
Multiplication
a.b
Division
a/b
Exponential
ab

Symbol
+
*
/ or \
^

WORKING WITH MATRICES:


MATLAB works with essentially only one kind of objects, i.e. a rectangular numerical matrix with possibly
complex entries. All variables represent matrices.
Scalars

1 by 1 matrix

Row vector

matrix with one row

Column vector -

matrix with one column.

If you want to store a matrix


1

you type the following in the MATLAB prompt.


>> a = [1 2 3; 4 5 6; 7 8 9]
a=
1

Introduction to Optimization

Page 5 of 18

Institute of Transportation Engineers, Purdue Student Chapter

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

Matrix Operations available in MATLAB


Function

Task

Addition

Subtraction

Multiplication

Power
Transpose

Left Division

Right Division

Examples matrix addition:


>> a1 = a+b

(matrices a and b are added and stores in a1)

a1 =
2

6 10

6 10 14
10 14 18
Examples matrix multiplication:

Introduction to Optimization

Page 6 of 18

Institute of Transportation Engineers, Purdue Student Chapter

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

Institute of Transportation Engineers, Purdue Student Chapter

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.

Creating Logical Operations


The logical statements can be combination of variables, arithmetic operators and relational operators
and results in true or false. Following table shows the list of relational operators.
Operator Description
<

Less than

<=

Less than or equal to

>

Greater than

>=

Greater than or equal to

==

Equal to

~=

Not equal to

For more details visit the mathworks web page


http://www.mathworks.com/help/techdoc/matlab_prog/f0-40063.html
The local operations can be used by inbuilt function logical or under the if(else) statement. Following
example shows use of inbuilt function logical.
clear; %clear all variables
clc; %clear screen
a=[1 2 3 2 4 2 5];
disp('values of vector "a" before');
disp(a);
b=logical(a==2);
disp('after modifying "a" using logical operations');
a(b)=99;
disp(a);

Introduction to Optimization

Page 8 of 18

Institute of Transportation Engineers, Purdue Student Chapter

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);

RIDING THE TIGER


How to Write Code that Runs Faster
There are three important techniques to speed up the execution of MATLAB code namely, preallocation,
vectorization and indexing by logical expression.
Preallocation
It means defining the size of a variable (matrix or vector) and giving some initial values. Run the
following two codes below see the difference in execution timeCode with preallocation

Code without preallocation

%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

Institute of Transportation Engineers, Purdue Student Chapter

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

Code without vectorization

% Vectorization example for


%elementwise multiplication
%clc;
clear;
tic; %start of measuring time
a=ones(50000,1);
b=ones(50000,1);
c=zeros(50000,1);
b=b*2;
%for i=1:1:50000
c=a.*b;
s=sum(c);
disp(s);
%end
toc;%End of measuring time

% No_Vectorization example for


%elementwise multiplication
%clc;
clear;
tic; %start of measuring time
a=ones(50000,1);
b=ones(50000,1);
c=zeros(50000,1);
b=b*2;
s=0;
for i=1:1:50000
c(i)=a(i)*b(i);
s=s+c(i);
end
disp(s);
toc;%End of measuring time

Logical Indexing
Indexing also gives faster performance compared to loop. Run the following codes to see the benefit of
indexingCode with indexing

Code without 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

Institute of Transportation Engineers, Purdue Student Chapter

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.

i its file a e. Mu h of our ork

There are two types of M-files: Script Files and Function Files.

WRITE YOUR OWN CODE


Write Simple m-files
A m-file consists of a sequence of normal MATLAB statements. If the file has the filename, say, test.m,
then the MATLAB command >> test will cause the statements in the file to be executed. Variables in a
script file are global and will change the value of variables of the same name in the environment of the
current MATLAB session.
M files are often used to enter data into a large matrix; in such a file, entry errors can be easily edited
out.
Example: In a file test.m enter the following:
% This is a sample m-file
a = [4,2,3;0,1,2;1,2,3]
b =a ;
c = a+b
d = inv(c)
save this file. Then to execute this file, type
>>test

Introduction to Optimization

<ENTER>

Page 11 of 18

Institute of Transportation Engineers, Purdue Student Chapter

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

Institute of Transportation Engineers, Purdue Student Chapter

engineering.purdue.edu/ITE

OPTIMIZATION TOOLBOX

Linear Constrained Optimization Problem


Example 1:
Apple manufactures multiple products including iphone and ipad. It has three manufacturing
bases. The products are assembled at assembling unit by the component parts arriving from
three manufacturing bases.
Following table describes the percentage of capacity available at the manufacturing bases due
to the other products being developed at same location. The table also shows percentage of
capacity required per million units production of iphone and ipad, in addition to the profit.
Table 1: Capacity of manufacturing base for iphone and ipad

Manufacturing base
California
Illinois
Atlanta
Profit (in hundred million USD per
million unit)

Introduction to Optimization

Capacity Used (per million unit)


iphone
ipad
1
0
3
3

0
2
2
5

Capacity Available
(per million unit)
4
12
18

Page 13 of 18

Institute of Transportation Engineers, Purdue Student Chapter

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

Institute of Transportation Engineers, Purdue Student Chapter

engineering.purdue.edu/ITE

Non-Linear Optimization Problem

Create following M files

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

Institute of Transportation Engineers, Purdue Student Chapter

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

Institute of Transportation Engineers, Purdue Student Chapter

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.

Given the initial interval of de isio

Introduction to Optimization

aria le

as

a d fi al o fide e i ter al

to e . .

Page 17 of 18

Institute of Transportation Engineers, Purdue Student Chapter

engineering.purdue.edu/ITE

Figure: Flow chart of golden section method

Introduction to Optimization

Page 18 of 18

S-ar putea să vă placă și