Differential Equations in Matlab-II: Riddhi@civil - Iitb.ac - in
Differential Equations in Matlab-II: Riddhi@civil - Iitb.ac - in
Lecture 13
Riddhi Singh
Email: [email protected]
Differential equations outline
• Lecture 1: Introduction to differential
equations, Initial value problems
• Lecture IIa: Boundary value problems,
Applications of ODEs
• Lecture IIb: Partial differential equations
2
BOUNDARY VALUE PROBLEMS
3
A typical boundary value problem
2
d x dx
2
= f (t , x (t ), )
dt dt
x(t = a ) = x(t = b) =
Why cant we use the methods for initial value problem to solve
this type of equation?
4
Solution strategies for boundary value problems
i. Shooting method
ii. Finite difference method
5
I. SHOOTING METHOD
6
An example:
2
d x
2
= − x
dt
x(t = 0) = 1 x(t = ) = −3
2
=1 = −3
7
Equivalent system of ODEs:
2
d x
2
= −x x(t = 0) = 1 x(t = ) = −3
dt 2
dx
x1 = x, x2 =
dt
x1 x2 x1 1
X = , X ' = , X t =0 = =
x2 − x1 x2 z
8
The shooting method:
( z) − = 0
z3 − z2 z2 − z1
=
− ( z2 ) ( z2 ) − ( z1 )
z2 − z1
→ z3 = z2 + [ − ( z2 )]
( z ) − ( z1
)
( z2 ) 2
zn − zn −1
→ zn +1 = zn + [ − ( zn )]
( z1 ) ( z n ) − ( z n −1
)
z1 z2 z3
10
Shooting method algorithm*
1. Start with a guess x’(t=α)
2. While (error < tolerable_error), do:
a. Solve the initial value problem for the interval [a,b] for two
different values of z (zn-1 and zn):
d 2x
2
= f (t , x, x ')
dt
x(t = a ) = x '(t = b) = z
b. Compute ϕ(zn-1) and ϕ(zn)
c. Compute zn+1 using the following formula:
zn − zn −1
zn +1 = zn + [ − ( zn )]
( z n ) − ( z n −1
)
d. Estimate ϕ(zn+1)
e. Estimate error = ϕ(zn+1)- β
*Limitations:
1. Will be very time consuming as we are solving an initial value 11
problem at every iteration
Shooting method in MATLAB
function [fx] = shooting(a,b,fa,fb,h,derivative_handle)
%%some definitions and file description
%initializations for x, z, fxz, error, itr, etc.
%perform the shooting method
while (error<tolerable_error)
%solve initial value problem
z(3) = z(2)+(fb-dxz(2))*(z(2)-z(1))/(dxz(2)-dxz(1));
fx = taylor1(a,b,h,[fa z(3)],derivative_handle);
dxz(3) = fx(lengthx,1);
error = dxz(3)-fb;
%re-assign z-values for the next iteration
z([1 2]) = z([2 3]);
dxz([1 2])= dxz([2 3]);
%tract iteration count
iterations= iterations+1
end
12
Shooting method in MATLAB using ODE45
function [fx] = shooting(a,b,fa,fb,h,derivative_handle)
%%some definitions and file description
%initializations for x, z, fxz, error, itr, etc.
%perform the shooting method
while (error<tolerable_error)
%solve initial value problem
z(3) = z(2)+(fb-dxz(2))*(z(2)-z(1))/(dxz(2)-dxz(1));
[dx sol] = ode45(@(dx,dfx) derivative_handle(dx,dfx),
[a b], [fa z(3)]);
fx = interp1(dx,sol,x);
dxz(3) = fx(lengthx,1);
error = dxz(3)-fb;
%re-assign z-values for the next iteration
z([1 2]) = z([2 3]);
dxz([1 2])= dxz([2 3]);
%tract iteration count
iterations= iterations+1
end
13
A typical output:
Choices made:
1. Value of ‘h’, the
grid size
2. Initial values of z
14
0.5
-0.5
2
2
0
0
-2 -2
If , 0 i n
b−a
ti = a + ih, with, h = 16
n
Then, Taylor series can be used to derive the
approximations to the derivatives:
h2 h2
f (t + h) = f (t ) + f '(t )h + f ''(t ) + ... f (t − h) = f (t ) − f '(t )h + f ''(t ) + ...
2 OR 2
f (t + h) − f (t ) f (t ) − f (t − h)
→ f '(t ) = − O ( h) → f '(t ) = − O ( h)
h h
Forward difference Backward difference
f (t + h) − f (t − h)
OR f '(t ) = + O(h 2 ) Central difference
2h
18
After discretization, the equation becomes:
2
d x
2
= −x , x(t = 0) = 1 x(t = ) = −3
dt 2
Discretize ‘t’ into ‘n-1’ segments
d 2 x 2 x xi+1 − 2 xi + xi −1
2 = 1 i n −1
dt 2
t h 2
xi+1 − 2 xi + xi −1
→ 2
= − xi , xo = 1, xn = −3
h
19
In this example, this simplifies to a linear system of
equations
/ 2−0
Let , n = 6, h =
6
xi+1 − 2 xi + xi −1 = − h 2 xi
x2 − 2 x1 + xo = −h 2 x1 → x2 − 2 x1 + 1 = − h 2 x1
x3 − 2 x2 + x1 = −h 2 x2
x4 − 2 x3 + x2 = −h 2 x3
x5 − 2 x4 + x3 = −h 2 x4
x6 − 2 x5 + x4 = −h 2 x5 → −3 − 2 x5 + x4 = −h 2 x5
20
The resulting system of equations can be solved using
matrix algebra methods
1
( x2 − 2 x1 + 1) = − x1
h2
1
( x3 − 2 x2 + x1 ) = − x2
h2
1
( x4 − 2 x3 + x2 ) = − x3
h2
1
( x5 − 2 x4 + x3 ) = − x4
h2
1
(−3 − 2 x5 + x4 ) = − x5
h2
−2 1 0 0 0 1 0 0 0 0 x1 0 1
1 −2 1 0
1 0 0 1 0
0 0 x2 0 0
2 1
0 1 −2 1 0 + 0 0 1
0 0 x3 = 0 − 2 0
h h
0 0 1 −2 1 0 0 0 1 0 x4 0 0
0 0 0 1 − 2 0 0 0 0 1 x5 0 −3
21
Typical solution
1
Analytical Solution
0.5 Finite diffrence method with h = 0.2618
-0.5
x[-]
-1
-1.5
-2
-2.5
-3
0 0.5 1 1.5
t[-]
22
Generalizing the linear case
−2 1 0 0 0 1 0 0 0 0 x1 0 1
1 −2 1 0
1 0 0 1 0 0 0 x2 0 0
2 1
0 1 −2 1 0 + 0 0 1 0 0
x3 = 0 − 2 0
h h
0 0 1 −2 1 0 0 0 1 0 x4 0 0
0 0 0 1 − 2 0 0 0 0 1 x5 0 −3
A B X D C
• A: matrix resulting from discretizing the derivative
• B: matrix containing coefficients of the unknowns on R.H.S beyond those that appear
during discretization
• C: vector with boundary conditions, all 0 elements except those at boundaries
• D: vector with any other terms depending on the independent variable (t in this case)
• X: unknown dependent variable to be solved for
23
Finite difference in MATLAB
function [fx] = fdlinear(xbegin, xend, n, fxbegin, fxend)
%%some definitions and file description
%initializations for X, A, B, C, D, etc.
h = (xend-xbegin)/n;
X = xbegin:h:xend;
lnx = length(x);
A = toeplitz([-2 1 0 0 0]);
B = eye(lnx-2);
D = zeros(lnx-2,1);
C = zeros(lnx-2,1);
C(1) = fxbegin;
C(lnx-2) = fxend;
%solve the linear algebraic equations:
fx = ((1/h/h)*A+B)\(D-(1/h/h)*C);
%append the boundary conditions to the solution
fx = [fxbegin ;fx ;fxend];
24
APPLICATIONS OF ODES
25
Example 1. Simply supported beam with tension
w
T T
dy y
= r1c1 − r2
dt V
y: amount of salt in the tank at any time ‘t’
27
Example I1I. Outflow of water through a hole
Water tank
h(t)
dh A
= 0.600 2 gh(t )
dt B
28
PARTIAL DIFFERENTIAL EQUATIONS
IN MATLAB ®
29
Partial derivatives appear when there are multiple
independent variables
−1 x 1
f = x + y + xy
2 2
f (0,0) = 0, f (1,1) = 3
f f f (1,0) = 1, f (0,1) = 1
= 2x + y, = 2y + x
x y
f
=?
x x =0
f
=?
y y =1
30
To solve a partial differential equation is to find the
function ‘f’ given the information on the derivatives:
f f
2 2
+ 2 =4
x 2
y
f =?
u 2
u u
2
u u
2
A 2 +B +C 2 + D + E + Fu + G = 0
x xy y x y
with A,B,..,G =constant
Elliptical if B 2 − 4 AC 0
This equation is Parabolic if B − 4 AC = 0
2
Hyperbolic if B 2 − 4 AC 0
33
Elliptical equations generally represent steady-state while
hyperbolic/parabolic equations are time dependent
Elliptical* u u
2 2
(Ex: Poisson, + 2 = f ( x, y ) u = f ( x, y)
Laplace) x y
2
Parabolic**
u 2u
Ex: diffusion, − 2 = 0 u = f ( x, t )
heat or decay t x
Hyperbolic*** 2u 2
u
Ex: wave − 2
=0 u = f ( x, t )
t 2
x 2
37
Finite difference method (FDM)
Basic principle: Divide the region spanned by independent variables into grids
Approximate the partial derivative at each point on the grid
Example: 2 f 2 f
+ 2 = 4, 0 x 1
x 2
y
f (0,0) = 0,..., f (1,1) = 3 y
f (1,0) = 1,..., f (0,1) = 1
Elliptical/Hyperbolic/Parabolic? x
Approximate the derivative
Boundary conditions
38
Using Taylor series to approximate a partial derivative:
d2 f f i+1, j − 2 f i, j + f i−1, j
2
=
dx h2
i,j
d2 f f i, j+1 − 2i, j + f i, j−1
2
=
dy h2
Combining the approximations of 2 partial derivatives at the grid point
(i,j), we get:
40
Converting to matrix form:
f1,1 f1,0 + f0,1
−4 1 0 1 0 0 0 0 0 4
f 2,1 f 2,0
1 − 4 1 0 1 0 0 0 0 4
0 1 −4 0 0 1 0 0 0 3,1 4
f f
3,0 + f 4,1
f
1 0 0 − 4 1 0 1 0 0
1,2 4
f 0,2
1 1
0 1 0 1 − 4 1 0 1 0 f = 4 − 0
h
2
h
2,2 2
0 0 1 0 1 − 4 0 0 1 f3,2
4 f 4,2
0 0 0 1 0 0 − 4 1 0 4 f + f
1,3
f 0,3 1,4
0 0 0 0 1 0 1 − 4 1 4 f
f
2,3 4 2,4
0 0 0 0 0 1 0 1 − 4 f f 4,3 + f3,4
3,3
A B D C
A – Matrix approximating the derivative
B – unknown vector
D – Derivative vector
41
C – Boundary condition
Method I. Finite difference method*
1. Divide the region into grids based on a chosen value
of ‘h’
2. Formulate the matrix approximating the derivative,
the derivative vector, and the boundary condition
vector
3. Solve the linear system of equations
4. Plot f vs. x and y (3 dimension plots or contours or
surface plots)
*Limitations:
1. Both variables are forced to have the same grid spacing
(might be taken care of by scaling)
42
Finite difference for PDEs in MATLAB
[fx error] = fdpde(xbound, ybound, yend, numsegs,
derivative_handle, function_handle)
%%some definitions and file description
%initializations for X, A, B, C, D, etc.
I = speye(numsegs+1);
e = ones(3,1);
A = spdiags([e, -4*e,e],
[-1,0,1],numsegs+1,numsegs+1);
J = spdiags([e,e],[-1,1],numsegs+1,numsegs+1);
Lh = kron(I,A) + kron(J,I);
A = full(Lh);
D = ones((numsegs+1)*(numsegs+1),1)*4;
C = [fx(2,1)+fx(1,2), fx(3,1), fx(4,1)+fx(5,2),
fx(1,3), 0, fx(5,3), fx(1,4)+fx(2,5), fx(3,5),
fx(5,4)+fx(4,5)]';
%solve the linear system
fxvec = ((1/h/h)*A)\(D-(1/h/h)*C);
43
A typical output:
Analytical
Choices made:
1. Value of ‘h’, the grid
size
2. Order of the
Approximated approximated Taylor
series
44
Finite difference formulas for hyperbolic/parabolic PDEs
k h2
2 2 k 2k
h h
(
→ f i,t+1 = 1− 2 f i,t + 2 f i+1,t + f i−1,t )
0
Parabolic: 2 f f i,t+1 − 2 f i,t + f i,t−1 2 f f i+1,t − 2 f i,t + f i−1,t
= , 2 =
t 2
k 2
x h2
2 f 2 f
2
− =0 f i,t+1 − 2 f i,t + f i,t−1 f − 2 f i,t + f i−1,t
t 2
x 2
→ 2
= 2 i+1,t
k h2
2k 2 2k 2
h h
(
→ f i,t+1 = 2 1− 2 f i,t + 2 f i+1,t + f i−1,t − f i,t−1 )
45