
3.8 Numerical Solutions 177
Thus, we are in a situation here where we can compare the approximate solution
of Equations 3.214 and 3.215 derived using the Euler method with the exact
solution (Equation 3.216), which is a standard procedure to test the performance
and accuracy of numerical methods. A comparison of Equations 3.204 and 3.214
shows that we have F(x, y) = y here, which means that Equation 3.213 becomes
y
i
= y
i−1
+ h · y
i−1
, i = 1, 2, ..., n (3.217)
Using Equation 3.215, the entire iteration procedure can be written as follows:
y
0
= 1 (3.218)
y
i
= (1 + h) · y
i−1
, i = 1, 2, ..., n (3.219)
To compute the approximate solution of Equations 3.214 and 3.215 for x ∈ [0, 4],
the following Maxima code can be used (compare
Euler.mac in the book software):
1: h:1;
2: n:4/h;
3: y[0]:1;
4: for i:1 thru n step 1 do
5: y[i]:(1+h)*y[i-1]
;
(3.220)
After the initializations in lines 1 and 2, the remaining lines are in direct
correspondence with the iteration equations (3.218)–(3.219). Note that ‘‘
:’’ serves
as the assignment operator in Maxima [106], that is, a command such as line 1 in
program 3.220 assigns a value to a variable (
h receives the value 1 in this case).
Many other programming languages including R (Appendix B) use the equality
sign ‘‘ = ’’ as assignment operator. In Maxima,theequalitysign‘‘= ’’ serves as
the equation operator, that is, it defines unevaluated equations that can then, for
example, be used as an argument of the
solve command (line 4 of program 1.7 in
Section 1.5.2). The
for...thru command in line 4 iterates the command in line
5fori = 1, 2, ..., n, that is, that command is repeatedly executed beginning with
i = 1, then i = 2, and so on. See [106] for several other possibilities to formulate
iterations using Maxima’s
do command.
The code (3.220) produces the y
i
values shown in Table 3.1. In the table,
these values are compared with the corresponding values of the exact solution of
Equations 3.214 and 3.215 (Equation 3.216). As can be seen, the y
i
values deviate
substantially from the exact values, e
x
i
. This is due to the fact that a ‘‘small h’’ was
assumed in the derivation of the Euler method above. Better approximations are
achieved as h is decreased toward zero. This is illustrated in Figure 3.7a. The line
in the figure is the exact solution, and it is compared with the result of program
3.220 obtained for h = 1(points)andh = 0.25 (triangles). As can be seen, the
approximation obtained for h = 0.25 is much closer to the exact solution. Still
better approximations are obtained as h is further decreased toward zero – test this
yourself using the code (3.220).