Dr. Kevin G. TeBeest
Assoc. Prof. of Applied Mathematics
Kettering University

Maple Script for the (explicit) Euler Method to Solve a 1st-order Initial Value Problem:

y '   =   f ( x , y ) ,

y ( x0)   =   y0 ,

on an interval [ a , b ] (denote x0 by a ) using "Nsteps" steps.

User must enter: function f(x,y), Nsteps, a, b, y0 below.
This code is set up to use equally spaced nodes (constant stepsize h).


> restart ;    (This clears Maple's memory. Always best to make this the first line in a Maple code.)

> Digits := 16 ;    (Sets the precision of Maple. This should be an integer.)

Digits := 16

> f := ( x, y ) –> evalf( y * cos(x) ) ;
    (This defines f as a function of x and y. You may use any variable names you want, but it's best not to use names you've used elsewhere.)

f := proc (x, y) options operator, arrow; y*cos(x) ...

> a := 0.0 ;   b := 2.0 ;    (We are solving the problem on the interval [0.0, 2.0]. a is x0 from the initial condition.)

a := 0.

b := 2.0

> y0 := 1.0 ;

y0 := 1.0

> Nsteps := 10 ;    (We are using 10 steps. This should be an integer.)

Nsteps := 10


Euler's method follows:

> h := (b - a) / Nsteps ;    (Determine (set) the nodal stepsize.)

h := .2000000000000000

> X[0] := a ;   Y[0] := y0 ;
    ( Use square brackets, not parentheses. Square brackets are used to denote elements of a vector or elements of a sequence. )


> printf("\n                x                   y\n") :    (after each line in this block, hit Shift-Enter)
    printf("-----------------------------------------------\n") :

    printf("  %3d  %18.12f  %18.12f\n", 0, X[0], Y[0] ) :    (This prints the initial value.)
    for n from 0 to (Nsteps–1) do
          X[n+1]   :=   a + (n+1) * h :
          Y[n+1]   :=   Y[n] + h * f( X[n], Y[n] ) :
          printf("  %3d  %18.12f  %18.12f\n", n+1, X[n+1], Y[n+1] ) :
    od:



   n             x                   y
-----------------------------------------------
    0      0.000000000000      1.000000000000
    1      0.200000000000      1.200000000000
    2      0.400000000000      1.435215978682
    3      0.600000000000      1.699600269869
    4      0.800000000000      1.980148396635
    5      1.000000000000      2.256064931323
    6      1.200000000000      2.499856348239
    7      1.400000000000      2.681024814812
    8      1.600000000000      2.772162040375
    9      1.800000000000      2.755972878911
   10      2.000000000000      2.630740316710

> plot( [ [ X[k], Y[k] ]$k = 0 .. Nsteps ], a .. b, 0.0 .. 3.0, style = point, symbolsize = 30, symbol = diamond ) ;

[Maple Plot]

> plot( [ [ X[k], Y[k] ]$k = 0 .. Nsteps ], a .. b, 0.0 .. 3.0, style = line, thickness = 4 ) ;

[Maple Plot]

Comments:
  1. To plot a set of points, use square brackets (not parentheses).
  2. Square brackets are used in Maple to denote a sequence or an element of a vector (which is a sequence).
  3. The phrase 0.0 .. 3.0 in the plot command restricts the vertical range. You should change that depending on the results you are plotting.
    Change the vertical plot range to 0.0 .. 10.0. Do you see why that would not be an appropriate vertical range?
  4. printf   →  Here we're using the printf command which allows us to use formatted printing.
    1. Use double quotes.
    2. The string \n inside double quotes terminates a line and starts a new line. To insert a blank line, use \n\n.
    3. The string %3d prints an integer within 3 character widths.
    4. The string %18.12f prints a number in floating point (decimal) form inside 18 character widths and 12 decimal places. In general it takes the form %w.df where d is the number of decimal places to print and w is the total character widths. (Choose w large enough to ensure that you print all digits and any possible negative sign.)
    5. Note: To print a number in scientific notation, use for example, %13.6e, where the format is always %w.de where d is the number of decimal places to print and w is the total character width to be used. (In this case always choose wd + 7.)

    CLICK HERE for more information on the printf command and to learn more about plotting options.


Created by
Dr. Kevin G. TeBeest
Associate Professor of Applied Mathematics
ktebeest@kettering.edu