MATLAB lessons N.5¶
A simple implementation of Euler scheme (vector form): File BVPsolver.m
:open:
1%
2% Solve the BVP of the form
3%
4% y'' + p y' + q y = r
5% y(a) = ya, y(b)=yb
6%
7% with N subinterval.
8%
9% h = (b-a)/N
10% x(k) = a + h*k,
11% y(k) ~ y(a+h*k),
12%
13% p : reference of a function of one argument p(x)
14% q : reference of a function of one argument q(x)
15% r : reference of a function of one argument r(x)
16%
17% BCa : vector of two element [ a , y(a) ] with left BC
18% BCb : vector of two element [ b , y(b) ] with ight BC
19%
20% N : number of subdivision of interval [a,b]
21%
22function [x,y] = BVPsolver( p, q, r, BCa, BCb, N )
23 % extract the boundary conditions
24 a = BCa(1) ;
25 b = BCb(1) ;
26 ya = BCa(2) ;
27 yb = BCb(2) ;
28 % fill x
29 h = (b-a)/N ;
30 x = a + h*[0:N]' ;
31 % fill the 3 diagonal of the linear system and the RHS
32 alpha = zeros( N-1, 1 ) ;
33 beta = zeros( N-1, 1 ) ;
34 gamma = zeros( N-1, 1 ) ;
35 omega = zeros( N-1, 1 ) ;
36
37 alpha = -2/h^2 + feval( q, x(2:end-1) ) ;
38 beta = 1/h^2 - feval( p, x(2:end-1) ) ./(2*h) ;
39 gamma = 1/h^2 + feval( p, x(2:end-1) ) ./(2*h) ;
40 omega = feval( r, x(2:end-1) ) ;
41 omega(1) = omega(1) - beta(1)*ya ;
42 omega(N-1) = omega(N-1) - gamma(N-1)*yb ;
43
44 % this is equivalent to the following loop
45 % for k=1:N-1
46 % alpha(k) = -2/h^2 + feval( q, x(k+1) ) ;
47 % beta(k) = 1/h^2 - feval( p, x(k+1) ) /(2*h) ;
48 % gamma(k) = 1/h^2 + feval( q, x(k+1) ) /(2*h) ;
49 % omega(k) = feval( r, x(2:end-1) ) ;
50 % end
51 % omega(1) = omega(1) - beta(1)*ya ;
52 % omega(N-1) = omega(N-1) - gamma(N-1)*yb ;
53
54 % fill the tridiagonal system
55 A = zeros( N-1, N-1 ) ;
56 A = diag(alpha,0) + diag(gamma(1:N-2),1) + diag( beta(2:N-1),-1) ;
57
58 % this is equivalent to the following loop
59 % for k=1:N-1
60 % A(k,k) = alpha(k) ;
61 % end
62 % for k=1:N-2
63 % A(k+1,k) = beta(k+1) ; % fill the lower diagonal
64 % A(k,k+1) = gamma(k) ; % fill the upper diagonal
65 % end
66
67 % fill the RHS
68 b = zeros( N-1, 1 ) ;
69 b = omega ;
70
71 % solve the linear system
72 sol = A\b ;
73
74 % fill the y
75 y = [ ya; sol; yb] ;
76
77end
A simple BVP defined by the functions p1
, q1
,
r1
to test the code:
File p1.m
:open:
1function res = p1( x )
2 res = -x ;
3end
File q1.m
:open:
1function res = q1(x)
2 %res = -1 ;
3 res = -ones(size(x)) ;
4end
File r1.m
:open:
1function res = r1(x)
2 %res = 1 ;
3 res = ones(size(x)) ;
4end
Exact solution to check the order: File exact1.m
:open:
1function res = exact1(x)
2 t1 = exp(0.5);
3 t3 = sqrt(2);
4 t5 = erf(t3 / 2);
5 t10 = erf(x .* t3 / 2);
6 t11 = x .^ 2;
7 t13 = exp(t11 / 2 - 1 / 2);
8 t17 = exp(t11 / 2);
9 res = t13 .* t10 ./ t5 .* (3 - t1) + t17 - 1 ;
10end
A simple script to check the order: File checkerror.m
:open:
1BCa = [0;0] ;
2BCb = [1;2] ;
3
4N = [ 10, 20, 40, 80, 160, 320 ] ;
5
6for i=1:length(N)
7 [x,y] = BVPsolver( @p1, @q1, @r1, BCa, BCb, N(i) );
8 err(i) = norm( y - exact1( x ), inf ) ;
9end
10
11for i=2:length(N)
12 h1 = 1/N(i-1) ;
13 h = 1/N(i) ;
14 p = log( err(i-1)/err(i) ) / log( h1/h ) ;
15 disp(p) ;
16end