Numerical Solutions MATLAB PPT 2
Numerical Solutions MATLAB PPT 2
using MATLAB
3 Shooting Method
6 Advanced Applications
7 Method Comparison
8 Conclusion
MATLAB provides:
Built-in solvers (e.g., bvp4c, bvp5c)
Visualization tools for better insight
Ease of coding and prototyping
Useful for:
Research simulations
Engineering design
Academic learning
Physical Contexts:
BVP vs IVP:
Heat conduction in rods
IVP: All conditions at x = x0
Beam deflection
BVP: Conditions at x = a and
Fluid flow
x=b
Electromagnetic fields
Example
d2 T
Heat equation: dx2 = − kq , with T (0) = T1 , T (L) = T2
Linear, non-homogeneous, Dirichlet BCs
Basic Idea
Convert BVP to IVP by guessing missing initial condition, then iterate to
match boundary condition.
Problem
y ′′ + y = 0, y(0) = 0, y(π/2) = 1
1 function s h o o t i n g _e x a m p l e ()
2 % Shooting method to solve : y ’ ’ + y = 0
3 % Boundary conditions : y (0) = 0 , y ( pi /2) = 1
4 % Convert to system : y1 = y , y2 = y ’
5 % So : y1 ’ = y2 , y2 ’ = - y1
6
7 % Define the shooting function
8 function residual = shoot_func ( s )
9 % Solve IVP with initial conditions [ y (0) , y ’(0) ] =
[0 , s ]
10 [~ , Y ] = ode45 ( @ (t , y ) [ y (2) ; -y (1) ] , [0 , pi /2] , [0;
s ]) ;
11 residual = Y ( end ,1) - 1; % y ( pi /2) should equal 1
12 end
0.9
0.8
0.7
0.6
Numerical (Shooting)
y(t)
0.5
Analytical (sin(t))
0.4
0.3
0.2
0.1
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6
t
Basic Syntax
sol = bvp4c(@odefun, @bcfun, solinit);
1. ODE Function:
1 function dydx = odefun (x , y )
2 % y (1) = y , y (2) = y ’
3 dydx = [ y (2) ; f (x , y (1) , y (2) ) ];
4 end
2. Boundary Condition Function:
1 function res = bcfun ( ya , yb )
2 % ya = [ y ( a ) ; y ’( a ) ] , yb = [ y ( b ) ; y ’( b ) ]
3 res = [ ya (1) - alpha ; yb (1) - beta ];
4 end
Problem
y ′′ = −2y + x, y(0) = 0, y(1) = 1
0.9
0.8
0.7
0.6
Numerical (bvp4c)
y(x)
0.5
Analytical
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
2.5
Absolute Error |y numerical - yanalytical |
1.5
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
Procedure
1 Discretizing the continuous physical domain into a discrete finite-
difference grid.
2 Approximating the individual exact derivatives in the differential equa-
tion by algebraic finite difference approximations (FDAs)
3 Substituting the FDAs into the differential equation to obtain an al-
gebraic finite difference equation (FDE).
4 Solving the resulting algebraic FDEs
∂u ∂2u
= α 2 , a < x < b, t > 0, α ̸= 0,
∂t ∂x
u(x, 0) = f (x), u(a, t) = a1 , u(b, t) = b1 .
𝑛=4
𝑛=3
𝑛=2
𝑛=1
Δt
𝑛=0 𝑥
𝑖=0 𝑖=1 Δ𝑥 𝑖 =𝑀−1 𝑖 =𝑀
! !
(∆x)2 ∂2u (∆x)3 ∂3u
n
n n ∂u
T1 : ui+1 = ui + ∆x + + + ...
∂x i 2 ∂x2 6 ∂x3
i i
! !
(∆x)2 ∂2u (∆x)3 ∂3u
n
n n ∂u
T2 : ui−1 = ui − ∆x + − + ...
∂x i 2 ∂x2 6 ∂x3
i i
∂u ∂2u
=α 2
∂t ∂x
un+1
n
− un ui−1 − 2un n
i + ui+1
i i
1 Forward Time Centered Space (FTCS): =α
∆t (∆x)2
2 Backward Time Centered
" n+1 Space (BTCS): #
un+1
i − uni
ui−1 − 2un+1
i + un+1
i+1
=α 2
∆t (∆x)
3 Crank–Nicolson (CN):
" n+1 n+1
+ un+1
#
un+1 − un α ui−1 − 2ui α un n n
i−1 − 2ui + ui+1
i i i+1
= +
∆t 2 (∆x)2 2 (∆x)2
4 Theta (0 ≤ θ ≤ 1):"
un+1 n+1
+ un+1
#
un+1
n
− un i−1 − 2ui ui−1 − 2un n
i + ui+1
i i i+1
= αθ 2
+ α(1 − θ) 2
∆t (∆x) (∆x)
when θ = 0, it is FTCS.
when θ = 1, it is BTCS.
when θ = 21 , it is Crank-Nicolson method.
1
For 2 ≤ θ ≤ 1, scheme is unconditionally stable.
If 0 ≤ θ < 21 , then the scheme is stable if
α∆t 1
r := <
(∆x)2 2 − 4θ
Consistency
A finite difference representation of a partial differential equation (PDE)
is said to be consistent if we can show that the difference between the
PDE and its finite difference (FDE) representation vanishes as the mesh is
refined, i.e,
lim (P DE − F DE) = 0
∆x→0,∆t→0
Convergence
The solutions converge means that the solution obtained using the finite
difference method approaches the true solution as the steps ∆x and ∆t
approach zero.
" n+1
ui−1 − 2un+1 + un+1
#
un+1
n
− un ui−1 − 2un n
i + ui+1
i i i i+1
= αθ + α(1 − θ)
∆t (∆x)2 (∆x)2
h i
un+1 − un n+1 n+1
+ un+1 n n n
i i = rθ ui−1 − 2ui i+1 + r(1 − θ) ui−1 − 2ui + ui+1
n+1
− rθui−1 + (1 + 2rθ)un+1
i − rθun+1 n n
i+1 = r(1 − θ)ui−1 + (1 − 2r(1 − θ))ui
+ r(1 − θ)un
i+1
pun+1
i−1 + (1 − 2p)un+1
i + pun+1 n n n
i+1 = qui−1 + (1 − 2q)ui + qui+1
qu0 − pun+1
n
1 − 2p p u1
0
p 1 − 2p p u2 0
. .
A=
.. .. .. , u = . , F =
.
. . . . .
p 1 − 2p p u
M −2
0
n+1
p 1 − 2p uM −1 qun
M − puM
1 − 2q q
p
1 − 2q q
B=
.. .. ..
. . .
q 1 − 2q q
q 1 − 2q
∂u ∂2u
= 4 2 , 0 < x < 2, t > 0
∂t ∂x
πx
u(x, 0) = 6 sin , 0 < x < 2;
2
u(0, t) = 0, t > 0;
u(2, t) = 0, t > 0;
Exact solution:
πx 2
u(x, t) = 6 sin e−π t
2
1 clc
2 clear
3 close all
4 a = 0;
5 b = 2;
6 alpha = 4; % Thermal Diffusitivity
7 a1 = 0; b1 = 0; % Boundary Condition
8 M = 20; % No of grid points
9 dx = (b - a ) / M ; % Delta x
10 x = [ a : dx : b ]; % x grid points
11 theta = 1/2; % theta
12 dt = 0.01; % delta t % dt =(( dx ) ^2) /(2* alpha *(2 -4* theta ) ) ;
13 r = alpha * dt /(( dx ) ^2) ;
14 N = 1/ dt ; % no of time steps
1
2 % Set up tridiagonal matrix
3 p = -r * theta ;
4 q = r *(1 - theta ) ;
5 A = zeros (M -1 , M -1) ;
6 B = zeros (M -1 , M -1) ;
7 for i =1: M -1
8 A (i , i ) = 1 -2* p ;
9 B (i , i ) = 1 -2* q ;
10 end
11 for i =1: M -2
12 A (i , i +1) = p ;
13 B (i , i +1) = q ;
14 end
15 for i =2: M -1
16 A (i ,i -1) = p ;
17 B (i ,i -1) = q ;
18 end
1 % set initial condition
2 U = zeros ( M +1 , N +1) ;
3 U (: ,1) = 6* sin ( pi * x /2) ;
4 F = zeros (M -1 ,1) ;
5 % Main time stepping loop
6 for n =1: N
7 F (1) = q * U (1 , n ) -p * U (1 , n +1) ;
8 F (M -1) = q * U ( M +1 , n ) -p * U ( M +1 , n +1) ;
9 U (2: M , n +1) = A \( B * U (2: M , n ) + F ) ;
10 end
11 % plot the solution
12 plot (x , U (: , N +1) )
13 hold on
14 % plot exact solution
15 plot (x ,6* sin ( pi * x /2) * exp (( - pi ^2) * dt * N ) , ‘* - ’)
10-4
3.5
Numerical solution
3 Exact solution
2.5
2
f(x)
1.5
0.5
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x
The comparison between numerical and exact solutions.
Convergence Testing
Grid Convergence Study:
1 Solve with mesh size h
2 Solve with mesh size h/2
|yh/2 −yh |
3 Compare solutions: Error ≈ 2p −1
4 Expected order: p = 2 for FDM, p = 4 for bvp4c
u′′ = v + x
v ′′ = −u + sin(x)
u(0) = 0, u(1) = 1
v(0) = 0, v(1) = 0
1 function coupled_bvp ()
2 % State vector : [u , u ’ , v , v ’]
3 function dydx = odefun (x , y )
4 dydx = [ y (2) ; y (3) + x ; y (4) ; -y (1) + sin ( x ) ];
5 end
6
7 % Boundary conditions
8 function res = bcfun ( ya , yb )
9 res = [ ya (1) ; yb (1) - 1; ya (3) ; yb (3) ];
10 end
0.9
0.8
0.7
0.6
u(x)
0.5
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
-0.5
-1
v(x)
-1.5
-2
-2.5
-3
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
Heating a Rod
1 function h e a t _ t r a n s f e r _ b v p ()
2 % Parameters
3 m = 2; T_inf = 25; q0_over_k = 1000; T0 = 100; TL = 50;
L = 0.1;
4 % ODE system : d ^2 T / dx ^2 - m ^2( T - T_inf ) = - q0 / k
5 function dTdx = odefun (x , T )
6 dTdx = [ T (2) ; m ^2*( T (1) - T_inf ) - q0_over_k ];
7 end
8 % Boundary conditions : T (0) = T0 , T ( L ) = TL
9 function res = bcfun ( Ta , Tb )
10 res = [ Ta (1) - T0 ; Tb (1) - TL ];
11 end
12 % Numerical solution
13 x = linspace (0 , L , 50) ;
14 solinit = bvpinit (x , [75; 0]) ;
15 sol = bvp4c ( @odefun , @bcfun , solinit ) ;
80
Temperature (°C)
70
60
50
40
0 10 20 30 40 50 60 70 80 90 100
Position (mm)
1 function v a n_ de r_ p ol _b vp ()
2 mu = 2; A = -1; B = 1;
3 % ODE system : y1 ’ = y2 , y2 ’ = mu *(1 - y1 ^2) * y2 - y1
4 ode = @ (x , y ) [ y (2) ; mu *(1 - y (1) ^2) * y (2) - y (1) ];
5 % Boundary conditions : y1 ( -1) = A , y1 (1) = B
6 bc = @ ( ya , yb ) [ ya (1) - A ; yb (1) - B ];
7 % Better initial guess
8 x = linspace ( -1 , 1 , 50) ;
9 y_init = @ ( x ) [ A + (B - A ) *( x +1) /2; (B - A ) /2];
10 solinit = bvpinit (x , y_init ) ;
11 % Solve with options for difficult problems
12 options = bvpset ( ’ RelTol ’ , 1e -6 , ’ AbsTol ’ , 1e -8) ;
13 sol = bvp4c ( ode , bc , solinit , options ) ;
14 % Plot
15 plot ( sol .x , sol . y (1 ,:) , ’b - ’ , ’ LineWidth ’ , 2) ;
16 xlabel ( ’x ’) ; ylabel ( ’y ( x ) ’) ;
17 title ([ ’ Van der Pol BVP , \ mu = ’ , num2str ( mu ) ]) ;
18 grid on ;
19 end
0.5
0
y(x)
-0.5
-1
-1.5
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x
Shooting
Method Uses proven IVP solvers May diverge for stiff problems
Good for linear BVPs Sensitive to initial guess
Adaptive step size Requires root finding
bvp4c
Robust and reliable Requires proper formulation
Adaptive mesh May need good initial guess
refinement Black box approach
Handles nonlinear
problems
Error control
Summary of Techniques
Finite Difference: Best for simple, linear problems with regular
domains
Shooting Method: Good when familiar with IVP solvers, linear
BVPs
bvp4c: Most versatile, handles nonlinear and complex BVPs
Recommended Textbooks
Burden, R.L. & Faires, J.D. (2019). Numerical Analysis, 10th Ed.
Chapra, S.C. & Canale, R.P. (2015). Numerical Methods for
Engineers, 7th Ed.
Ascher, U.M. & Petzold, L.R. (1998). Computer Methods for ODEs
and DAEs
Shampine, L.F., Gladwell, I. & Thompson, S. (2003). Solving ODEs
with MATLAB