Upload
others
View
17
Download
1
Embed Size (px)
Citation preview
MATLAB Tutorials
Violeta Ivanova, Ph.D.Educational Technology ConsultantMIT Academic Computing
16.01 & 16.02 Unified Engineering - Fall 2006
Unified Engineering: MATLAB Tutorials
This Tutorial
Class materialsweb.mit.edu/acmath/matlab/unified/linsys
Topics Basics Review Linear Systems
Unified Engineering: MATLAB Tutorials
Other References
Mathematical Tools at MITweb.mit.edu/ist/topics/math
Unified: Intro to MATLAB Tutorialweb.mit.edu/acmath/matlab/unified/intro
Other Course 16 MATLAB Tutorialsweb.mit.edu/acmath/matlab/course16/
MATLAB Basics Review
Toolboxes & HelpVectors & MatricesOperators & Functions
Unified Engineering: MATLAB Tutorials
Help in MATLAB
Command line help>> help <command>
e.g. help eig>> lookfor <keyword>
e.g. lookfor eigenvalue Help Browser
Help->Help MATLAB
Unified Engineering: MATLAB Tutorials
MATLAB Toolboxes & Help MATLAB
+ Getting Started+ Mathematics
+ Matrices and Linear Algebra+ Solving Linear Systems of Equations Eigenvalues
+ Polynomials and Interpolation+ Convolution and Deconvolution
+ Programming+ Graphics
Unified Engineering: MATLAB Tutorials
Variables & Data Types Data type detection
>> a = 5; a =‘ ok’; a = 1.3
Numeric data types>> x = 5>> y = 5.34; x = round(y)>> w = 5 + 6i>> wr = real(w); wi = imag(w)
Built-in constants: pi i j Inf Special characters
[] () {} ; % : = . … @
Unified Engineering: MATLAB Tutorials
Vectors
Row vector>> R1 = [1 6 3 8 5]>> R2 = [1 : 5]>> R3 = [-pi : pi/3 : pi]
Column vector>> C1 = [1; 2; 3; 4; 5]>> C2 = R2'
Unified Engineering: MATLAB Tutorials
Matrices
Creating a matrix>> A = [1 2.5 5 0; 1 1.3 pi 4]
>> A = [R1; R2]
Accessing elements>> A(1,1)
>> A(1:2, 2:4)
>> A(:,2)
Unified Engineering: MATLAB Tutorials
Matrix Operations Operators + and -
>> X = [x1 x2]; Y = [y1 y2]; A = X+Y A =
x1+y1 x2+y2
Operators *, /, and ^>> Ainv = A^-1 Matrix math is default!
Operators .*, ./, and .^>> Z = [z1 z2]; B = [Z.^2 Z.^0]B =
z12 z2
2 1 1
Unified Engineering: MATLAB Tutorials
Built-In Functions Matrices & vectors
>> [n, m]= size(A)>> n = length(X)>> M1 = ones(n, m)>> M0 = zeros(n, m)>> En = eye(n)>> N1 = diag(En)>> [evecs, evals] = eig(A)
And many others …>> y = exp(sin(t)+cos(t))
Unified Engineering: MATLAB Tutorials
Graphics 2D linear plots: plot
>> plot (t, z, ‘r-’) Colors: b, r, g, y, m, c, k, w Markers: o, *, ., +, x, d Line styles: -, --, -., :
Annotating graphs>> legend (‘z = f(t)’)>> title (‘Position vs. Time’)>> xlabel (‘Time’)>> ylabel (‘Position’)
Unified Engineering: MATLAB Tutorials
Multiple Plots Multiple datasets on a plot
>> p1 = plot(xcurve, ycurve)>> hold on>> p2 = plot(Xpoints, Ypoints, ‘ro’)>> hold off
Subplots on a figure>> s1 = subplot(1, 2, 1)>> p1 = plot(time, velocity)>> s2 = subplot(1, 2, 2)>> p2 = plot(time, acceleration)
MATLAB Linear Systems
Linear Equations & SystemsEigenvalues & EigenvectorsConvolution
Unified Engineering: MATLAB Tutorials
Systems of Linear Equations Definition:
Matrix form: Ax = b
a11x1+ a
12x2+ ...+ a
1mxm= b
1
a21x1+ a
22x2+ ...+ a
2mxm= b
2
...
an1x1+ a
n2x2+ ...+ a
nmxm= b
m
a11
... a1n
... ... ...
an1
... amn
!
"
###
$
%
&&&
x1
...
xm
!
"
###
$
%
&&&
=
b1
...
bm
!
"
###
$
%
&&&
Unified Engineering: MATLAB Tutorials
Solving Linear Equations Define matrix A
>> A = [a11 a12 … a1m a21 a22 … a2m …
an1 an2 … anm]
Define vector b>> b = [b1; b2; … bm]
Compute vector x>> x = A \ b
Unified Engineering: MATLAB Tutorials
Differential Equations Ordinary Differential Equation (ODE)
Linear systems -> State Equation
Eigenvalues, λi, and eigenvectors, vi,of a square matrix A
A vi = λi vi
y ' = f (t, y)
!x1
...
!xn
"
#
$$$
%
&
'''
=
a11
... a1n
... ... ...
an1
... ann
"
#
$$$
%
&
'''
x1
...
xn
"
#
$$$
%
&
'''
x
•
= Ax
Unified Engineering: MATLAB Tutorials
Linear Dynamic Networks Solution using eigenvalue method
Identify the network’s states ………………………………..
Identify initial conditions …………………………………..
Find the state equation …………………………….
Find eigenvalues & eigenvectors …………
The general solution is ……….
Solve for ai …………….
x
•
= Ax
x
x t( ) = ai
i
! vie
"it
x 0( )
!i,"
i
a = v1
v2... v
n[ ]!1
x 0( )
Unified Engineering: MATLAB Tutorials
Example: RCL Circuitdv
1
dt= !
1
2v1! 2i
4
di4
dt=1
2v1! 3i
4
v40( ) = 2
i40( ) = 1
R3
6Ω
+_
R24Ω
C10.5F
L42Hv1
i4
State equation
x
•
=x1'
x2'
!
"#
$
%& =
dv1
dt
di4
dt
!
"
####
$
%
&&&&
=
'1
2'2
1
2'3
!
"
####
$
%
&&&&
v1
i4
!
"#
$
%& = Ax x 0( ) =
2
1
!
"#
$
%&
Unified Engineering: MATLAB Tutorials
Example: RCL Circuit (continued)
R3
6Ω
+_
R24Ω
C10.5F
L42Hv1
i4
Analytical solution
x t( ) = a1v
1e
!1t+ a
2v
2e
!2t"
v1
t( ) =4
3e
#t+
2
3e
#2.5t
u4
t( ) =1
3e
#t+
2
3e
#2.5t
x
•
= Ax
Avi= !
iv
i
a = v1
v2
"# $%&1
x 0( )x t( ) = a
i
i
' vie
!it
Unified Engineering: MATLAB Tutorials
RCL Circuit: MATLAB Solution
x
•
= Ax
x 0( )Av
i= !
i"
i
a = v1
v2
#$ %&'1
x 0( )x t( ) = a
i
i
( vie
!it
>> A = [a11 a12; a21 a22]
>> x0 = [v1,0; i4,0]
>> [V, L] = eig(A)
>> λ = diag(L)>> a = V \ x0>> v1 = a1*V11*exp(λ1*t)…
+ a2*V12*exp(λ2*t) i4 = a1*V21*exp(λ1*t)…
+ a2*V22*exp(λ2*t)
Unified Engineering: MATLAB Tutorials
Linear Systems Exercises Exercise 1: stateeig.m
Matrices & vectors Systems of linear equations Eigenvalues & eigenvectors
Follow instructions in m-file …
Questions?
Unified Engineering: MATLAB Tutorials
Complex Eigenvalues Example: complex eigenvalues and
eigenvectors from state equation
Euler’s Formula
Solution in terms of real variables
>> x1 =(cos(3*t)-sin(3*t))*exp(-t)
x t( ) ==0.5 + 0.5 j
0.5 ! j
"
#$
%
&'e
!1+3 j( )t+0.5 ! 0.5 j
0.5 + j
"
#$
%
&'e
!1!3 j( )t
e! +" j
= e! cos" + j sin"( )
x
1t( ) = ... = cos3t ! sin3t( )e!t
Unified Engineering: MATLAB Tutorials
State Space State-space model of a linear system
X, u & Y: state, input & output vectorsA, B & C: state, input & output matricesD: usually zero (feedthrough) matrix
Transfer functions
>> [Num, Den] = ss2tf(A, B, C, D)
X
•
= AX + Bu
Y = CX +Du
H s( ) =Num s( )Den s( )
= C sI ! A( )!1
B = D
Unified Engineering: MATLAB Tutorials
Convolution Definition
Example: signals & systems
g t( )! u t( ) = g t " #( )"$
$
% u #( )d#
Gu(t) y(t) = u(t)*g(t)
Unified Engineering: MATLAB Tutorials
Polynomial Convolution Polynomials:
x(s) = s2 + 2s + 5; y(s) = 2s2 + s + 3>> x = [1 2 5]; y = [2 1 3]
Convolution and polynomial multiplication>> w = conv(x, y)>> w =
2 5 15 11 15
Deconvolution and polynomial division>> [y, r] = deconv(w, x)
Unified Engineering: MATLAB Tutorials
Example: RC Circuit
u(t) =e! t, t " 0
0, t < 0
Impulse response:
System response:
g(t) =e!1.5t, t " 0
0, t < 0
#$%
1Ω
u(t)+
_2Ω 1F y(t)
+
_
Input:
y(t) =2e
! t ! 2e!1.5t , t " 0
0, t < 0
#$%
Unified Engineering: MATLAB Tutorials
Example: RC Circuit (continued)
Unified Engineering: MATLAB Tutorials
RC Circuit: MATLAB Solution
0 ! t ! tmax
g(t) = e"t
u(t) = e#t
y (t) = g(t)$u(t)
>> t = [0 : dt : tmax]>> nt = length(t)>> g = exp(α*t)>> u = exp(β*t)>> y = conv(g, u)*dt>> y = y(1 : nt)>> plot(t, u, ‘b-’, … t, g, ‘g-’, … t, y, ‘r-’)
Unified Engineering: MATLAB Tutorials
Linear Systems Exercises Exercise 2: convolution.m
Matrices & vectors Linear systems of equations Convolution
Follow instructions in m-file …
Questions?