11.3 Finite Difference Methods for Linear Problems
11.3 Finite Difference Methods for Linear Problems
11.3 Finite Difference Methods for Linear Problems
Finite-Difference
Methods for Linear
Problems
Sec:11.3 Finite-Difference Methods for Linear Problems
,
𝝅
𝑦 (𝟎)=− 0.3 , 𝑦 ( )=− 0.1
𝟐
Sec:11.3 Finite-Difference Methods for Linear Problems
Example
Initial-value problem
,
,
with and ,
Sec:11.3 Finite-Difference Methods for Linear Problems
𝒑 ( 𝒙 )=𝟏
𝟏
𝒚 ( 𝒙 )=− ( 𝒔𝒊𝒏 𝒙 +𝟑 𝒄𝒐𝒔 𝒙 ).
𝟏𝟎
𝒒 ( 𝒙 ) =𝟐
𝒓 ( 𝒙 )=𝐜𝐨𝐬 𝒙
𝒂= 𝟎𝒃=𝝅 / 𝟐
𝜶=− 𝟎. 𝟑𝜷=− 𝟎 .𝟏
Sec:11.3 Finite-Difference Methods for Linear Problems
𝒙𝒊− 𝐡 𝒙𝒊 𝒙 𝒊 +𝐡
where
′ 𝒚 (𝒙 ¿¿ 𝒊 − 𝟏) 𝒉𝟐 (𝟑)
′ 𝒚 ( 𝒙 𝒊 )= 𝒚 (𝒙¿ ¿ 𝒊+𝟏)− − 𝒚 ( 𝝃𝟐) ¿ ¿
𝒚 ( 𝒙𝒊) 𝟐𝒉 𝟔
where
Sec:11.3 Finite-Difference Methods for Linear Problems
1 we select an integer N
+¿𝒄𝒐𝒔 𝒙 𝒊
N=3
𝟐𝒉 +¿
≈𝒚 (𝒙 ¿¿ 𝒊+𝟏)− 𝒚 (𝒙¿¿𝒊−𝟏)
𝒚 (𝒙 ¿¿ 𝒊− 𝟏)
𝒚 (𝒙 ¿¿ 𝒊+𝟏)− 𝟐 𝒚 (𝒙 𝒊)+ ¿¿ ¿ ¿ 𝟐 𝒚(𝒙¿¿𝒊)¿
𝒉𝟐
( − 𝟏+
𝒉
𝟐 )
𝒘 𝒊+𝟏 + ( 𝟐+𝒉𝟐 ) 𝒘 𝒊 + −𝟏 −
𝒉
𝟐 (
𝒘 𝒊 −𝟏 =−𝒉𝟐 𝒄𝒐𝒔 𝒙 𝒊 )
𝒘 𝟎=−𝟎 . 𝟑 𝒘𝟏 𝒘𝟐 𝒘𝟑 𝒘 𝟒=−𝟎 . 𝟏
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
𝟎 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐
𝝅 /𝟖
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 ( 𝟐) 𝟐 𝟏 ( 𝟐)
𝒊=𝟏 → − 𝟏+ 𝒉 𝒘 + ( 𝟐+ 𝒉𝟐 ) 𝒘 + −𝟏 − 𝒉 𝒘 =−𝒉𝟐 𝒄𝒐𝒔 𝒙
𝟎 𝟏
𝒘 𝟎=−𝟎 . 𝟑 𝒘𝟏 𝒘𝟐 𝒘𝟑 𝒘 𝟒=−𝟎 . 𝟏
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
𝟎 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐
𝝅 /𝟖
Sec:11.3 Finite-Difference Methods for Linear Problems
Example 4 ( 𝟐) 𝟐 𝟏 ( 𝟐)
𝒊=𝟏 → − 𝟏+ 𝒉 𝒘 + ( 𝟐+ 𝒉𝟐 ) 𝒘 + −𝟏 − 𝒉 𝒘 =−𝒉𝟐 𝒄𝒐𝒔 𝒙
𝟎 𝟏
boundary-value problem
,
( 𝟐) 𝟑 ( 𝟐)
𝒊=𝟐 → − 𝟏+ 𝒉 𝒘 + ( 𝟐+ 𝒉 ) 𝒘 + −𝟏 − 𝒉 𝒘 =−𝒉 𝒄𝒐𝒔 𝒙
𝟐
𝟐 𝟏
𝟐
𝟐
(− 𝟏+ 𝒉𝟐 ) 𝒘 𝟐 + ( 𝟐+ 𝒉 ) 𝒘 𝟏 +¿ − 𝒉 𝒄𝒐𝒔 𝒙 𝟏 − 𝟎 . 𝟑(𝟏+
𝟐 𝟐 𝟏
𝟐
𝒉)
[ ]
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 −𝟎 . 𝟐𝟎𝟕𝟎
𝟎 𝝅 /𝟖 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐
Sec:11.3 Finite-Difference Methods for Linear Problems
[ ( 𝝅𝟖 ) −𝟎 . 𝟑(𝟐+ 𝒉)
]
− 𝒉𝟐 𝒄 𝒐𝒔
¿ − 𝒉𝟐 𝒄 𝒐𝒔 ( 𝝅 / 𝟒 )
𝟐 −𝒄𝒐𝒔 (𝟑𝝅
𝟖 )
−𝟎 . 𝟏(𝟐 − 𝒉)
𝟏
𝒚 ( 𝒙 )=− (𝒔𝒊𝒏 𝒙+𝟑 𝒄𝒐𝒔 𝒙).
𝟏𝟎
[ ]
= −𝟎 . 𝟑𝟏𝟓𝟕
−𝟎 . 𝟐𝟖𝟐𝟗
−𝟎 . 𝟐𝟎𝟕𝟎
𝑵 =𝟑
Sec:11.3 Finite-Difference Methods for Linear Problems
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
𝒘 𝟎=𝜶 𝒘𝟏 𝒘𝟐
⋯ 𝒘𝑵 𝒘 𝑵 +𝟏 =𝜷
𝒙𝟎 𝒙𝟏 𝒙𝟐
⋯ 𝒙𝑵 𝒙 𝑵 +𝟏
[ ][ ]
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
𝒘𝟏
⋯
𝒘𝟐 𝒘𝑵
𝒒𝟏
⋯
𝒒𝟐 𝒒𝑵
𝒘 𝟎=𝜶 𝒑𝟏
⋯
𝒑𝟐 𝒑𝑵 𝒘 𝑵 +𝟏 =𝜷
𝒙𝟎 𝒙𝟏
⋯𝒙𝟐 𝒙𝑵 𝒙 𝑵 +𝟏
[ ] [ ]
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);
plot(xx,yt,'-b',x,w,'or'); grid on ,
Example
,
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
𝟎 𝝅 /𝟖 𝟐 𝝅 /𝟖 𝟑 𝝅 /𝟖 𝝅 /𝟐