Академический Документы
Профессиональный Документы
Культура Документы
3
Differential Equations with MATLAB
y y
dy Dy
dt
2
d y
2 D2y
dt
n
d y
n Dny
dt
5
Finding Solutions to Differential Equations
6
Approach to Solving
Initial Value Problems
• Given an nth order time domain differential equation of x(t)
with initial initial conditions for x(t) and its n-1
derivatives
1. Take the Laplace Transform of the differential equation to
get a function of X(s)
2. Substitute Initial Conditions
3. Write equation for desired function
4. Convert X(s) to be strictly rational and use Partial Fraction
Expansion
5. Use the table method to get the Inverse Laplace Transform
thus getting x(t) 7
2nd Order DE Example
•• • •
y (t ) + 6 y (t ) + 8 y (t ) = x (t ) + x (t )
Subject to the Conditions:
•
y (0) = 3 y(0) = 1 x(t ) = u (t )
8
Left Side Laplace Transform
•• •
y (t ) + 6 y (t ) + 8 y (t )
⇓ LT ⇓ LT ⇓ LT
•
s Y ( s) − sy (0) − y (0) + 6{sY ( s) − y (0)} + 8Y ( s)
2
9
Right Side Laplace Transform
[ ]
Simplifying terms yields:
1
Y ( s) s + 6s + 8 − s − 9 = 1 +
2
12
s
Equation for Y(s)
[ ]
Y ( s) s + 6s + 8 − s − 9 = 1 +
2 1
s
1 1
1+ + s + 9 + s + 10
s s s + 10s + 1
2
Y (s) = 2 = =
s + 6s + 8 (s + 2)(s + 4) s(s + 2)(s + 4)
13
Partial Fraction Expansion
• Perform partial fraction expansion to break into terms with
known Inverse Laplace Transforms
s 2 + 10s + 1 A = sY ( s) s =0 =
1
Y (s) = 8
s( s + 2)( s + 4) 15
B = ( s + 2)Y ( s) s =−2 =
A B C 4
= + + 23
s s+2 s+4 C = ( s + 4)Y ( s) s =−4 = −
8
1 15 23
Y (s) = 8 + 4 + 8
s s+2 s+4 14
Inverse Laplace
y(t) is the inverse Laplace Transform of Y(s):
1 15 23
Y (s) = 8 − 4 − 8
s s+2 s+4
LT-1
1 15 − 2t 23 − 4t
y (t ) = + e − e u (t )
8 4 8
15
Exponential DE Example
• Find the solution to the following second order differential
equation with a causal exponential term:
•• •
−t
y (t ) − 3 y (t ) + 2 y (t ) = 2e u (t )
Subject to the initial conditions:
•
y (0) = 2 y ( 0) = 1
16
Take Laplace Transform
•• •
y (t ) − 3 y (t ) + 2 y (t ) = 2e −t u (t )
⇓ LT ⇓ LT ⇓ LT ⇓ LT
2 •
s Y ( s) − sy (0) − y (0) − 3{sY ( s ) − y (0)}
2
+ 2Y ( s ) =
( s + 1)
17
Equation for Y(s)
( s + 1)
Write an equation for Y(s)
2 1 2 + 2 s 2 + 2 s − 5s − 5
Y (s) = + 2s − 5 2 =
( s + 1) ( s − 3 s + 2 ) ( s + 1)( s 2
− 3s + 2)
18
Partial Fraction Expansion
51 16 51 51
x(t ) = e − 0.7 t
2 cos
t −
sin
t u (t )
10 51 10 20
Inverse Laplace of X2(s)
Use Matlab to determine Inverse Laplace of X2(s) 1
X 2 (s) = 2
s( s + 1.4s + 1)
» syms s
» X2=1/(s*(s^2+1.4*s+1));
» x2=ilaplace(X2)
x2 =
1-exp(-7/10*t)*cos(1/10*51^(1/2)*t-7/51*
exp(-7/10*t)*51^(1/2)*sin(1/10*51^(1/2)*t)
51 7 51 51
cos t + sin t u (t )
− 0.7 t
x2 (t ) = 1 − e
10 15 10
21
Solving a 1st Order DE
dy
Consider the differential equation: + 2 y = 12
dt
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =6+exp(-2*t)*C1
23
Verify Results
y (0) = 9 → C1 = 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
24
Solving a 2nd Order DE
» syms c y
» ys=dsolve('D2y = - c^2*y')
ys = C1*sin(c*t)+C2*cos (c*t)
y (t ) = C1 sin(ct ) + C2 cos(ct )
25
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
dx dy
= 3x + 4 y = −4 x + 3 y
dt dt
26
General Solution
dx dy
= 3x + 4 y = −4 x + 3 y
dt dt
27
Matlab Verification
Given the dx dy
equations: = 3x + 4 y = −4 x + 3 y
dt dt
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x = exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y = -exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
28
Initial Conditions
x(0) = 0 y (0) = 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x = exp(3*t)*sin(4*t) x = e sin( 4t )
3t
y = exp(3*t)*cos(4*t) y = e 3t cos(4t )
29
Non-Linear Differential Equation Example
dy
Solve the differential equation: = 4− y 2
dt
Subject to initial condition:
y (0) = 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
y=
y (t ) =
(
2 3e 4t − 1)
2*(3*exp(4*t)-1)/(1+3*exp(4*t))
1 + 3e 4t
30
Specifying the Independent Parameter of a
Differential Equation
If another independent variable, other than t, is used, it must
be introduced in the dsolve command
dy
Solve the differential equation: + 2 y = 12
dx
» y=dsolve('Dy+2*y=12','x')
−2 x
y = 6+exp(-2*x)*C1 y ( x) = 6 + C1e
31
Numerical Solution Example
Not all non-linear differential equations have a closed
form solution, but a numerical solution can be found
2
d y
Solve the differential equation:
2
+ 9 sin( y ) = 0
dt
y ( 0) = 1
Subject to initial conditions:
•
y ( 0) = 0
32
Rewrite Differential Equation
2
d y d 2 y ••
2
+ 9 sin( y ) = 0 2
= y = −9 sin( y )
dt dt
Rewrite in the
x1 = y • •
following form x2 = y = x1
• ••
x1 (0) = y (0) = 1
x2 = y = −9 sin( y )
• •
34
35
Definition of Laplace Transformation:
Here,
L = Laplace Transform Operator.
f(t) =determining function, depends on t
.
F(s)= Generating function, depends on s
.
36
The Laplace Transformation
Differential Algebraic
equations equations
Input Input
excitation e(t) excitation E(s)
Output Output
response r(t) response R(s)
37
Laplace Transforms with MATLAB
38
example for the function f(t)
>> syms t s
>> f=-1.25+3.5*t*exp(-2*t)+1.25*exp(-2*t);
>> F = laplace ( f , t , s)
F = -5/4/s+7/2/(s+2)^2+5/4/(s+2)
>> simplify(F)
ans = (s-5)/s/(s+2)^2
>> pretty (ans)
39
Inverse Laplace Transform
>> syms t s
>> F=(s-5)/(s*(s+2)^2);
>> ilaplace(F)
ans = -5/4+(7/2*t+5/4)*exp(-2*t)
>> simplify(ans)
ans = -5/4+7/2*t*exp(-2*t)+5/4*exp(-2*t)
>> pretty(ans)
- 5/4 + 7/2 t exp(-2 t) + 5/4 exp(-2 t)
40
Part 3: Direct Solution
• x(t) for f(t)=u(t) can also be solved
2s − 0.2 + 1
directly in Matlab to yield the same X ( s) = s
results s 2 + 1.4s + 1
» syms s
» X=(2*s-0.2+1/s)/(s^2+1.4*s+1);
» x=ilaplace(X)
x =
1+exp(-7/10*t)*cos(1/10*51^(1/2)*t)
-23/51*exp(-7/10*t)*51^(1/2)*sin(1/10*51^(1/2)*t)
− 0.7 t 51 23 51 51
x(t ) = 1 + e cos t− sin t u (t )
10 51 10
41
Summary
Equations with real poles can be solved efficiently by hand or using Matlab to verify portions
Matlab is useful for solving complex pole problems, but the solution may not be intuitively
interpreted
42
Reference
• http://www.mathworks.in/help/symbolic/simplify.html
• https://www.google.co.in/#q=laplace+transform+using+matlab
43
Thank You
44
Chapter Objectives
• Understanding how the Runge-Kutta Fehlberg
methods use RK methods of different orders to
provide error estimates that are used to adjust step
size.
• Familiarizing yourself with the built-in MATLAB
function for solving ODEs.
• Learning how to adjust options for MATLAB’s ODE
solvers.
• Learning how to pass parameters to MATLAB’s
ODE solvers.
• Understanding what is meant by stiffness and its
implications for solving ODEs.
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.
Symbolic Differential Equation Terms
y •y
dy •Dy
dt
2
d y
2
•D2y
dt
n
d y
n •Dny
dt
Adaptive Runge-Kutta Methods
• LMS and RK solvers are general algorithms that can work with any ODE
• Need a way to pass the function f to the solver
N0 t0 T N t
Solver f
53
... Solving first-order ODEs
Example
54
Output
55
Example
• Another example to solve a 2nd order ODE, y'' + y' + y + t =
0, the matlab code yprime1.m should be like,
• function yp=yprime1(y,t)
• yp(1) = y(2);
• yp(2) = -y(2) - y(1) -t;
• Solve it by
– tspan=[0,10];
– y0 = 0;
– [t,y] = ode23(@yprime1,tspan,y0)
• Plot the graph t vs the first and second column of y to show
the solution y and y’
– plot(t,y(*,1),y(*,2))
Purpose of function files
As indicated above, the function file generates the value of outputs every
time it called upon with certain sets of inputs of dependent and independent
variables
function dy=pbr (w, y)
For instance the pbr.m file generates the value of output (dy), every iteration
it is called upon with inputs of independent variable catalyst weight (w) and
dependent variables (y)
NOTE: For pbr.m file, the output dy is actually dX/dW and dP/dW;
The function pbr returns the numerical values of dy or
OUTPUTS for corresponding sets of INPUTS y defined
at given value of y
function dy=pbr(W,y)
global dtube
% operating conditions
To=300+273; %Inlet temperature
Po=1.5*101.325; %Inlet pressure in kPa
In our case,
OUTPUTS (dy) INPUTS
d(P)/dW y(2,:) = P
d(X)/dW y(1,:) = X
Function file name
function dy=pbr(W,y)
global dtube
% operating conditions
To=300+273; %Inlet temperature
Po=1.5*101.325; %Inlet pressure in kPa
Semi-colon suppresses the SCREEN
PRINTING of the statement
“To=300+273”
function dy=pbr(W,y)
global dtube
%operating conditions
To=300+273; %Inlet temperature
Po=1.5*101.325; %Inlet pressure in kPa
%defined variables
P=y(1,:);
X=y(2,:);
P=y(1,:);
X=y(2,:);
global dtube
dtube = 4e-4; %microreactor diameter 400 micron
Po=1.5*101325;
y0 = [Po 0]; %initial condition for P and X.
W0=0;
Wmax=3.1416*(dt/2)^2*(5e-2)*(1-0.3)*1400;% w=pi*r^2*L*(1-
porosity)*cat_density
Wspan=[W0 Wmax];% Defines integration range
hold on
plot(W,y(:,1)/Po,'b-.', W,y(:,2),'r-')
This statement defines the initial condition of the dependent variable
global dtube
dtube = 4e-4; %microreactor diameter 400 micron
Po=1.5*101325;
y0 = [Po 0]; %initial condition for P and X.
W0=0;
Wmax=3.1416*(dt/2)^2*(5e-2)*(1-0.3)*1400;% w=pi*r^2*L*(1-
porosity)*cat_density
Wspan=[W0 Wmax];% Defines integration range
hold on
plot(W,y(:,1)/Po,'b-.', W,y(:,2),'r-')
This statement defines the Limits of Integration over which
the Differential Equation must be INTEGRATED over.
global dtube
dtube = 4e-4; %microreactor diameter 400 micron
Po=1.5*101325;
y0 = [Po 0]; %initial condition for P and X.
W0=0;
Wmax=3.1416*(dt/2)^2*(5e-2)*(1-0.3)*1400;% w=pi*r^2*L*(1-
porosity)*cat_density
Wspan=[W0 Wmax];% Defines integration range
hold on
plot(W,y(:,1)/Po,'b-.', W,y(:,2),'r-')
This is the Main COMMAND.
An output in form of a matrix of W and y’s will be generated
by solving the differential equations defined/calculated by “pbr”
W0=0;
Wmax=3.1416*(dtube/2)^2*(5e-2)*(1-0.3)*1400;% w=pi*r^2*L*(1-
porosity)*cat_density
Wspan=[W0 Wmax];% Defines integration range
hold on
plot(W,y(:,1)/Po,'b-.', W,y(:,2),'r-')
Another useful command is
dy
+ 2y =
12 y (0) = 10
dt
•>> y = dsolve('Dy + 2*y = 12', 'y(0)=10')
•y =
•6+4*exp(-2*t)
dy y (0) = 10
+ 2y =
12sin 4t
dt
•>> 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)
2
d y dy
2
+ 3 + 2y = 24
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 5]}
Laplace transform
Syntax
• laplace(F)
• laplace(F,t)
• laplace(F,w,z)
• Description
• L = laplace(F) is the Laplace transform of the
scalar symbol F with default independent
variable t. The default return is a function of s.
The Laplace transform is applied to a function of
t and returns a function of s.
LT Matlab commands
The following Matlab commands are used in order to strengthen
the understanding of LT.
• syms
• laplace
• diff
• ilaplace
Inverse Laplace Transofrm Matlab Command
1 syms s
F (s) = 2 ilaplace(1/s^2)
s
Return
t
Laplace Matlab
Transofrm Command
syms t
f (t ) = t 4
f = t^4
4 laplace(f)
L[t ] Return
s24/s^5
LaplaceTransform[Sin[t],t,s]
L[sin(t )]
Laplace Transform MATLAB
Command
f(t) = t4 syms t
f = t^4
laplace(f)
returns
24/s^5
g(t)=sqrt(s) syms s
g = 1/sqrt(s)
laplace(g)
returns
pi^(1/2)/t^(1/2)
f(t) = e–at syms t a x
f = exp(-a*t)
laplace(f,x)
returns
1/(a + x)
Laplace Transform (LT)
g (t ) = t cos(bt )
l {t cos(bt )} ↔ ?
∂
G ( s) = − F (s)
ds Matlab Code:
∂ s
=− { 2 }; Re( s ) > 0
ds s + b 2
s 2 − b2
= ; Re( s ) > 0
(
s +b
2 2 2
)
Example
e − sb / a
• What is Laplace of t^3? f ( at − b)u( at − b) ↔ F (s / a)
a
– From the table: 3!/s^4 Re(s)>0
Time transformation
• Find the Laplace Transform:
g (t ) = sin(12t − π / 2)u (4t − π / 6)
g (t ) = sin{3(4t − π / 6)}u (4t − π / 6) = sin(3τ )u (τ ) τ = 4t −π / 6
Time _ Translation :
a = 4; b = π / 6
e − sπ / 24
G (s) = F ( s / a ); Note that without u(.) there will
4 be no time translation and thus,
3 the result will be different:
f (τ ) = sin(3τ )u (τ ) ↔ F ( s ) =
s2 + 9
e − sπ / 24 3
G (s) = ;
4 ( s / 4) + 9
2
Assume t>0
Inverse of LT
92
Transfer Function representation
Y (s ) 25
Given:
=
G (s ) = 2
U (s ) s + 4s + 25
Matlab function: tf
93
State Space representation
1 0 1
Given: =x& ,Ax + =
Bu A = B 0
−2 1
=y Cx + Du
C=[3 −2] D = [0 ]
Matlab function: ss
A = [1 0 ; -2 1];
B = [1 0]’;
C = [3 -2];
D = [0];
sys = ss(A,B,C,D)
94
THANK YOU
Prof Kabbashi
International Islamic University Malaysia
KOE/BTE Eng/BERC