Académique Documents
Professionnel Documents
Culture Documents
engineering.purdue.edu/ITE
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
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 <ENTER>
Introduction to Optimization
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 >>b =10 b= 10 >>c= a*b c= 50 >>d = 10/5 d= 2 <ENTER>
<ENTER>
<ENTER>
<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
The basic arithmetic operations are given by: Operation Addition a+b Subtraction a-b Multiplication a.b Division a/b Exponential ab Symbol + * / or \ ^
Introduction to Optimization
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 2 3 4 5 6 7 8 9
Matrix Operations available in MATLAB Function + * ^ \ / Task Addition Subtraction Multiplication Power Transpose Left Division Right Division
Examples matrix addition: >> a1 = a+b a1 = 2 6 10 (matrices a and b are added and stores in a1)
Introduction to Optimization
Page 6 of 18
engineering.purdue.edu/ITE
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.
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
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);
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
% 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
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; 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 we have to click on New in the file menu in menubar. It will open a new file as we are doing in word; and if we save this file (called m-file), will be saved in bin folder of MATLAB. Such files are called M-files because they have an extension of .m in its filename. Much of your work with MATLAB will be creating and refining M-files. There are two types of M-files: Script Files and Function Files.
Introduction to Optimization
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
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
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.
Given the initial interval of decision variable (x) as *0, 2+ and final confidence interval (2) to be 0.1.
Introduction to Optimization
Page 17 of 18
engineering.purdue.edu/ITE
Introduction to Optimization
Page 18 of 18