Numerical Methods for Differential Equations Contents • Review of numerical integration methods – Rectangular Rule – Trapezoidal Rule – Simpson’s Rule • How to make a connect-the-dots graphic • Numerical Methods for y 0 = F (x) – Maple code for Rect, Trap, Simp methods • Numerical Methods for y 0 = f (x, y) – Maple code for Euler, Heun, RK4 methods • Methods for planar systems • Methods for n × n systems y Rectangular Rule. The approximation uses Euler’s idea of replacing the integrand by a constant. The value of the integral is approximately the area of a rectangle of width b − a and height F (a). Z F x b a b F (x)dx ≈ (b − a)F (a) a y Trapezoidal Rule. The rule replaces the integrand F (x) by a linear function L(x) which connects the planar points (a, F (a)), (b, F (b)). The value of the integral is approximately the area under the curve L, which is the area of a trapezoid. Z b F (x)dx ≈ b−a 2 a x a Simpson’s Rule. The rule replaces the integrand F (x) by a quadratic polynomial Q(x) which connects the planar points (a, F (a)), ((a + b)/2, F ((a + b)/2)), (b, F (b)). Then the integral of F is approximately the area under the quadratic curve Q. b a F (x)dx ≈ (b − a) b (F (a) + F (b)) y Z F L F (a) + 4F a+b 2 6 + F (b) Q F x a b How to make a connect-the-dots graphic x 0.0 0.1 0.2 0.3 0.4 0.5 y 2.000 1.901 1.808 1.727 1.664 1.625 x 0.6 0.7 0.8 0.9 1.0 y 1.616 1.643 1.712 1.829 2.000 y x The table consists of xy -values for y = x3 − x + 2 . The graphic represents the table’s rows, which are pairs (x, y), as dots. Joined dots make the connect-the-dots graphic. Maple code A connect-the-dots graphic can be made in maple by supplying a list L of pairs to be connected. An example: L:=[[1,3],[2,1],[3,5]]: plot(L); Numerical Methods for y 0 = F (x) Rx Quadrature applies to give y(x) = y0 + x0 F (x)dx. Numerical solution methods amount to approximating the integral on the right by Rectangular, Trapezoidal and Simpson methods. R x0+h The methods replace the exact value of an integral x0 F (x)dx by a numerical approximation value which is useful for graphics when h is small. Larger intervals are broken into smaller intervals of length h, then the approximation is applied. Table 1. Three numerical integration methods. Rect Y = y0 + hF (x0 ) Trap Y = y0 + Simp Y = y0 + h 2 h 6 (F (x0) + F (x0 + h)) (F (x0) + 4F (x0 + h/2) + F (x0 + h))) Maple code for the Rectangular and Trapezoid Rules # Rectangular algorithm # Trapezoidal algorithm # Group 1, initialize. # Group 1, initialize. F:=x->evalf(cos(x) + 2*x): F:=x->evalf(cos(x) + 2*x): x0:=0:y0:=0:h:=0.1*Pi: x0:=0:y0:=0:h:=0.1*Pi: Dots1:=[x0,y0]: Dots2:=[x0,y0]: # Group 2, repeat 10 times Y:=y0+h*F(x0): x0:=x0+h:y0:=evalf(Y): Dots1:=Dots1,[x0,y0]; # Group 2, repeat 10 times Y:=y0+h*(F(x0)+F(x0+h))/2: x0:=x0+h:y0:=evalf(Y): Dots2:=Dots2,[x0,y0]; # Group 3, plot. plot([Dots1]); # Group 3, plot. plot([Dots2]); Maple code for Rectangular and Simpson Rules # Rectangular algorithm # Simpson algorithm # Group 1, initialize. # Group 1, initialize. F:=x->evalf(exp(-x*x)): F:=x->evalf(exp(-x*x)): x0:=0:y0:=0:h:=0.1: x0:=0:y0:=0:h:=0.1: Dots1:=[x0,y0]: Dots3:=[x0,y0]: # Group 2, repeat 10 times Y:=evalf(y0+h*F(x0)): x0:=x0+h:y0:=Y: Dots1:=Dots1,[x0,y0]; # Group 2, repeat 10 times Y:=evalf(y0+h*(F(x0)+ 4*F(x0+h/2)+F(x0+h))/6): x0:=x0+h:y0:=Y: Dots3:=Dots3,[x0,y0]; # Group 3, plot. plot([Dots1]); # Group 3, plot. plot([Dots3]); Numerical Methods for y 0 = f (x, y) The methods replace the exact value of Z x0 +h y(x0 + h) = y0 + f (x, y(x))dx x0 by a numerical approximation Y . The value is useful for graphics when h is small. Table 2. Three numerical methods for y 0 = f (x, y). Euler Y = y0 + hf (x0 , y0 ) Heun y1 = y0 + hf (x0, y0) Y = y0 + RK4 h (f (x0, y0) + f (x0 + h, y1)) 2 k1 = hf (x0, y0) k2 = hf (x0 + h/2, y0 + k1/2) k3 = hf (x0 + h/2, y0 + k2/2) k4 = hf (x0 + h, y0 + k3) k1 + 2k2 + 2k3 + k4 Y = y0 + 6 Maple code for Euler and Heun methods # Euler algorithm # Group 1, initialize. f:=(x,y)->-y+1-x: x0:=0:y0:=3:h:=0.1:L:=[x0,y0]: # Heun algorithm # Group 1, initialize. f:=(x,y)->-y+1-x: x0:=0:y0:=3:h:=0.1:L:=[x0,y0]: # Group 2, repeat 10 times Y:=y0+h*f(x0,y0): x0:=x0+h:y0:=Y:L:=L,[x0,y0]; # Group 2, repeat 10 times Y:=y0+h*f(x0,y0): Y:=y0+h*(f(x0,y0)+f(x0+h,Y))/2: x0:=x0+h:y0:=Y:L:=L,[x0,y0]; # Group 3, plot. plot([L]); # Group 3, plot. plot([L]); Maple code for Heun and RK4 methods # Heun algorithm # Group 1, initialize. f:=(x,y)->-y+1-x: x0:=0:y0:=3:h:=0.1:L:=[x0,y0]: # RK4 algorithm # Group 1, initialize. f:=(x,y)->-y+1-x: x0:=0:y0:=3:h:=0.1:L:=[x0,y0]: # Group 2, repeat 10 times Y:=y0+h*f(x0,y0): Y:=y0+h*(f(x0,y0)+f(x0+h,Y))/2: x0:=x0+h:y0:=Y:L:=L,[x0,y0]; # Group 2, repeat 10 times. k1:=h*f(x0,y0): k2:=h*f(x0+h/2,y0+k1/2): k3:=h*f(x0+h/2,y0+k2/2): k4:=h*f(x0+h,y0+k3): Y:=y0+(k1+2*k2+2*k3+k4)/6: x0:=x0+h:y0:=Y:L:=L,[x0,y0]; # Group 3, plot. plot([L]); # Group 3, plot. plot([L]); Numerical Algorithms: Planar Case Notation. Let t0 , x0 , y0 denote the entries of the dot table on a particular line. Let h be the increment for the dot table and let t0 + h, x, y stand for the dot table entries on the next line. Planar Euler Method. x = x0 + hf (t0, x0, y0), y = y0 + hg(t0, x0, y0). Planar Heun Method. x1 y1 x y = = = = x0 + hf (t0, x0, y0), y0 + hg(t0, x0, y0), x0 + h(f (t0, x0, y0) + f (t0 + h, x1, y1))/2 y0 + h(g(t0, x0, y0) + g(t0 + h, x1, y1))/2. Planar RK4 Method. k1 m1 k2 m2 k3 m3 k4 m4 x y = = = = = = = = hf (t0, x0, y0), hg(t0, x0, y0), hf (t0 + h/2, x0 + k1/2, y0 + m1/2), hg(t0 + h/2, x0 + k1/2, y0 + m1/2), hf (t0 + h/2, x0 + k2/2, y0 + m2/2), hg(t0 + h/2, x0 + k2/2, y0 + m2/2), hf (t0 + h, x0 + k3, y0 + m3), hg(t0 + h, x0 + k3, y0 + m3), 1 = x0 + (k1 + 2k2 + 2k3 + k4) , 6 1 = y0 + (m1 + 2m2 + 2m3 + m4) . 6 Numerical Algorithms: General Case Consider a vector initial value problem u0(t) = F(t, u(t)), u(t0) = u0. Vector Euler Method u = u0 + hF(t0, u0) Vector Heun Method w = u0 + hF(t0, u0), h u = u0 + (F(t0, u0) + F(t0 + h, w)) 2 Vector RK4 Method k1 k1 k1 k1 = = = = hF(t0, u0), hF(t0 + h/2, u0 + k1/2), hF(t0 + h/2, u0 + k2/2), hF(t0 + h, u0 + k3), 1 u = u0 + (k1 + 2k2 + 2k3 + k4) . 6