josh xiaomin xi phd candidate feb 27, 2013 a tutorial from
TRANSCRIPT
Josh Xiaomin XiJosh Xiaomin XiPhD CandidatePhD CandidateFeb 27, 2013Feb 27, 2013
A tutorial fromA tutorial from
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 2 / INF
OverviewOverview
Introduction
Installation (Toolboxes)
Layout of Matlab Windows
Basics of Matlab language
Arithmetic Operations
Variables
Matrix
Plot
Functions: inline and sym
Programming in Matlab
m-file
Optimization in Matlab
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 3 / INF
IntroIntro
MATLAB: MATrix and LABoratory
First developed by Dr. Cleve Molder: Fortran based
In 1984, MathWorks was founded: C based
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 4 / INF
Intro: InstallationIntro: Installation
Go to: http://ocio.osu.edu/software/directory/slwin/
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 5 / INF
Intro: InstallationIntro: Installation
Select the tool boxes that you need
e.g. Matlab, curve fitting, optimization, statistics, symbolic math, etc.
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 6 / INF
Intro: Matlab Windows LayoutIntro: Matlab Windows Layout
Command Window
Command History
Current Directory Browser
Workspace Browser
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 7 / INF
OverviewOverview
Introduction
Installation (Toolboxes)
Layout of Matlab Windows
Basics of Matlab language
Arithmetic Operation
Variables
Matrix
Plot
Functions: inline and sym
Programming in Matlab
m-file
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 8 / INF
Basics: Arithmetic Basics: Arithmetic OperationsOperations
+ plus
- minus
* multiply
/ right divide
\ left divide
^ exponential
2+3=5
2-3= -1
2*3=6
2/3=0.6667
2\3=1.5000
2^3=8
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 9 / INF
Basics: VariablesBasics: Variables
How to define a variable name
Numbers and letter, but first component must be a letter
Case sensitive
No space, punctuations (except underline)
Special variables
ans
NaN, nan
Inf, -Inf
pi
i, j
realmax, realmin
However, you can redefine these variables, and use “clear” to clear redefinition.
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 10 / INF
Basics: MatrixBasics: Matrix
How to define a matrix/vector
A = [1 2 3 4; 4 5 6 7] ~~ [1:4; 4:7] (!!! Comma, colon, semicolon bracket)
Special matrix
zeros(m,n)
ones(m,n)
diag(vec)
Matrix operation
Basic arithmetic operation (!!! Period & dimensions)
Inverse (inv) and transpose (apostrophe)
Read/change matrix component (!!! parenthesis)
Stacking and breaking
Size(), length(), eig()
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 11 / INF
Basics: PlotBasics: Plot
An example of attenuation oscillation curve:
t=0:pi/50:4*pi; y=exp(-t/3).*sin(3*t); plot(t,y,'-r') grid
3 3/ sin ,ty e t 0 4[ , ]t
Use “help” to find more info of plot, e.g. linespec, legend, title, xlabel
loglog log plot, taking log on both x & y
semilogx log plot, taking log only on x
semilogy log plot, taking log only on y
mesh 3-d plot
bar bar chart
Subplot one figure with sub figures
Other
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 12 / INF
Basics: FunctionsBasics: Functions
Use “sym” / “syms” Use “inline”
23 2sin( )f x y
syms x y;f=3*sin(2*x^2-y)
Df=diff(f)Df2=diff(f,2)
subs(f,x,4)
fin=inline(char(f)) fin(1,1)
f=inline(‘3*sin(2*x^2-y)’)
f=inline(‘3*sin(2*x^2-y)’,’x’,’y’)
f(1,1)
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 13 / INF
OverviewOverview
Introduction
Installation (Toolboxes)
Layout of Matlab Windows
Basics of Matlab language
Arithmetic Operation
Variables
Matrix
Plot
Functions
Programming in Matlab
m-file: run large program, build large function
Control flow: if-else, while, for
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 14 / INF
M FileM File
Replace command window when running large code
Easy management
Can be reused in future
Define functions / sub-sections
Better structure
Good for complicated programming/logic
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 15 / INF
Control FlowControl Flow
If-ElseIf condition
expression(s) 1;
else
expression(s) 2;
end
If condition1
expression(s) 1;
else if condition2
expression(s) 2;
else
expression(s) 3;
end
240
2130
1020
010
t
t
t
t
F
If t >= 2
F = 40;
else if t > 1
F = 30;
else if t > 0
F = 20;
else
F = 10;
end
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 16 / INF
Control FlowControl Flow
For / while
for i=1:5;
for j=1:3
x(i , j)= i * j;
end
end
x =
1 2 3 2 4 6 3 6 9 4 8 12 5 10 15
n=1;
while prod ( 1 : n ) < 100;
n=n+1;
end
n
Result: n = 5 。
Because 5x4x3x2x1=120
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 17 / INF
Optimization In MatlabOptimization In Matlab
Common optimization functions
linprog: linear programming
Quadprog: quadratic programming
fmincon: constrained non-linear minimization
fminsearch, fminunc: unconstrained nonlinear minimization
fsolve: non-linear system of equations solve
lsqlin: linear least square with linear constraints
INFORMS OSU Josh Xiaomin XiA Matlab Tutorial 18 / INF
Optimization: linprogOptimization: linprog
linprog: linear programming
http://www.mathworks.com/help/optim/ug/linprog.html
Min f(x) = –5x1 –4x2 –6x3 s.t. x1 – x2 + x3 ≤ 20 3x1 + 2x2 + 4x3 ≤ 42 3x1 + 2x2 ≤ 30 0 ≤ x1, 0 ≤ x2, 0 ≤ x3.