You are on page 1of 27

DEPARTMENT OF BIOTECHNOLOGY ENGINEERING NASSERELDEEN AHMED KABBASHI nasreldin@iium.edu.my, Tel : Ext.

4524 Sem I, 2011-2012 MATLAB for

DE Matlab (BTE 4415)


BTE 4415

Solution of Differential Equations with MATLAB


MATLAB has some powerful features for solving differential equations of all types. We will explore some of these features for the CCLODE forms. The approach here will be that of the Symbolic Math Toolbox. The result will be the form of the function and it may be readily plotted with MATLAB.

BTE 4415 (TF Matlab)

Dr. Nassereldeen A. Kabashi

BTE 4415

Symbolic Differential Equation Terms


y dy dt 2 d y 2 dt n d y n dt

y
Dy

BTE 4415 (TF Matlab)

D2y

Dny
BTE 4415

Dr. Nassereldeen A. Kabashi

Representative CCLODE Form


d y dy b2 2 b1 b0 y A sin at dt dt y(0) C1 and y '(0) C2
>> y = dsolve(b2*D2y+b1*D1y+b0*y=A*sin(a*t), y(0)=C1, Dy(0)=C2)

BTE 4415 (TF Matlab)

>> ezplot(y, [t1 t2])


Dr. Nassereldeen A. Kabashi BTE 4415

Example 1. Solve DE below with MATLAB.


BTE 4415 (TF Matlab)

dy 2 y 12 dt

y(0) 10

>> y = dsolve('Dy + 2*y = 12', 'y(0)=10') y = 6+4*exp(-2*t)

>> ezplot(y, [0 3]) >> axis([0 3 0 10])

Dr. Nassereldeen A. Kabashi BTE 4415

BTE 4415 (TF Matlab)

Dr. Nassereldeen A. Kabashi

BTE 4415

Example 2. Solve DE below with MATLAB.


BTE 4415 (TF Matlab)

dy 2 y 12sin 4t dt

y(0) 10

>> y = dsolve('Dy + 2*y = 12*sin(4*t)', 'y(0)=10') y = -12/5*cos(4*t)+6/5*sin(4*t)+62/5*exp(-2*t)

>> ezplot(y, [0 8]) >> axis([0 8 -3 10])

Dr. Nassereldeen A. Kabashi BTE 4415

BTE 4415 (TF Matlab)

Dr. Nassereldeen A. Kabashi

BTE 4415

Example 3. Solve DE below with MATLAB.


BTE 4415 (TF Matlab)

d y dy 3 2 y 24 2 dt dt y(0) 10 y '(0) 0
>> y = dsolve('D2y + 3*Dy + 2*y = 24', 'y(0)=10', 'Dy(0)=0') y = 12+2*exp(-2*t)-4*exp(-t)

>> ezplot(y, [0 6])


Dr. Nassereldeen A. Kabashi BTE 4415

BTE 4415 (TF Matlab)

Dr. Nassereldeen A. Kabashi

BTE 4415

Example 4. Solve DE below with MATLAB.


d y dy 2 5 y 20 2 dt dt
BTE 4415 (TF Matlab)

y(0) 0

y '(0) 10

>> y = dsolve('D2y + 2*Dy + 5*y = 20', 'y(0) = 0', 'Dy(0) = 10') y = 4+3*exp(-t)*sin(2*t)-4*exp(-t)*cos(2*t)

>>ezplot(y, [0 5]}
Dr. Nassereldeen A. Kabashi BTE 4415

BTE 4415 (TF Matlab)

Dr. Nassereldeen A. Kabashi

BTE 4415

Symbolic Laplace Transform


Establish t and s as symbolic variables. >> syms t s The time function f is then formed and the Laplace transform command is >> F = laplace(f) Some useful simplifications are >> pretty(F) >> simplify(F)

Dr. Nassereldeen A. Kabashi BTE 4415

BTE 4415 (TF Matlab)

Symbolic Inverse Laplace Transform


Establish t and s as symbolic variables. >> syms t s The Laplace function F is then formed and the inverse Laplace transform command is >> f = ilaplace(F)

BTE 4415 (TF Matlab)

The simplification operations may also be useful for inverse transforms.

Dr. Nassereldeen A. Kabashi BTE 4415

Example 5. Determine the Laplace transform of f(t)=5t with MATLAB.


BTE 4415 (TF Matlab)

>>syms t s >> f = 5*t f = 5*t

>> F = laplace(f) F = 5/s^2

Dr. Nassereldeen A. Kabashi BTE 4415

Example 6. Determine the Laplace transform of the function below using MATLAB.

BTE 4415 (TF Matlab)

v(t ) 3e

2t

sin 5t 4e

2t

cos5t

>> syms t s >> v = 3*exp(-2*t)*sin(5*t) + 4*exp(-2*t)*cos(5*t) v = 3*exp(-2*t)*sin(5*t)+4*exp(-2*t)*cos(5*t)

Dr. Nassereldeen A. Kabashi

BTE 4415

Example 6. Continuation.
>> V = laplace(v) V = 15/((s+2)^2+25)+4*(s+2)/((s+2)^2+25)

BTE 4415 (TF Matlab)

>> V=simplify(V) V = (23+4*s)/(s^2+4*s+29)


Dr. Nassereldeen A. Kabashi

BTE 4415

Example 7. Determine the inverse transform of the function below using MATLAB.

BTE 4415 (TF Matlab)

100( s 3) F (s) 2 ( s 1)( s 2)( s 2s 5)

>> syms t s

>> F=100*(s+3)/((s+1)*(s+2)*(s^2+2*s+5)) F = (100*s+300)/(s+1)/(s+2)/(s^2+2*s+5)

Dr. Nassereldeen A. Kabashi BTE 4415

Example 7. Continuation.

>> f = ilaplace(F)

BTE 4415 (TF Matlab)

f= 50*exp(-t)-20*exp(-2*t)-30*exp(-t)*cos(2*t)-10*exp(t)*sin(2*t)

>> pretty(f) 50 exp(-t) - 20 exp(-2 t) - 30 exp(-t) cos(2 t) - 10 exp(-t) sin(2 t)

Dr. Nassereldeen A. Kabashi

BTE 4415

Example 8. Determine the inverse transform of the function below using MATLAB.

BTE 4415 (TF Matlab)

10 48 Y ( s) 2 s 2 ( s 2)( s 16)
>> syms t s >> Y = 10/(s+2) + 48/((s+2)*(s^2+16)) Y = 10/(s+2)+48/(s+2)/(s^2+16)

Dr. Nassereldeen A. Kabashi

BTE 4415

Example 8. Continuation.
>> y = ilaplace(Y) y = 62/5*exp(-2*t)12/5*cos(16^(1/2)*t)+3/10*16^(1/2)*sin(16^(1/2)*t)

BTE 4415 (TF Matlab)

>> y=simplify(y) y = 62/5*exp(-2*t)-12/5*cos(4*t)+6/5*sin(4*t)

Dr. Nassereldeen A. Kabashi

BTE 4415

Optimization

For a function f(x), we might want to know


BTE 4415 (TF Matlab)

at what value of x is f smallest largest? equal to some value?

All of these are optimization problems Matlab has several functions to perform optimization

Same problem as with ODE: solver needs to work with YOUR functions Must pass function handle

Dr. Nassereldeen A. Kabashi

BTE 4415

Optimization

Simplest functions are


x=fzero(@fun,

BTE 4415 (TF Matlab)

x0);%Finds f(x)==0 starting from x0 x=fminbnd(@fun, [xL, xR]); %Finds minimum in interval [xL,xR] x=fminsearch(@fun,x0); %Finds minimum starting at x0

All of these functions require you to write a function implementing f:


function

f=fun(x);%computes f(x)

Dr. Nassereldeen A. Kabashi

BTE 4415

Optimization Toolbox

More optimization techniques, but used in the same way


BTE 4415 (TF Matlab)

Some of these functions can make use of gradient information in higher dimensions, gradient tells you which way to go, can speed up procedure

df/dx = 0
Dr. Nassereldeen A. Kabashi BTE 4415

Optimization Toolbox

To return gradient information, your function must be polymorphic:


x=fun(x)

BTE 4415 (TF Matlab)

%return value [x,dx]=fun(x); %return value and gradient


function

[x,dx]=fun(x);

if(nargout>1)

dx=

end
x=

Dr. Nassereldeen A. Kabashi

BTE 4415

Optimization Options

Need to tell optimization toolbox that it can use gradient


Use

BTE 4415 (TF Matlab)

optimset:

opts=optimest(ParameterName,value); Then

pass opts to optimization function Lots of options--read docs!


opts=optimset(GradObj,on);%Turns

gradient on x=fminunc(@fun, x0,opts);%pass opts to function

Dr. Nassereldeen A. Kabashi

BTE 4415

Summary

ODE solvers & optimization routines in Matlab are function functions


You

BTE 4415 (TF Matlab)

must write a function returning

dx/dt

(ODE) f (Optimization)
You

pass the name of the function to the solver Solver calls it repeatedly and returns answer

Dr. Nassereldeen A. Kabashi

BTE 4415

You might also like