Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IAP2007/IntroMatlabLinsysCalcODE.pdf · IAP...
Transcript of Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IAP2007/IntroMatlabLinsysCalcODE.pdf · IAP...
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Introduction to MATLAB
Violeta Ivanova, Ph.D.Office for Educational Innovation & Technology
[email protected]://web.mit.edu/violeta/www
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Topics
MATLAB Interface and Basics Calculus, Linear Algebra, ODEs Graphics and Visualization Basic Programming Programming Practice Statistics and Data Analysis
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Resources
Class materialshttp://web.mit.edu/acmath/matlab/IAP2007 Previous session: InterfaceBasics <.zip, .tar> This session: LinsysCalcODE <.zip, .tar>
Mathematical Tools at MIT web sitehttp://web.mit.edu/ist/topics/math
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
MATLAB Toolboxes & Help MATLAB
+ Mathematics+ Matrices and Linear Algebra
+ Solving Linear Systems of Equations+ Inverses and Determinants Eigenvalues
+ Polynomials and Interpolation+ Convolution and Deconvolution
+ Differential Equations+ Initial Value Problems for ODEs and DAEs
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
MATLAB Calculus & ODEs
Integration & DifferentiationDifferential EquationsODE Solvers
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Integration
Polynomial integration
Analytical solution Built-in function polyint
>> p = [p1 p2 p3 …];
>> P = polyint(p); assumes C=0>> P = polyint(p, k); assumes C=k
p1! x
n+ ...+ pnx + pn+1dx = P1x
n+1+ ...+ Pn+1x + C
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
More Polynomial Integration
Area under a curve
Built-in function polyval>> p = [p1 p2 p3 …];
>> P = polyint(p) >> area = polyval(P, b) - …
polyval(P, a)
p1
a
b
! xn+ p
2xn"1...+ pn"1x
2+ pnx + pn+1dx
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Differentiation
Polynomial differentiation
Built-in function polyder>> P = [P1 P2 … Pn C]
>> p = polyder(P)
d
dx(P1xn+ ...+ Pnx + C) = p
1xn!1
+ ...+ pn
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Differential Equations
Ordinary Differential Equations (ODE)
Differential-Algebraic Expressions (DAE)
MATLAB solvers for ODEs and DAEs>> ode45; ode23; ode113; ode23s …
y ' = f (t, y)
M (t, y)y ' = f (t, y)
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
ODE and DAE Solvers Syntax
>> [T,Y] = solver(odefun,tspan,Y0)
solver: ode45, ode23, etc.odefun: function handletspan: interval of integration vectorY0: vector of initial conditions[T, Y]: numerical solution in two vectors
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
ODE Example: Mars Lander Entry, descent, landing
(EDL) force equilibriumLander velocity: vDrag coefficient: k = 1.2Lander mass: m = 150 kgMars gravity: g = 3.688 m/s2
EDL velocity ODEVelocity at t0=0: v0=67.056 m/s
mdv t( )
dt= mg ! kv
2t( )
dv t( )
dt= g !
k
mv2t( )
!F = m
!a
Theoretical example courtesy of Ms. Shannon Cheng, MIT Department of Aeronautics & Astronautics.
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
ODE Example (continued)
Problem:
Solution: odeLV.m, MarsLander.mtspan = [0 : 0.05 : 6];V0 = 67.056;
[t, V] = ode45(@odeLV, tspan, V0)-------------------------function DV = odeLV(t, V)
K = 1.2; M = 150; G = 3.688;DV = G - K/M * V^2
dv t( )
dt= g !
k
mv2t( )
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
PDE Solver Partial Differential Equations (PDEs)
Elliptic and parabolic
e.g. Laplace’s equation:
Numerical PDE solver: pdepe Syntax:
>> solution = pdepe(m, pdefun,icfun, bcfun, xmesh, tspan)
Function handles: pdefun, icfun, bcfun
!2V
!x2+!2V
!y2= 0
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
MATLAB Linear Systems
Linear Equations & SystemsEigenvalues & EigenvectorsLinear Dynamic Networks
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Useful 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)
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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
!
"
###
$
%
&&&
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
State Equation Ordinary Differential Equations
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
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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( )
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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
!
"#
$
%&
Theoretical example courtesy of Prof. Stephen Hall, MIT Department of Aeronautics & Astronautics.
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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)
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Linear Systems Exercises
Exercise One: stateeig.m Matrices & vectors Systems of linear equations Eigenvalues & eigenvectors
Follow instructions in m-file …
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Convolution Definition
Example: signals & systems
g t( )! u t( ) = g t " #( )"$
$
% u #( )d#
Gu(t) y(t) = u(t)*g(t)
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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)
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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
#$%
Theoretical example courtesy of Prof. Stephen Hall, MIT Department of Aeronautics & Astronautics.
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Example: RC Circuit (continued)
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
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-’)
Introduction to MATLAB: Calculus, Linear Algebra, Differential EquationsIAP 2007
Linear Systems Exercises
Exercise Two: convolution.m Matrices & vectors Convolution
Follow instructions in m-file …