ColoState Notes on Finite Difference Method MATH451 for Linear 2nd Order ODE Boundary Value Problems Jiangguo (James) Liu ∗ March 2, 2016 We consider a typical two-point boundary value problem for a linear 2nd order ODE ( 00 x (t) = u(t) + v(t)x(t) + w(t)x0 (t), t ∈ [a, b] x(a) = α, x(b) = β (1) After applying respectively the 2nd order and 1st order central finite differences for discretization x00 (ti ), x0 (ti ), we obtain 1 1 − 1 − hwi yi−1 + 2 + h2 vi yi + − 1 + hwi yi+1 = −h2 fi (2) 2 2 Here is an example with [a, b] = [0, 1], α = 0, β = 1 and u(t) = −π 2 sin(πt) − 2t(1 + π cos(πt)) − (t2 + 1)(t + sin(πt)); v(t) = t2 + 1, w(t) = 2t along with a known exact solution x(t) = t + sin(πt). FDM for 2nd order ODE BVP: Numerical vs exact solutions 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Figure 1: Time step h = 0.1; ODE: x00 = u(t) + v(t)x + w(t)x0 , BVP: x(0) = 0, x(1) = 1; Exact solution x(t) = t + sin(πt) ∗ Department of Mathematics, (liu@math.colostate.edu) Colorado State University, 1 Fort Collins, CO 80523-1874, USA Shown below is Matlab code. close all; clear all; format compact; format long e; a = 0; b = 1; alpha = 0; beta = 1; h = 0.1; n = (b-a)/h - 1; t = a:h:b; % All nodes including boundary nodes wval vval uval xval = = = = 2*t; t.*t + 1; % Componentwise multiplication -(pi^2)*sin(pi*t) - 2*t.*(1+pi*cos(pi*t)) - (t.*t+1).*(t+sin(pi*t)); t + sin(pi*t); dtmp atmp ctmp btmp = 2 + (h*h)*vval; = -1 - 0.5*h*wval; = -1 + 0.5*h*wval; = -(h*h)*uval; d = dtmp(2:n+1); a = atmp(3:n+1); c = ctmp(2:n); b = btmp(2:n+1); b(n) = b(n) - ctmp(n+1)*beta; % Applying bndry cond. at the right endpoint % Solving the linear system A = diag(d,0) + diag(a,-1) + diag(c,1); NumerSln = A\b’; % ExactSln = xval(2:n+1)’; norm(ExactSln-NumerSln,inf) % Padding with boundary values x = [alpha; ExactSln; beta]; y = [alpha; NumerSln; beta]; figure(1); plot(t,x,’k-’,t,y,’r*’); title(’FDM 2nd order ODE BVP: Numerical vs exact solutions’); return; 2