Section 4. Approximate Solutions
Looping constructs and user defined procedures are featured in this set of exercises.
0. Pull down the Help menu, choose New Users/Full Tour, then click on item ten: Programming and work through the examples of Procedures, Control Flow statements, and Local Variables. You can safely ignore the discussion of Modules but might like to read about how to see the source code for Maple's routines. You will also learn that Maple can be used to generate code compatible with Visual Basic, MATLAB, Fortran, C, and Java as well as a typesetting language called LaTeX.
Open a new Maple worksheet and do the following.
1. Create the procedure called Euler defined on in Part 3, Section 4 of the manual. Test it on the differential equation y' = cos(t) y with the initial condition y(0) = 1 (as in the manual).
2. Use Euler to obtain the list L1 of Euler approximations to the initial value problem (IVP)
,
.
for t = 0, 0.2, 0.4, 0.6, 0.8, 1.0. (I.e. h = 0.2). Plot these points and the line segments connecting them along with the actual solution. Name the plot P1 by entering the following
plot( [L1,rhs(soln),L1], t=0..1, y=0..2, style=[line$2,point], color=[red,blue,black]); P1 := %:
Be sure to terminate the definition of P1 with a colon to suppress the output.
Note. You will have to create the solution using dsolve.
3. a. Make a similar plot named P2, displaying the solution and the approximation for h = 0.1. (Call the list of approximations L2 to distinguish it from the list in Exercise 2.)
b. Use the display procedure to display plots P1 and P2 together. Enter the following
plots[display]( P1, P2 );
Comment on the error displayed in the picture.
c. If you have the Ledder text, compare the plot displayed in part b to the one displayed in Figure 2.5.7.
4. a. (Continuing 3) Use unapply to make rhs(soln) into a function g. Then display the entries in list L1 and half of the entries in L2 (the ones corresponding to the same t values) as well as the actual solution values in a Matrix as follows:
Matrix( [ [t,0.2*k$k=0..5], ['h=0.2',evalf[4]('L1[k,2]'$k=1..6)],
['h=0.1',evalf[4]('L2[2*k+1,2]'$k=0..5)],
['g(t)',evalf[4](g(0.2*k)$k=0..5)] ] );
Compare the numbers in this Matrix with the numbers displayed in Table 2.5.2 in Ledder.
b. Use your calculator to check that the Error has been cut in half (approximately).
5. Create the procedure called ModEuler and test it on y' = cos(t) y with the initial condition y(0) = 1 (as in the manual).
6. Repeat 2 - 5 using ModEuler in place of Euler. Comment on the graphs and the reduction of error in the Matrix.
Hint. Use Copy and Paste, then make minor changes in the code.
7.* Modify the ModEuler procedure to make a procedure that implements the classical Runge-Kutta algorithm (see Ledder, Section 2.6). Test in by applying it to the problems described in Exercises 2 - 6. (Copy and Paste).