11.3 Finite Difference Methods for Linear Problems

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Sec:11.

Finite-Difference
Methods for Linear
Problems
Sec:11.3 Finite-Difference Methods for Linear Problems

Finite difference method


The finite difference method (FDM) is a numerical technique used to
solve differential equations. The idea of the FDM is to replace the
derivatives in the differential equation with their finite difference
approximations (such as backward difference formula, forward
difference formula, centered difference formula, etc.), which leads to a
linear system of equations that can be solved either by direct or
iterative techniques.
Boundary value problem
The boundary value problem is a differential equations with conditions
specified at more than one point.

Example boundary-value problem

,
𝝅
𝑦 (𝟎)=− 0.3 , 𝑦 ( )=− 0.1
𝟐
Sec:11.3 Finite-Difference Methods for Linear Problems

Initial value problem


The initial value problem is a differential equations with conditions
specified at only one point.

Example
Initial-value problem
,

𝑦 (𝟎)=− 0.3 , 𝑦 ′ (𝟎)=− 0.1

In this section, we aim to solve the following linear second-order


boundary-value problem using finite difference method:

,
with and ,
Sec:11.3 Finite-Difference Methods for Linear Problems

Example boundary-value problem


The finite difference method
for the linear second-order ,
boundary-value problem
, 𝜋
𝑦 (0)=− 0.3 , 𝑦 ( )=− 0.1
2
with and ,

𝒑 ( 𝒙 )=𝟏
𝟏
𝒚 ( 𝒙 )=− ( 𝒔𝒊𝒏 ⁡𝒙 +𝟑 𝒄𝒐𝒔 ⁡𝒙 ).
𝟏𝟎
𝒒 ( 𝒙 ) =𝟐
𝒓 ( 𝒙 )=𝐜𝐨𝐬 𝒙

𝒂= 𝟎𝒃=𝝅 / 𝟐
𝜶=− 𝟎. 𝟑𝜷=− 𝟎 .𝟏
Sec:11.3 Finite-Difference Methods for Linear Problems

𝒙𝒊− 𝐡 𝒙𝒊 𝒙 𝒊 +𝐡

Section (4.1) 𝒙 𝒊 −𝟏 𝒙 𝒊+𝟏

Second Derivative Midpoint Formula


centered-difference formula for

𝒚 ( 𝒙¿¿ 𝒊 −𝟏) 𝒉𝟐 (𝟒) ′′


𝒚 ( 𝒙𝒊 )
′′
𝒚 ( 𝒙𝒊 ) =𝒚 ( 𝒙¿¿ 𝒊+𝟏)−𝟐 𝒚 (𝒙 𝒊)+ − 𝒚 ( 𝝃𝟏) ¿ ¿
𝒉
𝟐
𝟏𝟐

where

Three-Point Midpoint Formula


centered-difference formula for

′ 𝒚 (𝒙 ¿¿ 𝒊 − 𝟏) 𝒉𝟐 (𝟑)
′ 𝒚 ( 𝒙 𝒊 )= 𝒚 (𝒙¿ ¿ 𝒊+𝟏)− − 𝒚 ( 𝝃𝟐) ¿ ¿
𝒚 ( 𝒙𝒊) 𝟐𝒉 𝟔

where
Sec:11.3 Finite-Difference Methods for Linear Problems

Example 3 At the interior mesh points, xi, for i = 1:N,


the differential equation is approximated as
boundary-value problem
( centeral diff )
, 𝒚 ′ ′ (𝒙 𝒊)=𝒚 ′( 𝒙𝒊 )+𝟐 𝒚 (𝒙 ¿¿𝒊)+𝒄𝒐𝒔 ⁡𝒙𝒊 ¿ 𝒊=𝟏 : 𝟑
𝜋
+¿𝟐 𝒚(𝒙¿¿𝒊)¿+¿𝒄𝒐𝒔 ⁡𝒙
𝑦 (0)=−0.3 , 𝑦 ( )=− 0.1
2
¿ 𝒊=𝟏 : 𝟑
𝒊

1 we select an integer N

+¿𝒄𝒐𝒔 ⁡𝒙 𝒊
N=3
𝟐𝒉 +¿
≈𝒚 (𝒙 ¿¿ 𝒊+𝟏)− 𝒚 (𝒙¿¿𝒊−𝟏)
𝒚 (𝒙 ¿¿ 𝒊− 𝟏)
𝒚 (𝒙 ¿¿ 𝒊+𝟏)− 𝟐 𝒚 (𝒙 𝒊)+ ¿¿ ¿ ¿ 𝟐 𝒚(𝒙¿¿𝒊)¿
𝒉𝟐

2 divide the interval [a, b]


into (N+1) equal 𝒘 𝒊+ 𝟏 −𝟐 𝒘 𝒊+ 𝒘 𝒊 −𝟏 𝒘 𝒊 +𝟏 − 𝒘 𝒊 −𝟏
𝟐
= +𝟐 𝒘 𝒊+ 𝒄𝒐𝒔 𝒙 𝒊
subintervals 𝒉 𝟐𝒉

h=(𝑏 −𝑎 )/( 𝑁 +1) Multiply by and collect similar terms 𝒊=𝟏 : 𝟑


𝟏
( − 𝒘 𝒊 +𝟏+ 𝟐 𝒘 𝒊 − 𝒘 𝒊 −𝟏 ) = 𝟐 𝒉 ( −𝒘 𝒊+𝟏 +𝒘 𝒊 − 𝟏) − 𝟐 𝒉𝟐 𝒘 𝒊 −𝒉𝟐 𝒄𝒐𝒔 𝒙 𝒊
𝒉= 𝝅 /𝟖

( − 𝟏+
𝒉
𝟐 )
𝒘 𝒊+𝟏 + ( 𝟐+𝒉𝟐 ) 𝒘 𝒊 + −𝟏 −
𝒉
𝟐 (
𝒘 𝒊 −𝟏 =−𝒉𝟐 𝒄𝒐𝒔 𝒙 𝒊 )
𝒘 𝟎=−𝟎 . 𝟑 𝒘𝟏 𝒘𝟐 𝒘𝟑 𝒘 𝟒=−𝟎 . 𝟏
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒

𝟎 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐
𝝅 /𝟖
Sec:11.3 Finite-Difference Methods for Linear Problems

Example 𝒊=𝟏 : 𝟑
boundary-value problem ( − 𝟏+
𝒉
𝟐 ) 𝟐
(
𝒘 𝒊+𝟏 + ( 𝟐+𝒉 ) 𝒘 𝒊 + −𝟏 −
𝒉
𝟐 ) 𝟐
𝒘 𝒊 −𝟏 =−𝒉 𝒄𝒐𝒔 𝒙 𝒊
,
𝜋
𝑦 (0)=−0.3 , 𝑦 ( )=− 0.1 Our goal now is to write a linear system of
2

[ ]
equations:
𝑤1
1 we select an integer N
𝒘 = 𝑤2
N=3
𝑨 𝒘 =𝒃 𝑤3
2 divide the interval [a, b]
into (N+1) equal
subintervals ( 𝟐) 𝟐 𝟏 ( 𝟐)
𝒊=𝟏 → − 𝟏+ 𝒉 𝒘 + ( 𝟐+ 𝒉𝟐 ) 𝒘 + −𝟏 − 𝒉 𝒘 =−𝒉𝟐 𝒄𝒐𝒔 𝒙
𝟎 𝟏

h=(𝑏 −𝑎 )/( 𝑁 +1)


( 𝟐) 𝟑 𝟐 ( 𝟐)
𝒊=𝟐 → − 𝟏+ 𝒉 𝒘 + ( 𝟐+ 𝒉 ) 𝒘 + −𝟏 − 𝒉 𝒘 =−𝒉 𝒄𝒐𝒔 𝒙
𝟐
𝟏
𝟐
𝟐

𝒊=𝟑 → (− 𝟏+ ) 𝒘 + ( 𝟐+ 𝒉 ) 𝒘 + (− 𝟏− ) 𝒘 =−𝒉 𝒄𝒐𝒔 𝒙


𝒉 𝟐 𝒉 𝟐
𝒉= 𝝅 /𝟖 𝟐 𝟒 𝟑
𝟐 𝟐 𝟑

𝒘 𝟎=−𝟎 . 𝟑 𝒘𝟏 𝒘𝟐 𝒘𝟑 𝒘 𝟒=−𝟎 . 𝟏
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒

𝟎 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐
𝝅 /𝟖
Sec:11.3 Finite-Difference Methods for Linear Problems

Example 4 ( 𝟐) 𝟐 𝟏 ( 𝟐)
𝒊=𝟏 → − 𝟏+ 𝒉 𝒘 + ( 𝟐+ 𝒉𝟐 ) 𝒘 + −𝟏 − 𝒉 𝒘 =−𝒉𝟐 𝒄𝒐𝒔 𝒙
𝟎 𝟏

boundary-value problem

,
( 𝟐) 𝟑 ( 𝟐)
𝒊=𝟐 → − 𝟏+ 𝒉 𝒘 + ( 𝟐+ 𝒉 ) 𝒘 + −𝟏 − 𝒉 𝒘 =−𝒉 𝒄𝒐𝒔 𝒙
𝟐
𝟐 𝟏
𝟐
𝟐

𝒊=𝟑 → (− 𝟏+ 𝟐 ) 𝒘 + ( 𝟐+ 𝒉 ) 𝒘 + (− 𝟏− 𝟐 ) 𝒘 =−𝒉 𝒄𝒐𝒔 𝒙


𝒉 𝒉 𝟐 𝟐
𝜋 𝟒 𝟑 𝟐 𝟑
𝑦 (0)=−0.3 , 𝑦 ( )=− 0.1
2

(− 𝟏+ 𝒉𝟐 ) 𝒘 𝟐 + ( 𝟐+ 𝒉 ) 𝒘 𝟏 +¿ − 𝒉 𝒄𝒐𝒔 𝒙 𝟏 − 𝟎 . 𝟑(𝟏+
𝟐 𝟐 𝟏
𝟐
𝒉)

𝑨 𝒘 =𝒃 (− 𝟏+ 𝒉𝟐 ) 𝒘 + (𝟐+ 𝒉 ) 𝒘 +(−𝟏 − 𝒉𝟐 ) 𝒘 =−𝒉 𝒄𝒐𝒔 𝒙


𝟑
𝟐
𝟐 𝟏
𝟐
𝟐

+ ( 𝟐+𝒉 ) 𝒘 +( − 𝟏 − ) 𝒘 =− 𝒉 𝒄𝒐𝒔 𝒙 −𝟎 . 𝟏(𝟏 − 𝒉)


𝟐 𝒉 𝟐𝟏
𝟑 𝟐 𝟑
𝟐 𝟐

[ ]
5 Write in Matrix form and 𝟐
−𝒉 𝒄 𝒐𝒔 ( 𝝅 / 𝟖 ) −𝟎 . 𝟑(𝟐+ 𝒉)
solve the system

¿
𝟐
− 𝒉 𝒄 𝒐𝒔 ( 𝝅 / 𝟒 )
−𝒉𝟐 𝒄 𝒐𝒔
𝟑𝝅
𝟖 ( )−𝟎 . 𝟏(𝟐 − 𝒉)

𝒘 𝟎=−𝟎 . 𝟑 𝒘𝟏 𝒘𝟐 𝒘𝟑 𝒘 𝟒=−𝟎 . 𝟏
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒

𝟎 𝝅 /𝟖 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐
Sec:11.3 Finite-Difference Methods for Linear Problems

Example
boundary-value problem

[ ]
𝟐
−𝒉 𝒄 𝒐𝒔 ( 𝝅 / 𝟖 ) −𝟎 . 𝟑(𝟐+ 𝒉)
, − 𝒉𝟐 𝒄 𝒐𝒔 ( 𝝅 / 𝟒 )
¿

𝑦 (0)=−0 . 3 , 𝑦 (
𝜋
2
)=−0 . 1
𝟐
−𝒉 𝒄 𝒐𝒔
𝟑𝝅
𝟖 ( )
−𝟎 . 𝟏(𝟐 − 𝒉)

[ ¿
][ ] [ ]
2.3084 -0.8037 0 𝒘 𝟏 = -0.5014 = −𝟎 . 𝟑𝟏𝟓𝟕
-1.1963 2.3084 -0.8037 𝒘𝟐 -0.1090 −𝟎 . 𝟐𝟖𝟐𝟗
0 ¿
-1.1963 2.3084 𝒘𝟑 -0.1394 −𝟎 . 𝟐𝟎𝟕𝟎

𝒚 (𝝅 /𝟖)≈ 𝒚 (𝟐 𝝅 /𝟖)≈ 𝒚 (𝟑 𝝅 /𝟖)≈


-0.3157 -0.2829 -0.2070
𝒘 𝟎=−𝟎 . 𝟑 𝒘𝟏 𝒘𝟐 𝒘𝟑 𝒘 𝟒=−𝟎 . 𝟏
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒

𝟎 𝝅 /𝟖 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐
Sec:11.3 Finite-Difference Methods for Linear Problems

[ ( 𝝅𝟖 ) −𝟎 . 𝟑(𝟐+ 𝒉)
]
− 𝒉𝟐 𝒄 𝒐𝒔
¿ − 𝒉𝟐 𝒄 𝒐𝒔 ( 𝝅 / 𝟒 )
𝟐 −𝒄𝒐𝒔 (𝟑𝝅
𝟖 )
−𝟎 . 𝟏(𝟐 − 𝒉)

𝟏
𝒚 ( 𝒙 )=− (𝒔𝒊𝒏 ⁡𝒙+𝟑 𝒄𝒐𝒔 ⁡𝒙).
𝟏𝟎

[ ]
= −𝟎 . 𝟑𝟏𝟓𝟕
−𝟎 . 𝟐𝟖𝟐𝟗
−𝟎 . 𝟐𝟎𝟕𝟎

𝑵 =𝟑
Sec:11.3 Finite-Difference Methods for Linear Problems

2.077106 0.90183- 0 0 0 0 0 0.36726-


1.09817- 2.077106 0.90183- 0 0 0 0 0.03562-
𝐴=¿ 0 1.09817- 2.077106 0.90183-
0
0
0 1.09817- 2.077106 0.90183-
0
0
0
0 𝑟h𝑠=¿
0.03206-
0.02726-
0 0 0 1.09817- 2.077106 0.90183- 0 0.02142-
0 0 0 0 1.09817- 2.077106 0.90183- 0.01475-
0 0 0 0 0 1.09817- 2.077106 0.0977-

0.3-
0.3138-
0.3155-
0.30505-
𝑤=¿ 0.28286-
0.2498-
0.20715-
0.15656-
0.1-

𝑵 =𝟕
Sec:11.3 Finite-Difference Methods for Linear Problems
BVP: Constant Coeff
The coefficients and are constants
The finite difference method
for the linear 2ed-order BVP divide the interval [a, b] into equal
subintervals
,
with and , h=(𝑏 −𝑎 )/( 𝑁 +1)

[ ] [][ ][ ]
h
2+ h2 𝒒 −1+ 𝒑 0 ⋯ 0

− 1−
h
𝒑 2+ h 𝒒
2
2
⋱ ⋱ ⋮
𝒘𝟏
2
− h 𝑟 ( 𝑥1 ) ( 1+ h2 𝒑) 𝜶
2 𝒘𝟐 − h2 𝑟 ( 𝑥2 ) 0
0 ⋱ ⋱ ⋱ 0 ⋮ = ⋮ + ⋮
h ⋮ ⋮ 0
⋮ ⋱ ⋱ ⋱ −1+ 𝒑
2 𝒘𝑵
( h
)
2
− h 𝑟 ( 𝑥 𝑁) 1− 𝒑 𝜷
h 2
0 … 0 − 1− 𝒑 2+ h2 𝒒
2

𝒘 𝟎=𝜶 𝒘𝟏 𝒘𝟐
⋯ 𝒘𝑵 𝒘 𝑵 +𝟏 =𝜷

𝒙𝟎 𝒙𝟏 𝒙𝟐
⋯ 𝒙𝑵 𝒙 𝑵 +𝟏

𝒂 𝒂+𝒉 𝒂+𝟐 𝒉 ⋯ 𝒂+𝑵𝒉 𝒃


Sec:11.3 Finite-Difference Methods for Linear Problems

[ ][ ]
BVP
( )
2 h h
2+ h 𝑞 −1+ 𝑝 0 ⋯ 0 2
−h 𝑟 ( 𝑥 1 ) + 1+ 𝑝 𝛼

[]
2 2
h 𝑤1 2
− 1− 𝑝 2+h 𝑞
2
⋱ ⋱ ⋮ − h 𝑟 ( 𝑥2)
2 𝑤2 ,

0 ⋱ ⋱ ⋱ 0 ⋮ = ⋮
h ⋮
⋮ ⋱ ⋱ ⋱ −1+ 𝑝 2
− h 𝑟 ( 𝑥 𝑁 −1 )
2 𝑤𝑁
0 … 0
h
− 1− 𝑝
2
2+h 𝑞 2 2
( h
− h 𝑟 ( 𝑥 𝑁 )+ 1 − 𝑝 𝛽
2 )
clear; clc
a=0; b=pi/2; alpha=-0.3; beta=-
0.1;
p=1; q=2; r = @(x) cos(x); N
=10;
[w,x] = function [w,x] = bvpconst(a,b,p,q,r,alpha,beta,N)
bvpconst(a,b,p,q,r,alpha,beta,N); h=(b-a)/(N+1); x=[a+h:h:b-h]';
d = (2+h^2*q)*ones(N,1);
% ----- display true solution -------- ad = (-1+h*p/2)*ones(N-1,1);
y_true = @(x) -(sin(x) + bd = (-1-h*p/2)*ones(N-1,1);
3*cos(x))./10; A = diag(d) + diag(ad,1) +diag(bd,-1);
n=50; hh=(b-a)/100; xx=a:hh:b;
Example rhs = -h^2*r(x);
yt = y_true(xx); rhs(1) = rhs(1) + alpha*(1+h*p/2);
plot(xx,yt,'-b',x,w,'or');
, grid on rhs(N) = rhs(N) + beta*(1-h*p/2);
x0 = sparse(N,1);
[w,iter] = GS(A,rhs,x0,1000,1e-5);
w=[alpha ; w; beta];
x = [a; x ;b];
end
Sec:11.3 Finite-Difference Methods for Linear Problems

[ ][ ] [ ]
( )
h h
2+ h2 𝑞 −1+ 𝑝 0 ⋯ 0 −h 2 𝑟 ( 𝑥 1 ) + 1+ 𝑝 𝛼
2 2
h 𝑤1 2
− 1− 𝑝 2+h 𝑞 2
⋱ ⋱ ⋮ − h 𝑟 ( 𝑥2)
2 𝑤2
= ⋮
0 ⋱ ⋱ ⋱ 0 ⋮ ⋮
h ⋮
⋮ ⋱ ⋱ ⋱ −1+ 𝑝 − h2 𝑟 ( 𝑥 𝑁 −1 )
2 𝑤𝑁
0 … 0 − 1−
h
2
𝑝 2+h 2 𝑞 (
− h2 𝑟 ( 𝑥 𝑁 ) + 1 −
h
2 )
𝑝 𝛽
Sec:11.3 Finite-Difference Methods for Linear Problems

[ ][ ] [ ]
( )
h h
2+ h2 𝑞 −1+ 𝑝 0 ⋯ 0 −h 2 𝑟 ( 𝑥 1 ) + 1+ 𝑝 𝛼
2 2
h 𝑤1
− 1− 𝑝 2+h2 𝑞 ⋱ ⋱ ⋮ − h2 𝑟 ( 𝑥 2 )
2 𝑤2
= ⋮
0 ⋱ ⋱ ⋱ 0 ⋮ ⋮
h ⋮
⋮ ⋱ ⋱ ⋱ −1+ 𝑝 2
− h 𝑟 ( 𝑥 𝑁 −1 )
2 𝑤𝑁
0 … 0
h
− 1− 𝑝
2
2+h 𝑞 2
(
− h2 𝑟 ( 𝑥 𝑁 ) + 1 −
h
2 )
𝑝 𝛽

𝑵 =𝟐 →
Sec:11.3 Finite-Difference Methods for Linear Problems

BVP Variable Coefficients


The finite difference method for the
linear 2ed-order BVP 𝒒 𝒊=𝒒( 𝒙 𝒊 ) 𝒑 𝒊=𝒑 ( 𝒙 𝒊 )
,
with and ,

𝒘𝟏

𝒘𝟐 𝒘𝑵
𝒒𝟏

𝒒𝟐 𝒒𝑵
𝒘 𝟎=𝜶 𝒑𝟏

𝒑𝟐 𝒑𝑵 𝒘 𝑵 +𝟏 =𝜷

𝒙𝟎 𝒙𝟏
⋯𝒙𝟐 𝒙𝑵 𝒙 𝑵 +𝟏

𝒂 𝒂+𝒉 𝒂+𝟐 𝒉 ⋯ 𝒂+𝑵𝒉 𝒃

[ ] [ ]
2 h h
2+ h 𝑞1 −1+ 𝑝 0 ⋯ 0 −h 2 𝑟 ( 𝑥 1 ) +( 1+ 𝑝 ) 𝑤0
2 1

[]
2 1
h 𝑤1 2
− 1− 𝑝 2+h 𝑞2
2
⋱ ⋱ ⋮ − h 𝑟 ( 𝑥2 )
2 2 𝑤2
= ⋮
0 ⋱ ⋱ ⋱ 0 ⋮ ⋮
h ⋮
⋮ ⋱ ⋱ ⋱ −1+ 𝑝 𝑁 −1 2
− h 𝑟 ( 𝑥 𝑁 − 1)
2 𝑤𝑁
h h
0 … 0 −1 − 𝑝 𝑁 2
2+ h 𝑞 𝑁 −h 2 𝑟 ( 𝑥 𝑁 ) +(1 − 𝑝 𝑁 )𝑤 𝑁 +1
2 2
Sec:11.3 Finite-Difference Methods for Linear Problems
Sec:11.3 Finite-Difference Methods for Linear Problems
Sec:11.3 Finite-Difference Methods for Linear Problems

Finite-Difference method
tridiagonal linear system
has a unique solution.
truncation error of order O()

Theorem 11.3
Sec:11.3 Finite-Difference Methods for Linear Problems
Sec:11.3 Finite-Difference Methods for Linear Problems

[ ] [ ]
2 h h
2+ h 𝑞1 −1+ 𝑝 0 ⋯ 0 2
−h 𝑟 ( 𝑥 1 ) +( 1+ 𝑝 )𝑤
2 1

[]
2 1 0
h 𝑤1 2
− 1− 𝑝 2+h 𝑞2
2
⋱ ⋱ ⋮ − h 𝑟 ( 𝑥2 )
2 2 𝑤2 link

0 ⋱ ⋱ ⋱ 0 ⋮ =

h ⋮
⋮ ⋱ ⋱ ⋱ −1+ 𝑝 𝑁 −1 2
− h 𝑟 ( 𝑥 𝑁 − 1)
2 𝑤𝑁
h h
0 … 0 −1 − 𝑝 𝑁 2
2+ h 𝑞 𝑁 −h 2 𝑟 ( 𝑥 𝑁 ) +(1 − 𝑝 𝑁 )𝑤 𝑁 +1
2 2

BVP
clear; clc
a=1; b=2; alpha=1; beta=2;
p=@(x) -2./x; q= @(x) 2./x.^2; r = @(x) ,
sin(log(x))./x.^2; N =9;
[w,x] = BVP(a,b,p,q,r,alpha,beta,N);
% ----- display true solution -------- function [w,x] = BVP(a,b,p,q,r,alpha,beta,N)
y_true = @(x) 1.1392070132*x+- h=(b-a)/(N+1); x=[a+h:h:b-h]';
0.03920701320./x.^2-(0.3)*sin(log(x))- pv = p(x); qv = q(x); rv = r(x);
0.1*cos(log(x)); d = 2+h^2*qv;
n=50; hh=(b-a)/100; xx=a:hh:b;
yt = y_true(xx);
ad = -1+h*pv(1:N-1)/2;
plot(xx,yt,'-b',x,w,'or'); grid on bd = -1-h*pv(2:N)/2;
A = diag(d) + diag(ad,1) +diag(bd,-1);
rhs = -h^2*rv;
Example rhs(1) = rhs(1) + alpha*(1+h*pv(1)/2);
rhs(N) = rhs(N) + beta*(1-h*pv(N)/2);
,
x0 = sparse(N,1);
[w,iter] = GS(A,rhs,x0,1000,1e-5);
w=[alpha ; w; beta];
x = [a; x ;b];
end
Sec:11.3 Finite-Difference Methods for Linear Problems
Sec:11.3 Finite-Difference Methods for Linear Problems

clear; clc
a=0; b=pi/2; alpha=-0.3; beta=-0.1;
p=1; q=2; r = @(x) cos(x);

N=3; h=(b-a)/(N+1); x=[a+h:h:b-h]';


d = (2+h^2*q)*ones(N,1);
ad = (-1+h*p/2)*ones(N-1,1);
bd = (-1-h*p/2)*ones(N-1,1);
A = diag(d) + diag(ad,1) +diag(bd,-1)
rhs = -h^2*r(x);
rhs(1) = rhs(1) + alpha*(1+h*p/2);
rhs(N) = rhs(N) + beta*(1-h*p/2);
w = A\rhs
w=[-0.3 ; w; -0.1];
x = [0; x ;pi/2];

% ----- display true solution -------- BVP


y_true = @(x) -(sin(x) + 3*cos(x))./10;
n=100; hh=(b-a)/100; xx=a:hh:b;
yt = y_true(xx);

plot(xx,yt,'-b',x,w,'or'); grid on ,

Example
,

𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒

𝟎 𝝅 /𝟖 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐

You might also like