Lesson 4: Population, Taylor and Runge-Kutta Methods 4.1 Applied Problem. Consider a single fish population whose size is given by x(t). The change in the size of the fish population over a given time interval is approximately equal to the product of the change in time, t, the difference in the birth rate and death rate, b - d, and the "present" fish population, x(t). This may be written as x(t + t) - x(t) t (b - d) x(t) where t is between t and t + t. The simplest model is for b - d being a constant. A second model is the logistic model where b - d = c(m - x) with c, M constant and M is the maximum population size. A third model may involve harvesting, -h(t), or stocking, +s(t), at a given rate. We want to be able to predict the fish population size as a function of these parameters. In this lesson we will focus on the logistic model, and in the lesson five we will account for harvesting or stocking of the lake. In both these lessons we have assumed there are no predator fish that significantly alter the fish population; we will study this in lesson seven. 4.2 Differential Equation Model. A differential equation can be formed from the above approximation by dividing by t and letting it go to zero. The approximation will get better as t goes to zero
2 because the population size in the interval will vary less as t goes to zero. The above approximation becomes x' = (b - d)x. If (b - d) are constant, then the solution is x(t) = x(0) e (b - d)t. If b - d = c(m - x), then the solution can be found in terms of a more complicated expression with an exponential function, provided c and M are constants! In more sophisticated models this is not the case, and so, we are forced to consider numerical solution to these models. A variation of the logistic model that includes harvesting is x' = c(m - x)x - h(t). 4.3 Taylor Numerical Method. We wish to have a higher order approximation scheme, and so, we apply the fourth order Taylor polynomial approximation to u(t + h) and a = t. Let u have five continuous derivatives so that for some C between t and t + h we have u(t + h) = u(t) + u t (t) h + u tt (t) h 2 /2 + u ttt (t) h 3 /6 + u tttt (t) h 4 /24 + u (5) (C) h 5 /120. Since u is a solution of the differential equation, one can compute the higher order derivatives by using the chain rule, for example, u tt = f t (t,u(t)) 1 + f u (t,u(t)) u t = f t (t,u(t)) 1 + f u (t,u(t)) f(t,u(t)). If f(t,u) is complicated, then these calculations will become costly. However, for simpler cases it is very powerful. Consider the Newton's law of cooling where f(t,u) = c(usur - u). Then the n th derivative of u has the form (-c) n-1 f(u). The above Taylor polynomial with variable h is
3 u(t + h) = u(t) + f(u(t)) h + (-c) 1 f(u(t)) h 2 /2 + (-c) 2 f(u(t)) h 3 /6 This suggests the following algorithm + (-c) 3 f(u(t)) h 4 /24 + (-c) 4 f(c)h 5 /120. u k+1 = u k + f(u k ) h - cf(u k ) h 2 /2 + c 2 f(u k ) h 3 /6 - c 3 f(u k ) h 4 /24. Since the error in Taylor's theorem is of order 5, the discretization error might have an error of order 4. This is the case provided the solution has 5 continuous derivatives. The following Matlab code in the m-file tayerr.m illustrated the order of convergence for the cooling problem with c = 1/26, ysur = 70 and y(0) = 200. The errors are computed at time equal to 10.0, and the steps sizes are reduced by 50% of the previous step size. The table indicates the fourth order Taylor method has an error that is proportional the step size raised to the fourth. Matlab m-file tayerr.m: Euler and Fourth Order Taylor Errors clear; kk = 40; T = 10.0; dt = T/kk; u0 = 200.; c = 1/26; usur = 70.; uk = u0; ut = u0; for k = 1:kk uk = uk + dt*c*(usur -uk); uex = usur + (u0 -usur)*exp(-c*k*dt); utp = ut; ut = ut + dt*c*(usur -utp); ut = ut - (dt^2)/2*c*(c*(usur -utp)); ut = ut + (dt^3)/6*c^2*(c*(usur -utp)); ut = ut - (dt^4)/24*c^3*(c*(usur -utp)); erreul = abs(uk - uex); errtay = abs(ut - uex); end kk erreul errtay
4 Table: Discretization Errors with Euler Order One and Taylor Order Four K (h = 10/K) Euler Error Taylor Error 5 (2.000) 1.3696 105.89 10-7 10 (1.000) 0.6693 6.4089 10-7 20 (0.500) 0.3309 0.3941 10-7 40 (0.250) 0.1645 0.0244 10-7 The n th order Talyor method for approximating the solution of ut = f(t,u(t)) with u(0) given is u k+1 = u k + f(t k, u k ) h + f (1) ( t k, u k ) h 2 /2 +... + f (n-1) ( t k, u k ) h n /n! where f (i) is the i th derivative of the composed function f(t, u(t)). Example. In the above Newton's model of cooling the derivatives of the composed function are f (i) = (-c) i f(u) provided f(u) = c(u sur - u) with u sur equal to a constant. Example. In the calculations below we consider the second order Taylor method, but for more general f(t, u(t)). u k+1 = u k + f(t k, u k ) h + f (1) ( t k, u k ) h 2 /2 = u k + f(t k, u k ) h + (f t (t k, u k ) + f u (t k, u k ) f(t k, u k )) h 2 /2.
5 4.4 Runge-Kutta Numerical Method. The Taylor method for more accurate numerical solutions of ordinary differential equations requires the computation of a number of partial derivatives of f(t,u), and this made its implementation difficult for higher order methods. Here we will show how one can make use a number of function evaluations of just f(t,u) and still obtain higher order methods based on the Taylor method. The second order Taylor algorithm is u k+1 = u k + h [f(t k, u k ) + (f t (t k, u k ) + f u (t k, u k ) f (t k, u k ))h/2]. In order to avoid some of the partial derivatives, we may evaluate f(t,u) at a number of points. The location of these points can be found by using the two variable version of Taylor polynomials for f(x,y). The derivation can be done via the one variable problem where F(t) = f(a+th,b+tk). Use the extended mean value theorem F(0+1) = F(0) + F (1) (0)(1-0) + F (2) (c)(1-0) 2 /2 where c is in [0,1]. The first and second order derivatives of F(t) must be computed via the chain rule: F (1) = f x h + f y k = (D x h + D y k)f F (2) = (D x h + D y k) 2 f = (D x h + D y k)f x h + (D x h + D y k)f y k = f xx hh + 2f xy hk + f yy kk. Therefore, the second order Taylor polynomial with error in two variables is f(a+h,b+k) = f(a,b) + f x (a,b) h + f y (a,b) k + ( f xx (a+ch,b+ck)hh + 2f xy (a+ch,b+ck)hk + f yy (a+ch,b+ck)kk)/2.
6 This multilinear approximation of a function of two variables is the model, which allows us to simplify the Taylor method for the numerical solution of ordinary differential equations. Consider the above second order Taylor method. We want to replace the expression in the square brackets with a suitable evaluation of f at f(t k + α, u k + β). The α and β can be chosen via the second order Taylor polynomial error in two variables with a = t k, h = α, b = u k and k = β f(t k + α, u k + β) = f(t k,u k ) + f t (t k,u k ) α + f u (t k,u k ) β + O(α 2,αβ,β 2 ). In order to match this with the terms in the square bracket in the second order Taylor algorithm, let α = h/2 and β = f(t k,u k ) h/2 so that a variation of this method is u k+1 = u k + h [f(t k + h/2, u k + f(t k,u k ) h/2)]. This is known as the midpoint method or the second order Runge-Kutta method. The fourth order Runge-Kutta method can be formulated using a similar construction. It also can be viewed as Simpson's rule numerical approximation of the equivalent integral equation t+ h ut ( + h) = ut ( ) + fzuz (, ( ))dz. The integral can be approximated by (h/6)(k 1 + 4(k 2 /2 + k 3 /2) + k 4 ) where k 1 = f(t,u(t)), k 2 = f(t+h/2, u(t) + (h/2)k 1 ), k 3 = f(t+h/2, u(t) + (h/2)k 2 ) and t
7 k 4 = f(t+h, u(t) + hk 3 ). The fourth order Runge-Kutta method, where t and u(t) are replaced by t k and u k, is u k+1 = u k + (h/6)(k 1 + 2k 2 + 2k 3 + k 4 ) In order to show this scheme is fourth order convergent, that is, u(t+h) - u k+1 Ah 5, one must use the Taylor expansions, and in this case the solution should have continuous derivatives of order five. 4.5 Matlab Implementation. The following Matlab code for the Runge-Kutta fourth order method uses fixed step sizes. The logistic differential equation is solved where c =.01, M = 100 and x(0) = 10. Notice the solution approaches 100. The steady state solutions are defined by any constant where the right side is zero. This requires cx(m - x) = 0, and so, x = 0 or x = M. The right side of the differential equation is stored is the function file fpop.m, and the Runge-Kutta code is stored in the file rk4.m. This initial population is stored in x(1), and the values for c = 0.01 and M = 100 are in fpop.m. function fpop = fpop(t,x) fpop =.01*(100 - x)*x; %your name, your student number, lesson number clear; x(1) = 10.; T = 10; KK = 50 h = T/KK; t(1)= 0.; for k = 1:KK k1 = h*fpop(t(k),x(k)); k2 = h*fpop(t(k)+.5*h,x(k)+.5*k1); k3 = h*fpop(t(k)+.5*h,x(k)+.5*k2);
8 k4 = h*fpop(t(k)+h,x(k)+.5*k3); x(k+1) = x(k) + (k1 + 2*k2 + 2*k3 +k4)/6; t(k+1) = t(k) + h; end plot(t,x) title('your name, your student number, lesson number') xlabel('time') ylabel('population') 100 your name, your student number, lesson number 90 80 70 population 60 50 40 30 20 10 0 1 2 3 4 5 6 7 8 9 10 time 6.5 Numerical Experiments. 4.6 Numerical Experiments. In the following numerical experiments we keep c = 0.01 and M = 100 fixed, and vary the initial population. We have executed rk4.m with a number of initial conditions, stored the output, and plotted then all on the same graph. Notice if x(0) is positive, then the solution will always converge to the largest steady state solution. Unless x(0) = 0, the smallest steady state solution, the x(t) always goes away from the smallest steady state
9 solution. Here the largest steady state solution is called stable, and the smallest steady state solution is call unstable. Use the hold on command and six different initial populations 1, 30, 60, 90, 110 and 140. These should be stored in the array x of the rk4.m file. This is, execute rk4.m six times with different x(1) (x(1) = 1,, x(1) = 140). Note the abuse of notation: if x = x(t) is a function of t, then x(1) is a function evaluation at t = 1; if x is an array, then x(1) is the first entry in the array. 140 120 populations for different x(0) 100 80 60 40 20 0 0 1 2 3 4 5 6 7 8 9 10 time
10 4.7 Additional Calculations. Next we fix the initial condition x(0) = 10, M = 100*(1+.S), and vary the c. You may want to increase the final time from T = 10 to T = 20 or larger. (a). (b). State the logistic differential equation for c = 0.005 and M = 100*(1+.S). Modify the pop.m and rk4.m files. (c). Execute the rk4.m file for c = 0.005. (d). Use the hold on command and repeat (c) for c = 0.01, 0.015 and 0.02. (e). Examine the curves. What happens to the population as c increases?