Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views61 pages

Numerical Solutions MATLAB PPT 2

Uploaded by

Jayshri Huddar
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)
11 views61 pages

Numerical Solutions MATLAB PPT 2

Uploaded by

Jayshri Huddar
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/ 61

Numerical Solution of Boundary Value Problems

using MATLAB

Dr. B. Rushi Kumar

Professor & Assistant Director RAAC


Department of Mathematics
School of Advanced Sciences
VIT, Vellore
[email protected]

Dr. B. Rushi Kumar BVP Solutions in MATLAB 1 / 61


Overview

1 Introduction to Boundary Value Problems

2 Numerical Methods Overview

3 Shooting Method

4 MATLAB bvp4c Solver

5 Finite Difference Method

6 Advanced Applications

7 Method Comparison

8 Conclusion

Dr. B. Rushi Kumar BVP Solutions in MATLAB 2 / 61


Why Study Boundary Value Problems?

BVPs arise in a wide range of engineering, physics, and biological


systems.
Many real-world processes involve conditions at more than one
point.
Examples:
Heat conduction in a rod with fixed temperatures at both ends.
Beam deflection under applied loads.
Electrostatic potential in regions with boundary constraints.

Dr. B. Rushi Kumar BVP Solutions in MATLAB 3 / 61


Analytical vs. Numerical Methods

Analytical Methods Numerical Methods


Only possible for simple BVPs Applicable to complex, real-world
problems

Closed-form expressions Approximated, discrete values

Limited by assumptions Flexible and adaptive

Dr. B. Rushi Kumar BVP Solutions in MATLAB 4 / 61


Why Numerical Methods with MATLAB?

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

Dr. B. Rushi Kumar BVP Solutions in MATLAB 5 / 61


Real-World Applications

Mechanical Engineering: Stress analysis in structures


Electrical Engineering: Voltage distribution in circuits
Fluid Mechanics: Flow through porous media
Environmental Science: Groundwater contamination modeling
Biomedical Engineering: Drug diffusion in tissues

Dr. B. Rushi Kumar BVP Solutions in MATLAB 6 / 61


Goals of This Talk

Understand the nature and types of BVPs.


Explore finite difference methods and collocation techniques
Learn to implement and visualize BVPs in MATLAB.
Interpret numerical results and validate accuracy.

Dr. B. Rushi Kumar BVP Solutions in MATLAB 7 / 61


Introduction to BVPs

Definition: Boundary Value Problem (BVP)


A differential equation where conditions are specified at the boundaries of
the domain, rather than at a single initial point.

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

General Form of Second-Order BVP


y ′′ = f (x, y, y ′ ), a≤x≤b

B(y, y ) = 0, ∀x ∈ Γ ⊆ [a, b]

Dr. B. Rushi Kumar BVP Solutions in MATLAB 8 / 61


Classification and Types
By Equation Type:
Linear:
y ′′ + p(x)y ′ + q(x)y = r(x), Boundary Condition Types:
where p(x), q(x), r(x) are
continuous functions. Dirichlet: y(a) = α, y(b) = β
Nonlinear: Contains nonlinear Neumann:
terms in y and it’s derivatives y ′ (a) = α, y ′ (b) = β
Robin: α1 y(a) + α2 y ′ (a) = α3
By Homogeneity: Mixed: Combination of above
Homogeneous: r(x) = 0
Non-homogeneous: r(x) ̸= 0

Example
d2 T
Heat equation: dx2 = − kq , with T (0) = T1 , T (L) = T2
Linear, non-homogeneous, Dirichlet BCs

Dr. B. Rushi Kumar BVP Solutions in MATLAB 9 / 61


Boundary Value Problems - Examples

1 Dirichlet BC: y ′′ + 2y ′ + y = e−x with y(0) = 1, y(2) = 0


2 Neumann BC: y ′′ − 4y = −4x2 with y ′ (0) = 0, y ′ (1) = 2
3 Robin BC: y ′′ + y = sin(x) with y(0) + 2y ′ (0) = 1,
y(π) − y ′ (π) = 0
4 Mixed BC: y ′′ + 3y ′ + 2y = 6e−x with y(0) = 2, y ′ (1) + y(1) = 1

Dr. B. Rushi Kumar BVP Solutions in MATLAB 10 / 61


Why Numerical Methods?

Limitations of Analytical Solutions


Most real-world BVPs have no closed-form solutions
Complex geometries and boundary conditions
Nonlinear equations
Variable coefficients

Common Numerical Methods


1 Finite Difference Method (FDM)
Discretize domain into grid points
Replace derivatives with difference quotients
2 Shooting Method
Convert BVP to IVP
Iterate to satisfy boundary conditions
3 MATLAB Built-in Solvers
bvp4c, bvp5c - adaptive mesh refinement

Dr. B. Rushi Kumar BVP Solutions in MATLAB 11 / 61


Shooting Method

Basic Idea
Convert BVP to IVP by guessing missing initial condition, then iterate to
match boundary condition.

For BVP: y ′′ = f (x, y, y ′ ), y(a) = α, y(b) = β


Step 1: Guess y ′ (a) = s (shooting parameter)
Step 2: Solve IVP: y ′′ = f (x, y, y ′ ), y(a) = α, y ′ (a) = s
Step 3: Check if y(b) = β. If not, adjust s and repeat.
Step 4: Use root-finding algorithm to find s such that y(b) − β = 0
Disadvantages:
Advantages:
May not converge for stiff
Uses reliable IVP solvers
problems
Good for linear problems
Sensitive to initial guess

Dr. B. Rushi Kumar BVP Solutions in MATLAB 12 / 61


Shooting Method Example

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
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 13 / 61


 
1 % Find the correct shooting parameter
2 s_correct = fzero ( @shoot_func , 1) ; % Initial guess : s =1
3 % Solve with correct initial condition
4 [T , Y ] = ode45 ( @ (t , y ) [ y (2) ; -y (1) ] , [0 , pi /2] , [0;
s_correct ]) ;
5 % Plot solution
6 figure ;
7 plot (T , Y (: ,1) , ’b - ’ , ’ LineWidth ’ , 2) ;
8 xlabel ( ’t ’) ;
9 ylabel ( ’y ( t ) ’) ;
10 title ( ’ Solution using Shooting Method ’) ;
11 grid on ;
12 % Display results
13 fprintf ( ’ Correct shooting parameter s = %.6 f \ n ’ ,
s_correct ) ;
14 fprintf ( ’ Final value y ( pi /2) = %.6 f \ n ’ , Y ( end ,1) ) ;
15 % Analytical solution for comparison ( y = sin ( t ) )
16 t_analytical = linspace (0 , pi /2 , 100) ;
17 y_analytical = sin ( t_analytical ) ;
18 hold on ;
19 plot ( t_analytical , y_analytical , ’r - - ’ , ’ LineWidth ’ ,
1.5) ;
20 legend ( ’ Numerical ( Shooting ) ’ , ’ Analytical ( sin ( t ) ) ’ ,
’ Location ’ , ’ best ’) ;
21 end
 
Dr. B. Rushi Kumar BVP Solutions in MATLAB 14 / 61
Solution using Shooting Method
1

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

Figure: Comparison of analytical vs shooting method solutions

Dr. B. Rushi Kumar BVP Solutions in MATLAB 15 / 61


MATLAB Solver: bvp4c
Introduction to bvp4c
bvp4c is MATLAB’s adaptive solver for boundary value problems using:
Collocation method with cubic splines
Automatic mesh refinement
Error control
Required Components:
1 ODE Function: Defines the differential equation system
2 BC Function: Defines boundary conditions
3 Initial Solution Structure: Provides initial mesh and guess

Basic Syntax
sol = bvp4c(@odefun, @bcfun, solinit);

Note: Must convert higher-order ODEs to first-order system:


(
′′ ′ y1′ = y2
y = f (x, y, y ) →
y2′ = f (x, y1 , y2 )
Dr. B. Rushi Kumar BVP Solutions in MATLAB 16 / 61
bvp4c Structure and Components

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
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 17 / 61


3. Initial Solution Structure:
 
1 x = linspace (a , b , n ) ; % Initial mesh
2 yinit = [ in it ia l _g ue s s_ y ; i n i t i a l _ g u e s s _ y _ p r i m e ];
3 solinit = bvpinit (x , yinit ) ; % Create initial solution
structure
 
4. Solve and Extract:
 
1 sol = bvp4c ( @odefun , @bcfun , solinit ) ;
2 x_sol = sol . x ;
3 y_sol = sol . y (1 ,:) ; % Extract y values
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 18 / 61


Hands-on Example using bvp4c

Problem
y ′′ = −2y + x, y(0) = 0, y(1) = 1

Dr. B. Rushi Kumar BVP Solutions in MATLAB 19 / 61


 
1 function bvp_c omparis on ()
2 % Define ODE function : y ’ ’ = -2 y + x
3 function dydx = odefun (x , y )
4 dydx = [ y (2) ; -2* y (1) + x ];
5 end
6 % Define boundary conditions : y (0) = 0 , y (1) = 1
7 function res = bcfun ( ya , yb )
8 res = [ ya (1) ; yb (1) - 1];
9 end
10 % Numerical solution using bvp4c
11 x = linspace (0 , 1 , 10) ;
12 yinit = [0; 1]; % Initial guess : y = 0 , y ’ = 1
13 solinit = bvpinit (x , yinit ) ;
14 sol = bvp4c ( @odefun , @bcfun , solinit ) ;
15 % Analytical solution : y ( x ) = [1/(2* sin ( sqrt (2) ) ) ] *
sin ( sqrt (2) * x ) + x /2
16 x_analytical = linspace (0 , 1 , 100) ; % Finer grid for
smooth curve
17 c2 = 1 / (2 * sin ( sqrt (2) ) ) ; % Coefficient from
boundary conditions
18 y_analytical = c2 * sin ( sqrt (2) * x_analytical ) +
x_analytical / 2;
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 20 / 61


 
1 % Also compute analytical solution at numerical
solution points for error analysis
2 y _ a n a l y t i c a l _ a t _ s o l _ x = c2 * sin ( sqrt (2) * sol . x ) +
sol . x / 2;
3 % Plot comparison
4 plot ( sol .x , sol . y (1 ,:) , ’ro - ’ , ’ LineWidth ’ , 2 ,
’ MarkerSize ’ , 8 , ’ DisplayName ’ , ’ Numerical
( bvp4c ) ’) ;
5 hold on ;
6 plot ( x_analytical , y_analytical , ’b - ’ , ’ LineWidth ’ , 2 ,
’ DisplayName ’ , ’ Analytical ’) ;
7 xlabel ( ’x ’) ; ylabel ( ’y ( x ) ’) ;
8 title ( ’ Comparison : Numerical vs Analytical Solution ’) ;
9 legend ( ’ Location ’ , ’ best ’) ; grid on ;
10 % Calculate and display error
11 error = abs ( sol . y (1 ,:) - y _ a n a l y t i c a l _ a t _ s o l _ x ) ;
12 max_error = max ( error ) ;
13 % Display results
14 fprintf ( ’\ n === BVP Solution Comparison ===\ n ’) ;
15 fprintf ( ’ ODE : y ’ ’ ’ ’ = -2 y + x \ n ’) ;
16 fprintf ( ’ Boundary conditions : y (0) = 0 , y (1) = 1\ n \ n ’) ;
17 fprintf ( ’ Analytical solution : y ( x ) = %.6 f * sin (%.6 f *
x ) + 0.5 * x \ n ’ , c2 , sqrt (2) ) ;
18 fprintf ( ’ Maximum absolute error : %.2 e \ n ’ , max_error ) ;
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 21 / 61


 
1 % Display solution values at key points
2 fprintf ( ’\ nSolution values at key points :\ n ’) ;
3 fprintf ( ’x \ t \ tNumerical \ tAnalytical \ tError \ n ’) ;
4 fprintf ( ’ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\ n ’) ;
5 for i = 1: length ( sol . x )
6 fprintf ( ’ %.3 f \ t \ t %.6 f \ t %.6 f \ t %.2 e \ n ’ , sol . x ( i ) ,
sol . y (1 , i ) , y _ a n a l y t i c a l _ a t _ s o l _ x ( i ) , error ( i ) ) ;
7 end
8 % Verify boundary conditions
9 fprintf ( ’\ nBoundary condition verification :\ n ’) ;
10 fprintf ( ’y (0) = %.6 f ( should be 0) \ n ’ , y_analytical (1) ) ;
11 fprintf ( ’y (1) = %.6 f ( should be 1) \ n ’ ,
y_analytical ( end ) ) ;
12 % Plot error
13 figure ;
14 plot ( sol .x , error , ’r - o ’ , ’ LineWidth ’ , 2 , ’ MarkerSize ’ ,
6) ;
15 xlabel ( ’x ’) ; ylabel ( ’ Absolute Error | y_ { numerical } -
y_ { analytical }| ’) ;
16 title ( ’ Error between Numerical and Analytical
Solutions ’) ;
17 grid on ;
18 % Display analytical derivative for completeness
19 fprintf ( ’\ nAnalytical derivative : y ’ ’( x ) = %.6 f * %.6 f
* cos (%.6 f * x ) + 0.5\ n ’ , c2 , sqrt (2) , sqrt (2) ) ;
20 end
 
Dr. B. Rushi Kumar BVP Solutions in MATLAB 22 / 61
Comparison: Numerical vs Analytical Solution
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

Figure: Comparison of analytical vs bvp4c solutions

Dr. B. Rushi Kumar BVP Solutions in MATLAB 23 / 61


10-6 Error between Numerical and Analytical Solutions
3

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

Figure: Absolute error between bvp4c and analytical solutions

Dr. B. Rushi Kumar BVP Solutions in MATLAB 24 / 61


Finite Difference method

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

Dr. B. Rushi Kumar BVP Solutions in MATLAB 25 / 61


Heat Equation

∂u ∂2u
= α 2 , a < x < b, t > 0, α ̸= 0,
∂t ∂x
u(x, 0) = f (x), u(a, t) = a1 , u(b, t) = b1 .

The dependent variable u (temperature) is a function of x (space) and t


(time) and α is a constant known as thermal diffusivity.

Dr. B. Rushi Kumar BVP Solutions in MATLAB 26 / 61


Discrete Grid Points
𝑡

𝑛=4

𝑛=3

𝑛=2

𝑛=1
Δt
𝑛=0 𝑥
𝑖=0 𝑖=1 Δ𝑥 𝑖 =𝑀−1 𝑖 =𝑀

∆x represents the spatial step size


∆t represents the time step size
Dr. B. Rushi Kumar BVP Solutions in MATLAB 27 / 61
Finite linear combinations

Principle: derivatives in the differential equation are approximated by


finite linear combinations of function values at the grid points

uni = u(a + i∆x, n∆t), i = 0, 1, . . . , M, n = 0, 1, 2, . . .


b−a
with mesh size ∆x = M .

Dr. B. Rushi Kumar BVP Solutions in MATLAB 28 / 61


Approximation of first-order derivatives

! !
(∆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

Accuracy of finite difference approximations


!n !n
un n
∂2u (∆x)2 ∂3u
n
i+1 − ui

∂u (∆x)
T1 ⇒ = − − + ...
∂x i ∆x 2 ∂x2 6 ∂x3
i i

forward difference truncation error O(∆x)


!n !n
un n
∂2u (∆x)2 ∂3u
n
i − ui−1

∂u (∆x)
T2 ⇒ = + − + ...
∂x i ∆x 2 ∂x2 6 ∂x3
i i

backward difference truncation error O(∆x)


!n
un un (∆x)2 ∂3u
n


∂u i+1 i−1
T1 − T2 ⇒ = − + ...
∂x i 2∆x 6 ∂x3
i

central difference truncation error O((∆x)2 )


Dr. B. Rushi Kumar BVP Solutions in MATLAB 29 / 61
Approximation of Second-order Derivatives

Central difference scheme


 2 n
∂ u uni−1 − 2uni + uni+1
T1 + T2 ⇒ = + O((∆x)2 )
∂x2 i (∆x)2

Dr. B. Rushi Kumar BVP Solutions in MATLAB 30 / 61


Finite Difference Scheme

∂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)

Dr. B. Rushi Kumar BVP Solutions in MATLAB 31 / 61


Theta Scheme

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θ

Dr. B. Rushi Kumar BVP Solutions in MATLAB 32 / 61


Consistency and Convergence

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.

Dr. B. Rushi Kumar BVP Solutions in MATLAB 33 / 61


Theta Scheme Simplification

" 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

Dr. B. Rushi Kumar BVP Solutions in MATLAB 34 / 61


Matrix Formulation

Linear system for the central difference scheme

n+1 n+1 n+1 n n n


i=1 pu0 + (1 − 2p)u1 + pu2 = qu0 + (1 − 2q)u1 + qu2
n+1 n+1 n+1 n n n
i=2 pu1 + (1 − 2p)u2 + pu3 = qu1 + (1 − 2q)u2 + qu3
.
.
.
n+1 n+1 n+1 n n n
i=M −2 puM −3 + (1 − 2p)uM −2 + puM −1 = quM −3 + (1 − 2q)uM −2 + quM −1
n+1 n+1 n+1 n n n
i=M −1 puM −2 + (1 − 2p)uM −1 + puM = quM −2 + (1 − 2q)uM −1 + quM

Matrix form Aun+1 = Bun + F A, B ∈ R(N −1)×(N −1) u, F ∈ RN −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

The matrix A is tridiagonal and symmetric positive definite ⇒ invertible.

Dr. B. Rushi Kumar BVP Solutions in MATLAB 35 / 61


Matrix Form (continued)

 
1 − 2q q
 p
 1 − 2q q 

B=
 .. .. .. 
 . . . 

 q 1 − 2q q 
q 1 − 2q

Dr. B. Rushi Kumar BVP Solutions in MATLAB 36 / 61


Example

∂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

Dr. B. Rushi Kumar BVP Solutions in MATLAB 37 / 61


Matlab Code (Part 1)

 
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
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 38 / 61


Matlab Code (Part 2)

 
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
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 39 / 61


Matlab Code (Part 3)

 
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 ) , ‘* - ’)
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 40 / 61


Analytic vs Numerical

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.

Dr. B. Rushi Kumar BVP Solutions in MATLAB 41 / 61


Error Analysis and Convergence

Finite Difference Error


Truncation Error: O(h2 ) for central differences

∥e∥∞ = max |y(xi ) − yi | = O(h2 )


1≤i≤n

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

Dr. B. Rushi Kumar BVP Solutions in MATLAB 42 / 61


Practical Guidelines
Always perform convergence studies for critical applications
Monitor solution changes with mesh refinement
Use adaptive solvers (bvp4c) for automatic error control

Dr. B. Rushi Kumar BVP Solutions in MATLAB 43 / 61


Systems of BVPs

Coupled System Example

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
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 44 / 61


 
1 % Solve system
2 x = linspace (0 , 1 , 100) ;
3 yinit = [0; 1; 0; 1];
4 solinit = bvpinit (x , yinit ) ;
5 sol = bvp4c ( @odefun , @bcfun , solinit ) ;
6
7 % Plot u ( x ) in first figure
8 figure (1) ;
9 plot ( sol .x , sol . y (1 ,:) , ’b - ’ , ’ LineWidth ’ , 2) ;
10 xlabel ( ’x ’) ;
11 ylabel ( ’u ( x ) ’) ;
12 title ( ’ Solution u ( x ) ’) ;
13 grid on ;
14
15 % Plot v ( x ) in second figure
16 figure (2) ;
17 plot ( sol .x , sol . y (3 ,:) , ’r - ’ , ’ LineWidth ’ , 2) ;
18 xlabel ( ’x ’) ;
19 ylabel ( ’v ( x ) ’) ;
20 title ( ’ Solution v ( x ) ’) ;
21 grid on ;
22 end
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 45 / 61


Solution u(x)
1

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

Figure: Solution of u(x)

Dr. B. Rushi Kumar BVP Solutions in MATLAB 46 / 61


10-3 Solution v(x)
0.5

-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

Figure: Solution of v(x)

Dr. B. Rushi Kumar BVP Solutions in MATLAB 47 / 61


BVPs in Engineering Applications
Heat Transfer in a Rod
Governing Equation:
d2 T hP q ′′′
− (T − T∞ ) = −
dx2 kA k
where:
T (x) = temperature distribution
h = convection coefficient, P = perimeter
k = thermal conductivity, A = cross-sectional area
q ′′′ = heat generation rate

Heating a Rod

Dr. B. Rushi Kumar BVP Solutions in MATLAB 48 / 61


Fluid Mechanics: Poiseuille Flow
Velocity Profile in Pipe:
 
1 d du 1 dp
r =
r dr dr µ dx
du
Boundary Conditions: u(R) = 0 (no-slip), dr (0) = 0 (symmetry)

Structural Mechanics: Beam Deflection


Euler-Bernoulli Beam:
d4 w
EI = q(x)
dx4

Dr. B. Rushi Kumar BVP Solutions in MATLAB 49 / 61


Application Example: Heat Transfer

Rod with Heat Generation


d2 T q0
− m2 (T − T∞ ) = − , T (0) = T0 , T (L) = TL
dx2 k
hP
where m2 = kA

Dr. B. Rushi Kumar BVP Solutions in MATLAB 50 / 61


Application Example: Heat Transfer

 
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 ) ;
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 51 / 61


 
1 % Analytical solution : T = T_inf + C1 * exp ( mx ) +
C2 * exp ( - mx ) + q0 /( k * m ^2)
2 q_term = q0_over_k / ( m ^2) ;
3 A = [1 , 1; exp ( m * L ) , exp ( - m * L ) ];
4 b = [ T0 - T_inf - q_term ; TL - T_inf - q_term ];
5 C = A \ b;
6 x_anal = linspace (0 , L , 200) ;
7 T_anal = T_inf + C (1) * exp ( m * x_anal ) +
C (2) * exp ( - m * x_anal ) + q_term ;
8 % Plot comparison
9 plot ( x_anal *1000 , T_anal , ’b - ’ , ’ LineWidth ’ , 2 ,
’ DisplayName ’ , ’ Analytical ’) ;
10 hold on ;
11 plot ( sol . x *1000 , sol . y (1 ,:) , ’ ro ’ , ’ MarkerSize ’ , 5 ,
’ DisplayName ’ , ’ Numerical ’) ;
12 xlabel ( ’ Position ’) ; ylabel ( ’ Temperature ’) ;
13 title ( ’ Temperature Distribution in Rod with Heat
Generation ’) ;
14 legend ; grid on ;
15 % Display error
16 T_num_interp = interp1 ( sol .x , sol . y (1 ,:) , x_anal ) ;
17 max_error = max ( abs ( T_anal - T_num_interp ) ) ;
18 fprintf ( ’ Max error : %.2 e \ n ’ , max_error ) ;
19 end
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 52 / 61


Temperature Distribution in Rod with Heat Generation
100
Analytical
Numerical
90

80
Temperature (°C)

70

60

50

40
0 10 20 30 40 50 60 70 80 90 100
Position (mm)

Figure: Temperature distribution in rod

Dr. B. Rushi Kumar BVP Solutions in MATLAB 53 / 61


Solving Nonlinear BVPs

Van der Pol Boundary Value Problem


y ′′ − µ(1 − y 2 )y ′ + y = 0, y(−1) = A, y(1) = B

Dr. B. Rushi Kumar BVP Solutions in MATLAB 54 / 61


Solving Nonlinear BVPs

 
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
 

Dr. B. Rushi Kumar BVP Solutions in MATLAB 55 / 61


Van der Pol BVP, =2
1

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

Figure: Solution of Van der Pol boundary value problem

Dr. B. Rushi Kumar BVP Solutions in MATLAB 56 / 61


Comparison of Methods

Method Advantages Disadvantages


Finite Differ-
ence Simple implementation Fixed grid spacing
Fast for linear problems Lower accuracy at boundaries
Direct matrix solution Memory intensive for fine grids

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

Dr. B. Rushi Kumar BVP Solutions in MATLAB 57 / 61


Conclusion & Recommendations

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

When to Use Each Method


Educational purposes: Start with Finite Difference
Research/Industrial: Use bvp4c for reliability
Simple linear problems: Shooting method can be effective
Complex systems: Always use bvp4c or bvp5c

Dr. B. Rushi Kumar BVP Solutions in MATLAB 58 / 61


Tips for Stable Solutions
Always check boundary condition formulation
Provide reasonable initial guesses
Use dimensional analysis to scale problems
Validate with known analytical solutions when possible
Check mesh convergence for critical applications

Dr. B. Rushi Kumar BVP Solutions in MATLAB 59 / 61


References

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

Dr. B. Rushi Kumar BVP Solutions in MATLAB 60 / 61


Questions & Discussion
Thank you for your attention!

Dr. B. Rushi Kumar BVP Solutions in MATLAB 61 / 61

You might also like