23
Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps at t = 0.5 are, respectively, y(end) = 8.1548, y5(end) = 7.4650 and y50(end) = 8.0748. The geometric reason the Euler approximations in this case are all underestimating the exact solution is that the solution y = 3e 2t is a convex function, that is it has a convex graph (positive second derivative), meaning the tangent line at any point lies fully under the graph of the function. SEE THE PICTURE:

Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

  • Upload
    others

  • View
    23

  • Download
    5

Embed Size (px)

Citation preview

Page 1: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

Matlab Lab 2 Solutions  

Problem 1 

a.  The  exact  value  and  the  corresponding  Euler  approximations  using  5  and  50  steps  at  t  =  0.5  are, respectively,  y(end)  =  8.1548,  y5(end)  =  7.4650  and  y50(end)  =  8.0748.  The  geometric  reason  the  Euler approximations in this case are all underestimating the exact solution is that the solution y = 3e2t is a convex function, that is it has a convex graph (positive second derivative), meaning the tangent line at any point lies fully under the graph of the function. SEE THE PICTURE:   

                                 

Page 2: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

b. e5=y(end)‐y5(end) ≈ 0.6899 and e50=y(end)‐y50(end) ≈ 0.0801 result in a ratio e5/e50 ≈ 8.6148. We clearly see that e50 is (over  8  times)  smaller  than  e5,  so  that  taking  smaller  step  sizes  helps  indeed  reduce  the  error.  Since  the  error  for  Euler’s method is proportional to the step size h (for h sufficiently small), we expect that the ratio of the errors corresponding to two different step sizes h1 and h2, say, should be equal to the ratio of the step sizes, i.e. e1/e2 = h1/h2: if e1 = Ch1 and e2 = Ch2 (where the constant C is independent of h), then e1/e2 = h1/h2 indeed. In our case the ratio of the step sizes is 10 compared to a ratio of the errors ≈ 8.6, suggesting that the step sizes involved are not yet sufficiently small. If we find the Euler approximations using, for example, 500 and 1,000 steps, y500 and y1000 say, we find e500/e1000 ≈ 1.9982, almost exactly equal to the corresponding ratio h1/h2 = (0.5/500)/ (0.5/1000) = 2. c. We find the Euler approximations for 500 and 5000 steps using the Matlab commands  >> [t500,y500]=euler(f,[0,0.5],3,500) ; >> [t5000,y5000]=euler(f,[0,0.5],3,5000); the values of the approximations at t = 0.5 are y500(end) ≈ 8.1467 and y5000(end) ≈ 8.1540, and we find the corresponding errors using the commands >> e500=y(end)‐y500(end) >> e5000=y(end)‐y5000(end) We obtain e500 ≈ 0.0081 and e5000 ≈ 8.1534e‐04 = 0.00081534,  resulting  in a  ratio e500/e5000 ≈ 9.9835, almost exactly equal to the ratio of the step sizes, namely 10.  

Problem 2 

a. The direction field generated by dfield7.m looks as follows: 

                                                                           

Page 3: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

We see that the arrow seem to indicate that, as t gets large, the solutions will approach 0. We include the graph of the solution corresponding to the given initial condition. b. The given ODE is separable and we can solve it readily:  dy/dt = ‐2y is equivalent to dy/y = ‐2 dt, leading to ln y = ‐2t + C or, equivalently, y = K exp(‐2t). Using the initial condition we find y(0) = K exp(0) = K =3, so that the (unique) solution of this IVP is y = 3 exp(‐2t). c. The following modified M‐file, lab2_prob2c.m, does what is required:  function lab2_prob2c clc %clears the screen close all %closes all previous windows N1=4; N2=8; N3=16; N4=32; N5=64; N6=128; y0=3; % initial condition [t1,y1]=euler(@f,[0,10],y0,N1); % Euler with N=N1 [t2,y2]=euler(@f,[0,10],y0,N2); % Euler with N=N2 [t3,y3]=euler(@f,[0,10],y0,N3); % Euler with N=N1 [t4,y4]=euler(@f,[0,10],y0,N4); % Euler with N=N2 [t5,y5]=euler(@f,[0,10],y0,N5); % Euler with N=N1 [t6,y6]=euler(@f,[0,10],y0,N6); % Euler with N=N2 t=linspace(0,10,2000); % t-array used to plot the exact solution y=3*exp(-2*t); % exact solution figure(1) % In figure(1) we plot six subplots: one each for Euler with % N=Nk, k=2,3,4,5,6,7 (in red) together with the exact solution (in black).

Page 4: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

% Type: "help subplot" on the command window if needed. subplot(6,1,1); plot(t1,y1,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('Euler N=',num2str(N1)), 'Exact',2) subplot(6,1,2); plot(t2,y2,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3' legend(strcat('Euler N=',num2str(N2)),'Exact',2) subplot(6,1,3); plot(t3,y3,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('Euler N=',num2str(N3)), 'Exact',2) subplot(6,1,4); plot(t4,y4,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('Euler N=',num2str(N4)),'Exact',2) subplot(6,1,5); plot(t5,y5,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('Euler N=',num2str(N5)), 'Exact',2) subplot(6,1,6); plot(t6,y6,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('Euler N=',num2str(N6)),'Exact',2) figure(2) % in figure(2) we plot the direction field of y' = -2y together with % the Euler approximation for N=N2 (in red) and the exact solution (in % black). Make sure dirfield7.m is in the directory you are working in. % Note how the Euler's approximation follows the direction of the slopes. dirfield7(@f,[0,10],[-80,80],'line'); % viewing window matched to output. title('Direction field of y''=-2 y'); hold on plot(t2,y2,'ro-',t,y,'k-','LineWidth',3); axis normal; hold off %=============================================================== function dydt=f(t,y) %defines the differential equation dydt=-2*y;  

Page 5: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

Next is shown the resulting figure with 6 subplots corresponding to N = 2k, k = 2,3,4,5,6,7.  

  Here is the direction field picture, including the Euler approximation with N2 = 8 steps. This small value of N results in a very poor, oscillatory approximation because the red segments representing the Euler approximation follow the direction field for too long (i.e. each red segment is long) so that each red segment ends on a direction line very different from the one where it started. This precludes the possibility of generating a smooth approximation. 

Page 6: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

  Next is the direction field picture, including the Euler approximation with N6 = 128 steps. This larger value of N results in a good  approximation  because  the  red  segments  that  represent  the  Euler  approximation  are  now  short  enough  that  their endpoints lie on direction lines with very similar direction to that of their starting points. This makes the union of those red segments  look  smooth.  We  also  note  that,  since  the  function  being  approximated  is  convex,  the  Euler  approximations underestimate the exact values (the t‐interval [0,2] was chosen to magnify the effect of the convexity).  

Page 7: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

 Problem 3 

a. The Matlab code to generate the Improved Euler approximations for N=50, 500 and 5000 for the given ODE:  >> f=inline('2*y','t','y'); >> [t50,y50] = impeuler(f,[0,.5],3,50);  >> [t500,y500] = impeuler(f,[0,.5],3,500); 

Page 8: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

>> [t5000,y5000] = impeuler(f,[0,.5],3,5000); >> t = linspace(0,.5,100); y = 3*exp(2*t);  To retrieve the value of each approximation we want more decimal places than we have been obtaining. We use the command >> format long; and then >> y50(end) >> y500(end) >> y5000(end) yield the values  8.154309936213517, 8.154840056963607 and  8.154845431019643, respectively. b. To compute the errors in these approximations we use the Matlab commands: >> e50 = y(end)‐y50(end) >> e500 = y(end)‐y500(end) >> e5000 = y(end)‐y5000(end)  that yield, respectively,  e50 = 5.355491636205301e‐04,  e500 =5.428413530239595e‐06, and  e5000 =5.435749450555249e‐08. The ratios of the errors are computed using the Matlab commands >> R1=e50/e500 >> R2=e500/e5000 that give the values R1 = 98.656662878977897 R2 = 99.865043074880788. We see that ratios of the numbers of steps used are both 10 (500/50 and 5,000/500). The ratios of the errors are almost the square of 10,  i.e. 100,  though slightly  smaller. These  ratios  indeed seem  to  confirm  the  theoretical  result  that  says  that  the error in the improved Euler approximation, for sufficiently small h, is proportional to h2, or, equivalently, the ratio of the errors equals the square of the ratio of the step sizes: if e1= Ch12 e2= Ch22 (where the constant C is independent of h), then we have e1/e2 = h12/ h22 = (h1/ h2)2 indeed. In our case, that is [(1/50)/(1/500)]2 = (500/50) 2 = 102 = 100.  

Problem 4 

Page 9: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

a. The following modification of the M‐file lab2_prob2c.m does what is required:  function lab2_prob4 clc %clears the screen close all %closes all previous windows N1=4; N2=8; N3=16; N4=32; N5=64; N6=128; y0=3; % initial condition [t1,y1]=impeuler(@f,[0,10],y0,N1); % improved Euler with N=N1 [t2,y2]=impeuler(@f,[0,10],y0,N2); % improved Euler with N=N2 [t3,y3]=impeuler(@f,[0,10],y0,N3); % improved Euler with N=N1 [t4,y4]=impeuler(@f,[0,10],y0,N4); % improved Euler with N=N2 [t5,y5]=impeuler(@f,[0,10],y0,N5); % improved Euler with N=N1 [t6,y6]=impeuler(@f,[0,10],y0,N6); % improved Euler with N=N2 t=linspace(0,10,2000); % t-array used to plot the exact solution y=3*exp(-2*t); % exact solution figure(1) % In figure(1) we plot six subplots: one each for improved Euler with % N=Nk, k=2,3,4,5,6,7 (in red) together with the exact solution (in black). subplot(6,1,1); plot(t1,y1,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('improved Euler N=',num2str(N1)),'Exact',2) subplot(6,1,2); plot(t2,y2,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('improved Euler N=',num2str(N2)),'Exact',2)

Page 10: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

subplot(6,1,3); plot(t3,y3,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('improved Euler N=',num2str(N3)),'Exact',2) subplot(6,1,4); plot(t4,y4,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('improved Euler N=',num2str(N4)),'Exact',2) subplot(6,1,5); plot(t5,y5,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('improved Euler N=',num2str(N5)),'Exact',2) subplot(6,1,6); plot(t6,y6,'ro-',t,y,'k-'); title('y''= - 2y, y(0)=3') legend(strcat('improved Euler N=',num2str(N6)),'Exact',2) figure(2) % in figure(2) we plot the direction field of y' = -2y together with % the improved Euler approximation for N=N2 (in red) and the exact solution % in black). Make sure dirfield7.m is in the directory you are working in. dirfield7(@f,[0,4],[0,15],'line'); % viewing window matched to output. title('Direction field of y''=-2 y'); hold on plot(t2,y2,'ro-',t,y,'k-','LineWidth',3); axis normal; hold off %=============================================================== function dydt=f(t,y) %defines the differential equation dydt=-2*y;  Next is shown the resulting figure with 6 subplots corresponding to N = 2k, k = 2,3,4,5,6,7. We see that the first 2 approxima‐tions have too few steps to be able to follow the exact solution and, indeed, they diverge from it. In contrast, the last 4 approxi‐mations seem to converge very rapidly toward the exact solution.    

Page 11: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

Here is the direction field picture, including the improved Euler approximation with N2 = 8 steps. This small value of N results in  a  very poor  approximation because  the  red  segments  representing  the  improved Euler  approximation  follow  the wrong direction for too long, and the initial direction is very far the correct one.   

Page 12: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

 The  situation  changes  visibly  as  soon  as  we  double  the  number  of  steps  to  16,  as  seen  in  the  following  picture.  The approximate solution obtained from the improved Euler method is not too good yet (the step size is still too big), but we now see that the initial direction (that of the first red segment) is not too far off from the correct one (that of the tangent line to the black curve at the initial point), and the approximation gets closer to the exact solution as they both approach 0.  

Page 13: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

 We include finally a picture of the direction field including the improved Euler approximation using 128 steps (i.e. using a step of size h = (0.5 – 0)/128 = 1/256 ≈ 0.0039). The approximation in this case is so accurate that becomes indistinguishable from the exact solution. We show the picture with the independent variable t restricted to the interval [0,4] so that the interesting part  of  the  graph  (i.e.  where  the  solution  and  the  approximation  are  not  too  close  to  0)  is  not  compressed  too  much horizontally and we can see better detail.   

Page 14: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

 In order  to  compare  the Euler  and  the  improved Euler  approximations with  the  same  step  size, we  include pictures of  the direction field and both approximations, using N = 8, N = 16, and N=128 steps.  

Page 15: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

  

                                                      Problem 5 

a. The direction field for this ODE is shown below, together with the solution corresponding to the initial condition y(0) = 2. 

Page 16: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

  Looking  at  the direction  field, we  surmise  that  as  t  gets  larger  solutions diverge  to ∞ or  to  ‐∞. This  behavior  seems  to  be confirmed for the solution corresponding to the initial condition y(0) = 2. b. The differential equation is linear:  y’ – 2y = ‐12e‐2t. An integrating factor is e‐2t and, multiplying the ODE by it, we obtain d/dt[e‐2t y] = e‐2t (‐12e‐2t) = ‐12e‐4t. Integrating, we see that e‐2t y = 3e‐4t + C or, equivalently,  y = 3e‐2t + C e2t . Using the initial condition we see that  2 = 3 + C, which gives  C = ‐1.  Therefore, the (unique, since the right‐hand‐side of the ODE is continuous 

Page 17: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

and differentiable for all t and y) solution of the IVP is y = 3e‐2t ‐ e2t. We readily see that, as t becomes very large negative, y goes to ∞ and, as t becomes very large positive, y goes to ‐∞. c. The following modification of the M‐file lab2_prob2c.m does what is required:  function lab2_prob5 clc %clears the screen close all %closes all previous windows N1=4; N2=8; N3=16; N4=32; N5=64; N6=128; y0=2; % initial condition [t1,y1]=euler(@f,[0,10],y0,N1); % Euler with N=N1 [t2,y2]=euler(@f,[0,10],y0,N2); % Euler with N=N2 [t3,y3]=euler(@f,[0,10],y0,N3); % Euler with N=N3 [t4,y4]=euler(@f,[0,10],y0,N4); % Euler with N=N4 [t5,y5]=euler(@f,[0,10],y0,N5); % Euler with N=N5 [t6,y6]=euler(@f,[0,10],y0,N6); % Euler with N=N6 t=linspace(0,10,2000); % t-array used to plot the exact solution y=3*exp(-2*t)-exp(2*t); % exact solution figure(1) % In figure(1) we plot six subplots: one each for Euler with N=Nk, % k=2,3,4,5,6,7 (in red) together with the exact solution (in black). subplot(6,1,1); plot(t1,y1,'ro-',t,y,'k-'); title('y''= 2y – 12 exp(-2t), y(0)=2') legend(strcat('Euler N=',num2str(N1)),'Exact',2) subplot(6,1,2); plot(t2,y2,'ro-',t,y,'k-');

Page 18: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

title('y''= 2y – 12 exp(-2t), y(0)=2') legend(strcat('Euler N=',num2str(N2)),'Exact',2) subplot(6,1,3); plot(t3,y3,'ro-',t,y,'k-'); title('y''= 2y – 12 exp(-2t), y(0)=2') legend(strcat('Euler N=',num2str(N3)),'Exact',2) subplot(6,1,4); plot(t4,y4,'ro-',t,y,'k-'); title('y''= 2y – 12 exp(-2t), y(0)=2') legend(strcat('Euler N=',num2str(N4)),'Exact',2) subplot(6,1,5); plot(t5,y5,'ro-',t,y,'k-'); title('y''= 2y – 12 exp(-2t), y(0)=2') legend(strcat('Euler N=',num2str(N5)),'Exact',2) subplot(6,1,6); plot(t6,y6,'ro-',t,y,'k-'); title('y''= 2y – 12 exp(-2t), y(0)=2') legend(strcat('Euler N=',num2str(N6)),'Exact',2) figure(2) % in figure(2) we plot the direction field of y' = -2y together with % the Euler approximation for N=N2 (in red) and the exact solution % in black). Make sure dirfield7.m is in the directory you are working in. dirfield7(@f,[0,10],[-5000,5],'line'); % viewing window matched to output. title('Direction field of y''= 2 y – 12 exp(-2t)'); hold on plot(t6,y6,'ro-',t,y,'k-','LineWidth',3); axis normal; hold off %=============================================================== function dydt=f(t,y) %defines the differential equation dydt=2*y-12*exp(-2*t);  Next  is shown the resulting  figure with 6 subplots corresponding  to N = 2k, k = 2,3,4,5,6,7. Note  that  the vertical scale  is  in billions and thus the graph seems to show the exact solution as zero for t in the interval [0,8], and the approximations seem to be zero for t in the interval [0,10], with the exception of the last one.   

Page 19: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

The 

 We show next the graphs of the Euler approximations using the same number of steps but for the time interval [0,5], so that the values of the exact solution and its approximations are not so large.  

Page 20: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

   d. Next is the figure that includes 6 subplots for the approximation of the solution of the same IVP, but now using the improved Euler method with 4, 8, 16, 32, 64, and 128 steps.   

Page 21: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps

 e. We can readily see how much better the improved Euler approximations are than those produced using Euler’s method with the same number of steps. The most striking difference with problem 2 is that the solution of the IVP there was bounded and approached 0 as t increased, while that of the IVP in this problem becomes unbounded as t increases (it goes to ‐∞). The Euler method has much more difficulty approximating values that decrease unboundedly than values that decrease towards a finite limit. Next, we show two pictures of the Euler and improved Euler approximations, using 8 and 64 steps respectively, together with the exact solution on each picture. The second one is restricted to the time interval [0,5] so that the values of the solution and its approximations do not become too large in magnitude (they are negative). 

Page 22: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps
Page 23: Matlab Lab 2 Solutions - Arizona State University · 2009-09-19 · Matlab Lab 2 Solutions Problem 1 a. The exact value and the corresponding Euler approximations using 5 and 50 steps