# Introduction to MATLAB

Violeta Ivanova, Ph.D.
Office for Educational Innovation & Technology

violeta@mit.edu http://web.mit.edu/violeta/www

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Topics
     

MATLAB Interface and Basics Calculus, Linear Algebra, ODEs Graphics and Visualization Basic Programming Programming Practice Statistics and Data Analysis

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Resources

Class materials
http://web.mit.edu/acmath/matlab/IAP2007  Previous session: InterfaceBasics <.zip, .tar>  This session: LinsysCalcODE <.zip, .tar>

Mathematical Tools at MIT web site
http://web.mit.edu/ist/topics/math

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

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
IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

MATLAB Calculus & ODEs
Integration & Differentiation Differential Equations ODE Solvers

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Integration

Polynomial integration ! p x + ... + p x + p dx = P x + ... + P
n 1 n n +1 n +1 1

n +1

x+C

Analytical solution

Built-in function polyint
>> p = [p1 p2 p3 …]; >> P = polyint(p); assumes C=0 >> P = polyint(p, k); assumes C=k

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

More Polynomial Integration

Area under a curve
n n "1 2 p x + p x ... + p x + pn x + pn +1dx 2 n "1 ! 1 a b

Built-in function polyval
>> p = [p1 p2 p3 …]; >> P = polyint(p) >> area = polyval(P, b) - … polyval(P, a)
IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Differentiation

Polynomial differentiation
d ( P1 x n + ... + Pn x + C ) = p1 x n !1 + ... + pn dx

Built-in function polyder
>> P = [P1 P2 … Pn C] >> p = polyder(P)

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Differential Equations

Ordinary Differential Equations (ODE)

y ' = f (t , y)

Differential-Algebraic Expressions (DAE)

M (t , y ) y ' = f (t , y )

MATLAB solvers for ODEs and DAEs
>> ode45; ode23; ode113; ode23s …

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

ODE and DAE Solvers

Syntax
>> [T,Y] = solver(odefun,tspan,Y0) solver: ode45, ode23, etc. odefun: function handle tspan: interval of integration vector Y0: vector of initial conditions [T, Y]: numerical solution in two vectors

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

ODE Example: Mars Lander

Entry, descent, landing (EDL) force equilibrium
Lander velocity: v Drag coefficient: k = 1.2 Lander mass: m = 150 kg Mars gravity: g = 3.688 m/s2

! ! F = ma
dv ( t ) m = mg ! kv 2 ( t ) dt
dv ( t ) k 2 = g ! v (t ) dt m

EDL velocity ODE
Velocity at t0=0: v0=67.056 m/s

Theoretical example courtesy of Ms. Shannon Cheng, MIT Department of Aeronautics & Astronautics.

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

ODE Example (continued)
 

Problem:

dv ( t ) k 2 = g ! v (t ) dt m

Solution: odeLV.m, MarsLander.m
tspan = [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
IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

PDE Solver

Partial Differential Equations (PDEs)

Elliptic and parabolic e.g. Laplace’s equation:

! 2V ! 2V + 2 =0 2 !x !y

Numerical PDE solver: pdepe

Syntax: >> solution = pdepe(m, pdefun, icfun, bcfun, xmesh, tspan)

Function handles: pdefun, icfun, bcfun
IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

MATLAB Linear Systems
Linear Equations & Systems Eigenvalues & Eigenvectors Linear Dynamic Networks

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

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)

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Systems of Linear Equations

Definition: a11 x1 + a12 x2 + ... + a1m xm = b1
a21 x1 + a22 x2 + ... + a2 m xm = b2 ... an1 x1 + an 2 x2 + ... + anm xm = bm

Matrix form: Ax = b
! a11 ... a1n \$ ! x1 \$ ! b1 \$ # ... ... ... & # ... & = # ... & # &# & # & # " an1 ... amn & %# " xm & % # "bm & %
IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

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
IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

State Equation

Ordinary Differential Equations

y ' = f (t , y )

Linear systems -> State Equation
! % " a11 ... a1n % " x1 % " x1 \$ ... ' = \$ ... ... ... ' \$ ... ' \$ ' \$ '\$ ' \$ !' # xn & \$ # an1 ... ann ' &\$ # xn ' &

x = Ax

Eigenvalues, λi, and eigenvectors, vi, of a square matrix A A vi = λi vi
IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Linear Dynamic Networks

Solution using eigenvalue method
    

Identify the network’s states ………………………………..

x

Identify initial conditions ………………………………….. x ( 0 ) Find the state equation ……………………………. x = Ax Find eigenvalues & eigenvectors ………… !i , " i The general solution is ………. x t = Solve for ai ……………. a = [ v1

()

! aiv ie
i

"i t

v2

... vn ] x ( 0 )
!1

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Example: RCL Circuit
R3 6Ω C1 0.5F + _ v1 R2 4Ω

i4

L4 2H

State equation
\$ '2 & ! v1 \$ & # & = Ax i '3 & " 4 % & %

i4 ( 0 ) = 1

dv1 1 = ! v1 ! 2i4 dt 2 di4 1 = v1 ! 3i4 dt 2 v4 ( 0 ) = 2

! dv1 \$ ! 1 ' • ! x1 ' \$ # dt & # 2 x =# &=# &=# " x2 ' % # di4 & # 1 # " dt & % # " 2
IAP 2007

!2 \$ x (0) = # & "1 %

Theoretical example courtesy of Prof. Stephen Hall, MIT Department of Aeronautics & Astronautics. Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Example: RCL Circuit (continued)
R3 6Ω C1 0.5F + _ v1 R2 4Ω

i4

L4 2H

x = Ax Av i = !iv i
&1

\$ a =" #v 1 v 2 % x 0 !t x t = ' aiv ie i

()

()

i

Analytical solution
x t = a1v 1e

()

!1t

+ a2v 2e

!2t

4 #t 2 #2.5t v1 t = e + e 3 3 " 1 #t 2 #2.5t u4 t = e + e 3 3

()

()

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

RCL Circuit: MATLAB Solution
x = Ax x 0

() ()

>> 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)

Av i = !i"i
% a =# \$v 1 v 2 & x 0 !t x t = ( aiv ie i
'1

()

i

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Linear Systems Exercises

Exercise One: stateeig.m
  

Matrices & vectors Systems of linear equations Eigenvalues & eigenvectors

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Complex Eigenvalues

Example: complex eigenvalues and eigenvectors from state equation
" 0.5 + 0.5 j % ( !1+ 3 j )t " 0.5 ! 0.5 j % ( !1! 3 j )t x ( t ) == \$ e +\$ e ' ' # 0.5 ! j & # 0.5 + j &

Euler’s Formula
e ! + " j = e ! cos " + j sin "

Solution in terms of real variables
x1 t = ... = cos3t ! sin3t e !t >> x1 =(cos(3*t)-sin(3*t))*exp(-t)
IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

(

)

()

(

)

Convolution

Definition

g (t ) ! u (t ) =

\$

"\$

% g (t " # )u (# ) d#
y(t) = u(t)*g(t)

Example: signals & systems u(t) G

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

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)

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Example: RC Circuit
1Ω + u(t) _ 2Ω + 1F y(t) _

Input:
e! t , t " 0 u (t ) = 0, t < 0

#e!1.5 t , t " 0 Impulse response: g(t ) = \$ % 0, t < 0

System response:

#2 e! t ! 2 e!1.5 t , t " 0 y(t ) = \$ 0, t < 0 %

Theoretical example courtesy of Prof. Stephen Hall, MIT Department of Aeronautics & Astronautics. IAP 2007 Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Example: RC Circuit (continued)

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

RC Circuit: MATLAB Solution
0 ! t ! t max

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-’)

IAP 2007

Introduction to MATLAB: Calculus, Linear Algebra, Differential Equations

Linear Systems Exercises

Exercise Two: convolution.m
 

Matrices & vectors Convolution