Mathematics for chemical engineers Drahoslava Janovská Department of mathematics Winter semester 2013-2014
Numerical solution of ordinary differential equations Initial value problem
Outline 1 Introduction 2 Euler s method Runge - Kutta methods
Introduction Numerical solution of differential equations necessity for engineering applications. We will consider a solution of one differential equation as well as a solution of systems of differential equations. Equations of the higher orders can be transformed into a system of differential equations of the first order. In particular, we will study differential equations of the first order, both initial and boundary value problems.
Solution of the initial value problem by a one step method We solve the initial value (Cauchy) problem y = f (x, y), y(x 0 ) = y 0. Uniqueness and existence of a solution: If f (x, y) is continuous in Ω = {(x, y), x x 0 a, a > 0, y y 0 b, b > 0} and if we denote M = max f (x, y), α = min(a, b (x,y) Ω M ), then there exist a solution of the equation y = f (x, y) defined in the interval (x 0 α, x 0 + α). Moreover, if f is a Lipschitz function, i.e. f (x, y 1 ) f (x, y 2 ) L y 1 y 2 L > 0, (x, y 1 ), (x, y 2 ) Ω, the solution is unique. Remark: L... Lipschitz constant
Taylor expansion of the solution We are looking for a function y = y(t) such that dy dt = y = f (y, t), y(0) = y 0, 0 < t < T. Let us suppose that we have already computed the solution y n := y(t n) in time t n and we want to find a solution in time t n+1. Let h := t n+1 t n be the corresponding time step. Taylor expansion of y n+1 in the point y n: And y n?... etc. y n+1 = y n + h y n }{{} + h2 2 y n + h3 6 y n +... y = = f (y n, t n) from the differential equation ( ) dy = f dt t 1 + f y y = f t + f y f y = (ft + f fy) + t y (ft + f fy) f = ftt + 2f fyt + ft fy + f f y 2 + f 2 f yy,
Euler s method Euler s method Euler s method is based on the first two terms of the Taylor expansion: We choose y(0) := y 0 and construct a sequence y n+1 = y n + hf (y n, t n), i.e.,we replace the function y in the point (y n, t n) by a tangent to the integral curve at this point. Accuracy of calculation: Euler s method is the first order method. Remark The step of the method h can be changed with a particular iteration (adaptive choice of step): y n+1 = y n + h nf (y n, t n).
Euler s method Example Solve the following initial value problem by Euler s method. Find the solution in time t = 3, y = 0, 3 y sin(t), y(1) = 2. For simplicity, let us consider n = 4, i.e., h = 0.5. y j+1 = y j + h f (y j, t j ), where f (y j, t j ) = 0, 3y j sin(t j ). t j step 0.5 step 0.005 1 2 2 1.5 2.252441295 2.30249902026881692 2 2.589461130 2.66460601831410714 2.5 2.942649681 2.99089235783755570 3 3.206813761 3.16533517440834976
Euler s method Stability of Euler s method Let us solve the model problem y = λy, λ a constant (1) Euler s method = y n+1 = y n + λh y n, t.j. y n+1 = y n(1 + λ h), i.e., we obtain y n = y n 1 (1 + λ h) = y n 2 (1 + λ h) 2 =... = y 0 (1 + λ h) n. For λ = λ 1 + iλ 2... imaginary, we have y n = y 0 (1 + λ 1 h + iλ 2 h }{{} )n = y 0 σ n. σ... so called amplification factor The numerical solution is stable (i.e. it will remain limited for the growing (large) n, too), if σ 1.
Euler s method Let in (1), λ = λ 1 + iλ 2, λ 1 0, σ = 1 + λ 1 h + iλ 2 h. Then the region of stability for Euler s method is part of the left half of the complex plane, in particular inside of the circle σ 2 = (1 + λ 1 h) 2 + λ 2 2h 2 = 1. For any value of λh in the left half of the complex plane outside of this circle the numerical solution is blowing up, while the exact solution decreases. If we want to have a stable solution, we must reduce the h so, that λh would be inside the circle.
Euler s method Implicit (backward) Euler s method The y n+1 occurs in the equation implicitly: y n+1 = y n + h f (y n+1, t n+1 ) Disadvantage: the method is computationally more demanding than explicit Euler s method. Advantage: it is more stable, sometimes linearization of f can be exploited with advantage. Example Let us apply the implicit Euler s method to the model problem (1): We obtain: 1 y n+1 = y n + λhy n+1 = y n+1 = yn, i.e., 1 λh ( ) 2 ( ) n 1 1 1 y n = 1 λh y n 1 = y n 2 =... = y 0. 1 λh 1 λh y n = σ n y 0, σ = 1 1 λh.
Euler s method θ methods We can define the following one-parameter class of one-step methods, called θ methods: For a given initial approximation y 0, we define y n+1 as a convex combination of f (y n, t n) and f (y n+1, t n+1 ) (θ... parameter): y n+1 = y n + h[(1 θ)f (y n, t n) + θf (y n+1, t n+1 )], n = 0, 1,..., N 1, θ 0, 1. θ = 0 = y n+1 = y n + hf (y n, t n)... (explicit) Euler s method θ = 1 = y n+1 = y n + hf (y n+1, t n+1 )... implicit Euler s method θ = 1 2 = y n+1 = y n + 1 2 h[f (yn, tn) + f (y n+1, t n+1 )]... trapezoidal rule. It can be shown that a θ method is explicit for θ = 0 and it is implicit for 0 < θ 1.
Runge - Kutta methods Runge - Kutta methods Runge - Kutta methods (RK) more precise than Euler s methods: explicit: The solution in time t n+1 is computed from the values y n, f (y n, t n) and from f (y, t) enumerated at a point between points t n and t n+1 = better accuracy because we use more information about the function f. implicit: They usually lead to the solution of nonlinear algebraic equations, but the amount of work involved is balanced by better numerical stability.
Runge - Kutta methods RK method of the second order Let us have the equation y = f (y, t). In the time step t n+1, we obtain the solution from the equation where y n+1 = y n + γ 1 k 1 + γ 2 k 2, (2) k 1 = hf (y n, t n) k 2 = hf (y n + βk 1, t n + αh), α, β, γ 1, γ 2 R. The constants α, β, γ 1, γ 2 have to be determined in such a way that the method should have the highest order of accuracy possible. To determine the order of accuracy, we exploit the Taylor expansion of y(t n+1 ) y n+1 = y n + h y n }{{} + h2 2 y n }{{} + = f (y n, t n) f t + f f y y n+1 = y n + hf (y n, t n) + h2 2 (ftn + f nf yn )+ (3) and compare the coefficients in (2) and (3).
Runge - Kutta methods Taylor series for the function of two variables k 2 = hf (y n + βk 1, t n + αh) = ( ) k 2 = h f (y n, t n) + βk 1 f yn + αhf tn + O(h 2 ). Remark Symbol O (capital O) g(h) = O(h p ) g(h) C h p, C is a constant independent of h. Then y n+1 = y n + (γ 1 + γ 2 )hf n + γ 2 βh 2 f nf yn + γ 2 αh 2 f tn + We compare the result with (3) and obtain three nonlinear equations for 4 unknowns: γ 1 + γ 2 = 1, γ 2 α = 1 2, γ 2β = 1 2. Let α R be a parameter. Then γ 2 = 1 2α, β = α, γ 1 = (1 1 2α ).
Runge - Kutta methods We obtain Runge - Kutta methods of the order 2: k 1 = hf (y n, t n) k 2 = hf (y n + αk 1, t n + αh) y n+1 = y n + (1 1 2α )k 1 + 1 2α k 2. We choose α and get the method. For example α = 1 2 = γ 2 = 1, β = 1 2, γ 1 = 0 = y n+1 = y n + k 2 = y n + hf (y n + 1 2 k 1, t n + 1 2 h). Remark RK method of the 2nd order requires at each step twice quantification of function values.
Runge - Kutta methods RK methods of 4th order For solving initial value problem, the RK methods of 4th order are mostly used y n+1 = y n + 1 6 k 1 + 1 3 (k 2 + k 3 ) + 1 6 k 4, where k 1 = hf (y n, t n) k 2 = hf (y n + 1 2 k 1, t n + h 2 ) k 3 = hf (y n + 1 2 k 2, t n + h 2 ) k 4 = hf (y n + k 3, t n + h). At each step, we need to compute function values 4 times. Although laborious, the RK method of 4th order is stable and very accurate. It is easily programmable, because it requires no differentiation, only computation of function values.
Runge - Kutta methods Computational error Computational error a) y(t n)... exact solution in time t n, y n... approximate solution in time t n e n = y n y(t n)... global error of the approximation b) The computer works in finite arithmetic: r n = ỹ n y n... rounding error We want to compute f but in fact, we compute the numerical approximation ỹ n. For Euler s method: (1)... the global error of approximation e n, E n is directly proportional to the first power of h (2)... rounding error r n is inversely proportional to the first power of h (3)... total error for Euler s method About rounding errors, we can convince only if we repeat the calculation with the different pracission (double precission,... )