Math 32 - Numerical Analysis Homework #4 Due End of term Note: In the following y i is approximation of y(t i ) and f i is f(t i,y i ).. Consider the initial value problem, y = 2y t 3y2 t 3, t 2, y() =. (a) (b) The exact solution to () is y(t) = t 2 +3ln(t). (a) Approximate the solution to () using Euler s method with h =.2 and h =.. Verify that the global error is O(h). Here is my code for Euler s method, function y=euler(f,a,b,h,y) n=(b-a)/h+; % the number of steps we need to take y=zeros(n,); % reserve space for the solution xi=a; % start at a with y(a)=y y()=y; % This is the Euler loop for i=2:n y(i,:)=y(i-)+h*feval(f,xi,y(i-)); xi=xi+h; Here is a graph of the two solutions and a graph of the two error functions. You can see from the graph that the error goes down by a factor of one half when we halve the step size..3.25 Solution line 2 line 3.2.5..5.95.9.8.2.4.6.8 2
.2 Error line 2..8.6.4.2.8.2.4.6.8 2 (b) Approximate the solution to () using Taylor s method of order 2 with h =.2 and h =.. Verify that the global error is O(h 2 ). When we use a Taylor method, we must find the Taylor polynomial for each problem. Since this is a 2 nd order method, we only need to find d dt (f(t,y(t))) = d ( ) 2y dt t 3y2 t 3, ( 2y = 2y ) t t 2 3yy t 3 + 9y2 t 4, ( 2 = t 3y )( ) ( 2y t 3 t 3y2 ) 9y 2 t 3 + t 4 2y ) t 2. We can sub the above expression into the formula for T (2), T (2) (t,y) = f(t,y)+ h d 2 dt f(t,y(t)), ( ) 2y = t 3y2 t 3 + h 2 (( 2 t 3y t 3 )( ) ( 2y t 3y2 9y 2 t 3 + t 4 2y )) t 2. Now we can plug this into the Taylor method. Here is a listing of my code for Taylor s Method of order 2 and the function I pass, function y=tay2(h,t,a,b,y) n=(b-a)/h+; y=zeros(n,); y()=y; t=a; for i=:(n-) y(i+)=y(i)+h*feval(t,t,y(i),h); t=t+h; 2
function f=t3(t,y,h) tmp=2*y/t-3*y^2/t^3; tmp2=tmp*(2/t-6*y/t^3)-2*y/t^2+9*y^2/t^4; f=tmp+h/2*tmp2; Note that I have reused the value of f(t,y) this saves on computations and also makes it easier to debug. Here is a graph of the two solutions obtained using Taylor s method. Note the difference between the second order and first order methods..3.25 line 2 line 3 line 4 line 5 line 6.2.5..5.95.9.8.2.4.6.8 2 Here is a graph of the error for both methods. You can see that the error is reduced by a factor of 4 when we halve the step size..5.45 line 2.4.35.3.25.2.5. 5e-5.8.2.4.6.8 2 3
2. Construct a 3-step method to solve the ODE, of the form, y = f(t,y), y() = α, y(h) = α, y(2h) = α 2, y i+ = y i 2 +b f i 2 +b f i +b 2 f i. Here f i = f(t i,y i ). Construct the method by considering a polynomial interpolating f(t,y(t)) at the points t i 2,t i,t i and using this polynomial to find an approximation of the following form, 3h To construct this method, we will use, f(t,y(t))dt = b f i 2 +b f i +b 2 f i + y(t i+ ) y(t i 2 ) = ti+ t i 2 f(t,y)dt. To form the approximation, we will let P(t) be the quadratic that goes through the points (t i 2,f i 2 ), (t i,f i ) and (t i,f i ). The method will then be given by, ti+ w i+ = w i 2 + P(t)dt. t i 2 All we need to do is find P(t) and evaluate the integral. We construct P(t) using Lagrangian interpolation. In this form P is given by, P(t) = (t t i )(t t i ) f i 2 2h 2 (t t i)(t t i 2 ) f i h 2 +(t t i 2 )(t t i ) f i 2h 2. Now we need to evaluate t i+ t i 2 P(t)dt. To simplify the integration, we will use the substitution x = t t i 2. We then get, ti+ t i 2 P(t)dt = f i 2 2h 2 3h (x 2h)(x h)dx f i h 2 3h x(x 2h)dx+ f i 2h 2 3h x(x h)dx After solving the above integrals and subbing back into the method, we find the approximation is given by, w i+ = w i 2 + h 4 (3f i 2 +9f i ). 3. A chemical reaction is modeled by the differential equation, dx (n dt = K x ) 2 ( n 2 x ) ( 2 n 3 3x ) 3, 2 2 4 where K is the rate constant of the reaction, the n i s are the initial amounts of reactant and x is the amount of product. If K = 6.22 9 M 6 s, n = n 2 = M and n 3 = 5M, how many moles of product have been formed after two seconds? Use both Runge-Kutta 4 th order method with h =. and the Midpoint method with h =.5. Both methods require the same number of function evaluations to approximate x(2). Which method do you think is providing the most accurate solution? Why? Here is a listing of the midpoint code: function y=rk2(f,a,b,y,h) n=(b-a)/h+; y=zeros(n,); t=a; 4
y()=y; for i=2:n K=h*feval(f,t,y(i-)); y(i)=y(i-)+h*feval(f,t+h/2,y(i-)+k/2); t=t+h; Here is the Runge-Kutta 4 th order code: function y=rk4(f,a,b,y,h) n=(b-a)/h+; y=zeros(n,); t=a; y()=y; for i=2:n K=h*feval(f,t,y(i-)); K2=h*feval(f,t+h/2,y(i-)+K/2); K3=h*feval(f,t+h/2,y(i-)+K2/2); K4=h*feval(f,t+h,y(i-)+K3); y(i)=y(i-)+(k+2*k2+2*k3+k4)/6; t=t+h; The code for the function: function f=f3(t,y) K=6.22e-9; n=; n2=; n3=5; f=k*(n-y/2)^2*(n2-y/2)^2*(n3-3*y/4)^3; The output from Octave: octave:29> y=rk2( f3,,2,,.5); octave:3> y2=rk4( f3,,2,,.); octave:3> t=:.5:2; octave:32> t2=:.:2; octave:33> plot(t,y); octave:34> hold on octave:35> plot(t2,y2); octave:36> y(max(size(y))) ans = 74.93479237355 octave:37> y2(max(size(y2))) ans = 75.42269255352 octave:38> diary off So using the Midpoint method, we have x(2) 74.9348. Using the 4 th order Runge-Kutta method, we get x(2) 75.4227. 5
8 line 2 7 6 5 4 3 2.5.5 2 The two solution plots from Octave. The difference between the two approximations cannot be seen from the graph. Now which approximation is more accurate? We have a higher order method with a larger step size and a lower order method with a smaller step size. It is not possible to say for certain which approximation is more accurate. However, since The midpoint method is O(h 2 ), we do know that the error is given by, The error for the 4 th order Runge-Kutta method is given by, e = C h 2 = C (.5) 2 = C (.25). (2) e = C 2 h 4 = C 2 (.) 4 = C 2 (.). (3) We do not know the constants C and C 2. We do know that if C 2 < 25C then the error for the 4 th order method will be smaller. This is very likely. 4. The family of implicit multi-step methods, know as backward difference formula or BDF methods, is given by, w = α, w = α,,w m = α m, w i+ = a w i +a 2 w i + +a m w i (m ) +hb f i+. To make an m-step BDF method, we construct a polynomial, P(t), which interpolates the points (t i+,y i+ ), (t i,y i ),...,(t i (m ),y i (m ) ). We then use the approximation, P (t i+ ) = f i+. Solving for w i+ from P (t i+ ) = f i+ gives us our method. These implicit methods have very nice stability properties as we will see later. Find the second order BDF method by constructing the polynomial P(t) which interpolates (t i+,y i+ ), (t i,y i ) and (t i,y i ) and equating P (t i+ ) = f i+. We begin by constructing the polynomial P(t). We have 3 points, so P(t) will be a quadratic in general. As this step is similar to previous work, I will just give P, P(t) = (t t i+ )(t t i ) w i 2h 2 (t t i+)(t t i ) w i h 2 +(t t i)(t t i ) w i+ 2h 2. Now we differentiate P, Finally, P (t) = (t t i+ +t t i ) w i 2h 2 (t t i+ +t t i ) w i h 2 +(t t i +t t i ) w i+ 2h 2. P (t i+ ) = h w i 2h 2 2hw i h 2 +3hw i+ 2h 2, = w i 2h 2w i h + 3w i+ 2h. 6
Now all we need to do is set P (t i+ ) = f i+ and solve for w i+. f i+ = w i 2h 2w i h + 3w i+ 2h, w i+ = 4 3 w i 3 w i +h 2 3 f i+. The above equation defines the second order BDF method. 5. Find and plot the region of absolute stability for the following methods: 2-step Adams-Bashford This is done in the notes. 2-step Adams-Moulton We solve for hλ and get, Plotting, as in the notes, results in the following stability region. z 2 z hλ = 5 5 z2 + 8 2 z. (4) 2 4 3 2 - -2-3 -4-6 -5-4 -3-2 - We must determine if the region of stability is inside the closed region or outside. If we let z = 2 in (4) then we find hλ = 24 35. This is outside the closed region. Since z = 2 corresponds to an unstable method, the region outside must be the unstable region. Hence the stable region is inside the closed loop. 2-step BDF (Make sure you choose the right region) Using question and solving for hλ, we find, Plotting the region results in, hλ = 3(z2 4 3 z + 3 ) 2z 2. (5) 7
2.5 2.5.5 -.5 - -.5-2 -2.5.5.5 2 2.5 3 3.5 4 Again we must determine which region of the complex plain corresponds to the stable region. Using z = 2 in (5), we find hλ = 5 8. Hence the unstable region is inside the closed curve. It would appear that the 2 nd -order BDF method is A-stable. In the text and in class I have said that the only A-stable methods are Forward Euler and Trapezoidal. Our results seems to contradict this, however if you can zoom in around, you will find that the unstable region does enter the negative half plane slightly above and below on the complex axis. BDF methods (only order 2 to 6) have the entire negative real axis within their stability regions. It is this property which makes them very popular for stiff problems. I will demonstrate how to plot these regions in the notes. 6. Write a subroutine which solves an initial value problem using the two-step Adams-Bashford method. You may use the mid-point subroutine from the last assignment to bootstrap the method. Apply the method to the following problem. y = 2y, y() =, t, (6) with time steps h =. and h =.25. Plot the two solutions and comment on the results. Here is a listing of the subroutine I used to solve this problem. The rk2 refers to the midpoint subroutine from homework 2. A listing of rk2 is included in the solutions to HW2. function y=ab2(f,a,b,y,h) n=(b-a)/h+; % number of steps y=rk2(f,a,a+h,y,h); % I am boot strapping the system % using the midpoint method % this returns a vector with y() and y(2) t=a+2*h; % We start here since the first two time steps are done y=[y ; zeros(n-2,)]; % I am reserving space for the rest of the solution fi=feval(f,a+h,y(2)); % The first function value used in the loop fim=feval(f,a,y()); % The second function value used in the loop for i=2:n- y(i+)=y(i)+h/2*(3*fi-fim); % Next value of y 8
t=t+h; fim=fi; % this is f_i fi=feval(f,t,y(i+)); % f_{i-} Running the above code for our problem with h =. produces the following solution. 6 4 2-2 -4-6 -8 - -2-4.2.4.6.8 The exact solution is given by y = e 2t. As you can see, our approximation is dreadful. This is to be expected. From the first question 2, we can see that if λ is real, < hλ < to be in the stable region. For this problem, λ = 2, so we need h < 2 =.5. With h =. we are well outside the stability region and can expect poor results. If we set h =.25, we are inside the stability region and we should expect reasonable results. The following approximation is quite reasonable..9.8.7.6.5.4.3.2..2.4.6.8 9
We define h max as the largest time step which can be used on this problem which results in an A-stable approximation for a chosen method. Give h max for each of the methods considered in question 5. I have already discussed the Adams-Bashforth method. We just need to look at the stability diagrams found in question 2. For the Addams-Moulton, we will need 2h < 6 or h <.3. This is much better than the Addams-Basho-rd. For the BDF method, we have no restriction. We may set h as large as we like and the only loss of accuracy will be due to truncation error.