Examples:
Using a hand calculator, solve the ODE from t=0 to t=3 using Euler's Method with a step size h=1. Then solve again using a step size h=0.5. Compare the error at t=3 in each case to the exact solution of .
h=1
Step h Error 1 1 0 1 4 8 12 29.556 146% 2 1 1 2 12 24 36 218.39 506% 3 1 2 3 36 72 108 1614.7 1394% h=0.5
Step h Error 1 0.5 0 0.5 4 8 8 2 0.5 0.5 1 8 16 16 29.556 85% 3 0.5 1 1.5 16 32 32 4 0.5 1.5 2 32 64 64 218.39 241% 5 0.5 2 2.5 64 128 128 6 0.5 2.5 3 128 256 256 1614.7 531% At t=3 and h=0.5 the error is roughly half of the error at t=3 and h=1.0. This is an example with extremely large errors in both cases, but it proves the point that the Euler Method is a first order accurate method. Reducing the error by a factor of two requires twice as much work. Furthermore, even with the aid of computers, if you reduce h to a value that is sufficiently small to satisfy your accuracy requirements, numerical round-off error associated with the finite precision of computer arithmetic will limit the solution accuracy. In other words, the more steps you take, the less accurate your result will be due to inherent round-off errors. Thus there is an incentive to use high order methods that allow larger values of h and require few steps.
Using a hand calculator solve the ODE from t=0 to t=3 using the fourth order Runge-Kutta Method with a step size of h=1. Then solve again using a step size of h=0.5. Compare the error at t=3 in each case to the exact solution of and compare the error to the Euler Method in the previous example. This is a very tedious example to work out, but you should try it. This will be the only time that you have to do these calculations by hand but doing it once will help you to understand the steps.
h=1
Step 1 2 3 h 1 1 1 0 1 2 1 2 3 4 28 196 8 56 392 16 112 784 24 168 1176 56 392 2744 28 196 1372 29.6 218.4 1613.7 Error 5.6% 11% 18% h=0.5
Step 1 2 3 4 5 6 h 0.5 0.5 0.5 0.5 0.5 0.5 0 0.5 1 1.5 2 2.5 0.5 1 1.5 2 2.5 3 4 10.8 29.3 79.5 215.2 582.9 8 21.7 58.7 158.9 430.4 1166 12 32.5 88 238.4 645.6 1749 14 37.9 102.7 278.1 753.2 2040 22 59.6 161.4 437.0 1184 3206 10.8 29.3 79.5 215.2 582.9 1579 29.6 218.4 1613.7 Error 0.7% 1.5% 2.2% Notice that the error with h=0.5 and t=3 is 2.2% whereas it is 531% using the Euler Method. While 2.2% is not very good, 531% is entirely useless as a result. This is why the fourth order Runge-Kutta Method is preferred.
Cutting the step size by two using the Runge-Kutta Method reduces the error from 18% to 2.2% and this is roughly a factor of 24 less. This supports the idea that this algorithm is fourth order.
Doing twice as much work by cutting the step size by two has a very large effect on reducing the error in the Runge-Kutta Method while doing twice as much work in the Euler Method only contributed a factor of two in error reduction. This demonstrates the value of so-called high-order numerical algorithms and shows how extremely accurate results can be obtained using them. In this example the analytic solution was known so the error could be exactly determined. In problems where the exact solution is not known, there are mathematical ways to bound the maximum error and thus choose the value of h to be sufficiently small to get the desired results.
- Compare different ways of computing numeric approximations of the solution to an ODE.
The ODE and the initial condition y(0)=y0 has the analytic solution
Compute the numeric solution from t=0 to 10, using Euler's, Runge-Kutte as described in this topic, and
ode45
when y(0)=4 and alpha=0.1. To see the difference, use a step size of h=2 for Euler's and then repeat for step size of h=0.5.Solution using different numeric methods
Solution to ODE using Euler's and step size h=2 t y exact error % 0 4 4 0 2 4.8 4.8856 1.7523 4 5.76 5.9673 3.4739 6 6.912 7.2885 5.1653 8 8.2944 8.9022 6.8271 10 9.95328 10.8731 8.4598 Solution to ODE using Runge-Kutte and step size h=2 t k1 k2 k3 k4 y exact error % 0 4 4.0000 0% 2 0.4 0.44 0.444 0.48 4.8856 4.8856 0.00022582 4 0.48856 0.53742 0.5423 0.59702 5.9673 5.9673 0.00045164 6 0.59673 0.6564 0.66237 0.7292 7.2884 7.2885 0.00067746 8 0.72884 0.80173 0.80902 0.89065 8.9021 8.9022 0.00090327 10 0.89021 0.97923 0.98813 1.0878 10.873 10.8731 0.0011291 Solution to ODE using ode45 and a timespan vector of times [0:h:10] t y exact error % 0 4 4.0000 0% 2 4.8856 4.8856 .04663e6 % 4 5.9673 5.9672 0.0932e6 % 6 7.2885 7.2884 0.1398e6 % 8 8.9022 8.9021 0.1865e6 % 10 10.8731 10.8731 0.2331e6 % Use these tables to determine if your own computations are correct. Even using the same step size for each algorithm, the error percentage for the Runge-Kutte algorithm (and the version implemented in ode45) are several orders of magnitude better than Euler's method.