0% found this document useful (0 votes)
0 views61 pages

L01 Matlab Introduction

The document is a lecture on using MATLAB for optimization, covering various features such as the Command Window, Command History, and Workspace Browser. It includes examples of MATLAB commands, data manipulation, control structures, and plotting functions. The content is designed for engineering students and professionals to understand practical applications of MATLAB in optimization tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views61 pages

L01 Matlab Introduction

The document is a lecture on using MATLAB for optimization, covering various features such as the Command Window, Command History, and Workspace Browser. It includes examples of MATLAB commands, data manipulation, control structures, and plotting functions. The content is designed for engineering students and professionals to understand practical applications of MATLAB in optimization tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Course #

Course Name

Lecture - 1

Instructor: First and Last names

Reference: Book Chapter 1


Copyright ©2014 by A. Messac Optimization in Practice with MATLAB:
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 2
for Engineering Students and Professionals
The Command Window is used to enter variables, evaluate MATLAB
commands, and run M-files or functions.

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 3


for Engineering Students and Professionals
The Command History window is used to view previously-used
functions, and to copy and execute selected lines from those functions.

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 4


for Engineering Students and Professionals
The Current Directory browser can be used to view, open, and make
changes to MATLAB related directories and files.

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 5


for Engineering Students and Professionals
The Workspace Browser is used to view the workspace, and the
information about each variable.

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 6


for Engineering Students and Professionals
The Variable Editing Panel opens when you double-click on a variable
in the Workspace Browser. It can be used to view and directly edit
one or two dimensional numeric arrays, strings, and arrays of strings
in the workspace.
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 7
for Engineering Students and Professionals
The Editor/debugger provides a GUI to create and debug M-files.

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 8


for Engineering Students and Professionals
The Help browser is used to search and view documentation for all MATLAB
products

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 9


for Engineering Students and Professionals
>> a = 1 + 2
a=
3
>> b = 4
b=
4
>> c = sqrt(a^2 + b^2)
c=
5
>> x = a + b;
>> y = 2*x
y=
14

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 10


for Engineering Students and Professionals
As you type commands or expressions that are very long, you can
simply type the command on several lines. To do this, begin the
expression on one line, then type three periods followed by Enter or
Return (where you want to break), and simply continue the expression
on the following line.

>> a_long_variable = 2;
>> another_long_variable = 3;
>> x = a_long_variable + a_long_variable^2 ...
+ another_long_variable
x=
9

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 11


for Engineering Students and Professionals
>> M = [ 1 2 3 4; 5 6 7 8; 9 10 11 12]
M=
1 2 3 4
5 6 7 8
9 10 11 12

>> v=[11 ; 22 ; 33]


v=
11
22
33
>> mv = [M v]
mv =
1 2 3 4 11
5 6 7 8 22
9 10 11 12 33

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 12


for Engineering Students and Professionals
>> h = [11 22 33 44]
h=
11 22 33 44
>> mh = [M ; h]
mh =
1 2 3 4
5 6 7 8
9 10 11 12
11 22 33 44

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 13


for Engineering Students and Professionals
>> ones(3)

ans =
ones(3) produces a 3 by 3 matrix of ones.
1 1 1

1 1 1

1 1 1

>> ones(1,3) ones(1,3) produces a 1 by 3 matrix of ones.


ans =

1 1 1

>> zeros(2)
zeros(3) produces a 2 by 2 matrix of zeros.
ans =

0 0

0 0

>> zeros(1,3)
zeros(1,3) produces a 1 by 2 matrix of zeros.
ans =

0 0 0

>> A = eye(3)
eye(3) produces a 3 by 3 identity matrix.
A=

1 0 0

0 1 0

0 0 1

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 14


for Engineering Students and Professionals
>> A = [1 3 5; 4 6 8]
A=
1 3 5
4 6 8
>> A(1,2)
ans =
3
>> A(2,end)
ans =
8

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 15


for Engineering Students and Professionals
>> A = [1 3 5; 4 6 8]
A=
1 3 5
4 6 8

>> v=[1;1;1]
v=
1
1
1
>> A*v
ans =
9
18

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 16


for Engineering Students and Professionals
>> a = 2:8

a=

2 3 4 5 6 7 8

>> 1:2:9

ans =

1 3 5 7 9

>> 50:-5:30

ans =

50 45 40 35 30

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

P=

1 2 3 4

5 6 7 8

>> pp = P(2, 2:4)

pp =

6 7 8

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 17


for Engineering Students and Professionals
>> a=[1 2 3;4 5 6]
a=
1 2 3
4 5 6
>> b = a’
b=
1 4
2 5
3 6

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 18


for Engineering Students and Professionals
The linspace function is a quick way to generate a row vector of n
linearly spaced points between two given numbers.
The syntax is linspace(a,b,n).
If the number, n, is not specified, it generates 100 points between a
and b by default.
If n is not specified, the syntax is linspace(a,b).

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 19


for Engineering Students and Professionals
Below are some more examples of MATLAB expressions.

>> x = 2

x=

>> y = exp(x) + log(x)

y=

8.0822

>> z = (-sin(y)) + abs(-x)

z=

1.0259

>> complex_number = 4 + 3i

complex_number =

4.0000 + 3.0000i

>> magnitude = abs(complex_number)

magnitude =

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 20


for Engineering Students and Professionals
The current directory is generally displayed in a text box towards
the top of the MATLAB screen. Alternatively, you can type pwd at
the command prompt to display the current directory.

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 21


for Engineering Students and Professionals
You can set the path to directories that you use often. The files in
these directories can then be directly accessed from any other
directory. The path can be set from File >> Set Path menu.

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 22


for Engineering Students and Professionals
Any variable in the workspace can be saved to the hard disk using the
save command.
save FILENAME x y

The above command saves the workspace variables x and y to a file


named FILENAME.mat in the current directory. Note that there is no
comma in the above syntax. To retrieve these variables, type the
command
load FILENAME x y

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 23


for Engineering Students and Professionals
The for loop executes a set of statements for a specified number of
times.
>> for i = 1:5
x(i)=1;
end
>> x
x=
1 1 1 1 1

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 24


for Engineering Students and Professionals
The STATEMENTS will be executed as long as the EXPRESSION is
true.

while EXPRESSION
STATEMENTS
end

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 25


for Engineering Students and Professionals
The if statement allows you to execute statements provided
certain conditions hold. Nested if statements must each be
paired with a matching end.

if EXPRESSION
STATEMENTS
else
STATEMENTS
end

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 26


for Engineering Students and Professionals
Switch provides a way to switch between several cases based on an
expression.

>> x=2
switch x
case 1
disp('x is equal to 1')
case 2
disp('x is equal to 2')
end

x=

x is equal to 2

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 27


for Engineering Students and Professionals
A complete list of operators can be obtained by typing help ops at the
command prompt.
Some commonly used relational operators are
EQUAL (==)
GREATER THAN, (>)
LESS THAN, (<)
NOT EQUAL (~=)

Some commonly used logical operators are:


AND (&)
OR (|)
NOT (~)

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 28


for Engineering Students and Professionals
Script files are simply a series of MATLAB commands stored in a
.m file. They do not take any arguments, or return any output.

% This is my first .m file


var = 5; new_var = var^2;
if new_var > 5
disp(‘My first output’);
end

>> myMFile
My first output

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 29


for Engineering Students and Professionals
Function M-files can receive one or more variables as input
arguments, and can return one or more variables as outputs. The
input and output variables are available for use outside of the
function, but the variables or parameters used inside the
function are not available outside the function.

function output_var = cuberoot(input_var)


output_var = input_var^(1/3);

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 30


for Engineering Students and Professionals
Subfunctions can be declared following the definition of the main
function in the same M-file. Subfunctions are visible to the main
function and other subfunctions in the same M-file, but are not
visible outside the M-file in which they are declared.

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 31


for Engineering Students and Professionals
All the variables created within a function M-file are local to that
function, that is, they cannot be accessed outside the function.
Similarly, workspace variables are local to the workspace, and are
not available in any function. To make a workspace variable, x,
globally available, we use the global command as follows.

global x

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 32


for Engineering Students and Professionals
helpdesk opens a MATLAB help GUI.
helpwin opens a hypertext help browser.
demo starts the MATLAB demonstration.
help prints on the screen the various help topics available.
help followed by a help topic, or any function name, provides help
on the requested topic or function.
lookfor followed by a topic keyword, which gives the names of all
the MATLAB functions that have that keyword on the first help
line (That keywork does not have to be the name of a function,
unlike for the help command)

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 33


for Engineering Students and Professionals
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y);
xlabel(‘X-axis’)
ylabel(‘Y-axis’)
title(‘Plot of Y = sin(x)’)

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 34


for Engineering Students and Professionals
clc
clear
x = 0:pi/100:2*pi;
y = sin(x);
y2 = 0.5*sin(x+1);
plot(x,y,x,y2,‘--’);
xlabel(‘X-axis’)
ylabel(‘Y-axis’)
title(‘Plot of Y = sin(x)’)
legend(‘sin(x)’,‘0.5*sin(x+1)’)

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 35


for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 36
for Engineering Students and Professionals
[X,Y] = meshgrid(-5:.5:5,-5:.5:5);
Z = (3*X.^2+4*Y.^2);
[C,h] = contour(X,Y,Z,5);
xlabel(’X-axis’)
ylabel(’Y-axis’)
title(’The contour plots …
of 3*X^2+4*Y^2 = C’)

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 37


for Engineering Students and Professionals
x = 0:pi/20:2*pi;
y = sin(x);
scatter(x,y);
xlabel(’X-axis’)
ylabel(’Y-axis’)
title(’Plot of Y = sin(x)’)

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 38


for Engineering Students and Professionals
fplot(’x^2+10’,[-20 20])
xlabel(’X-axis’)
ylabel(’Y-axis’)
title(’The function plotted …
using fplot’)

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 39


for Engineering Students and Professionals
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
mesh(X,Y,Z)
xlabel(’X-axis’)
ylabel(’Y-axis’)
zlabel(’Z-axis’)
title(’Mesh plot’)

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 40


for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 41
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 42
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 43
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 44
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 45
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 46
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 47
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 48
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 49
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 50
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 51
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 52
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 53
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 54
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 55
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 56
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 57
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 58
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 59
for Engineering Students and Professionals
Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 60
for Engineering Students and Professionals
What’s Next?

 Lecture 2 - Mathematical Preliminaries


 Lecture 3 - Design, Analysis, Modeling, and Optimization
 Lecture 4 - Introduction to Quantitative Optimization

Copyright ©2014 by A. Messac Optimization in Practice with MATLAB: 61


for Engineering Students and Professionals

You might also like