0% found this document useful (0 votes)
143 views

Differential Equations in Matlab-II: Riddhi@civil - Iitb.ac - in

Uploaded by

Vinay Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

Differential Equations in Matlab-II: Riddhi@civil - Iitb.ac - in

Uploaded by

Vinay Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

CE 657

Differential Equations in Matlab-II

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:

Guess the value of x’(t) at x=0, let’s call it z.


Then apply any iterative methods for solving the initial value
problem between x=[0,  / 2 ]

Repeat until  ( z ) matches


the 2nd boundary condition

Estimate the value of x at t=  / 2 . Let’s call it  ( z )


Compare with the 2nd boundary condition.

Ultimately, we are trying to bring  ( z ) as close as


possible to  or bring  ( z ) −  to 0 9
Any root solving method (example: secant
method) can be used to solve

 ( 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

1I. FINITE DIFFERENCE


METHOD
15
Finite difference methods divide the entire range of the
independent variable into discrete segments
2
d x
2
= −x
dt

x(t = 0) = 1 x(t = ) = −3
2
Discretize ‘t’ into ‘n-1’ segments

to=a t1=a+h t2=a+2h t3=a+3h ti=a+ih tn=a+nh

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

Similarly, for the 2nd derivative


f (t + h) − 2 f (t ) + f (t − h) Central difference
f ''(t ) = 2
+ O ( h 2
)
h
17
Its easy to interpret the forward, backward, and central
differences using the approximation graph
f (t + h) − f (t )
f '(t ) =
h
f (t ) − f (t − h)
f '(t ) =
h
Slope being
approximated f (t + h) − f (t − h)
f '(t ) =
2h
Central difference
f(tn+1)
Forward difference
f(tn)
Backward difference
f(tn-1)
tn-1 tn tn+1

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

to=a t1=a+h t2=a+2h t3=a+3h ti=a+ih tn=a+nh

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

to=0 t1=h t2=2h t3=3h t4=4h t5=5h t6=6h

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

d2y • y: deflection at point x in the beam


dx 2 T wx( L − x) • x: measurement along beam’s length
− y=
  dy   2 3/2
EI 2 EI • E: modulus of elasticity
1 +    • I: moment of inertia
  dx   • L: total length of the beam
• w: uniform load
d2y T wx( L − x) • T: end tension
2
− y=
dx EI 2 EI
26
Example I1. Concentration of salt in a tank

Brine in at rate r1 and


concentration c1
Brine tank,
volume V Brine out at rate r2 and
concentration c

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

Cross sectional area of


tank = B

Water tank
h(t)

Water outflow from a hole at the bottom


from hole of area A

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 =?

Instead of finding ‘f’ as a function of a single independent variable, you


have to find the ‘f’ as a function of multiple independent variables
31
Examples of partial differential equations
1. Laplace’s equation  2u  2u  2u
+ 2 + 2 =0
x y
2
z
 u u u  p   2u  2u 
2. Navier-Stokes equations  +u +v  = − +  2 + 2 
 t x y  x  x y 
 v v v  p   2v  2v 
 +u +v  = − +  2 + 2 
 t y y  y  x y 
  
 = + +
x y z
Laplace operator  2
 2
 2
2 = 2 + 2 + 2
x y z
1. Laplace’s equation  2u = 0
 u 
2. Navier-Stokes equation   + u .u  = −p +  2u 32
 t 
Generalized representation of linear, second order
PDEs

u 2
u u
2
u u
2
A 2 +B +C 2 + D + E + Fu + G = 0
x xy 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

*Time-independent steady state processes


* * Time-dependent dissipative physical processes such as diffusion that are evolving towards steady state
* * * Time-dependent conservative physical processes such as convection that are not evolving towards
steady states
34
There are three types of boundary conditions
Example equation
boundary, B(R)
 2  2 Region, R
+ 2 =0
x 2
y
Boundary conditions:

Dirichlet Values of the solution


along the boundary
 ( x, y) = o ( x, y)
Values of the derivatives 
Neumann of the solution along the ( x, y ) = k ( x, y )
boundary n

Mixed, both values of  ( x, y) = o ( x, y )


Cauchy solution and derivatives

along the boundary
( x, y ) = k ( x, y )
n
35
SOLVING PARTIAL DIFFERENTIAL
EQUATIONS
36
There are two broad methods to solve partial
differential equations numerically

1. Finite difference method (FDM)


2. Finite element method (FEM)
3. Etc... (Finite volume method (FVM))

Today, we will discuss FDM


FEM will be covered by Dr. Rajagopal on November 7th

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

Solution: Values of the independent variable(s) at each grid point

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:

d2 f d2 f f i+1, j − 2 f i, j + f i−1, j + f i, j+1 − 2 fi, j + fi, j−1


2
+ 2 = 4→ 2
=4
dx dy h
f i+1, j − 4 f i, j + f i−1, j + fi, j+1 + fi, j−1
→ 2
=4
h
39
Evaluating the PDE at each grid point
At the 1st grid point, i=1,j=1:

f2,1 − 4 f1,1 + f0,1 + f1,2 + f1,0 = 4h2

At the 2nd grid point, i=2,j=1:

f3,1 − 4 f2,1 + f1,1 + f2,2 + f2,0 = 4h2

In this way, the value of x at different values of t can be determined.

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

Hyperbolic f f i,t+1 − f i,t  2 f f i+1,t − 2 f i,t + f i−1,t


= , 2 =
t k x h2
f 2  f
2
− =0 f i,t+1 − f i,t f − 2 f i,t + f i−1,t
t x 2
→ = 2 i+1,t

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

You might also like