0

I am solving this particular ODE:

$$ \begin{cases} u'(t)=-2\,tu^2/20, \quad t \in [0,\sqrt{20}]\\ u(0) = 1 \end{cases} $$ which the analytical solution is given: $$ u(t) = \frac{1}{1 + t^2/20}. $$

Furthermore, I am evaluating the expression at different step sizes, as such:

$$n=2^k, h=\sqrt{20}/2^k, 0 \le k \le 9$$

I then calculate the value for $u(t=\sqrt{20})$ and compare it to the value of the approximate function given by the Euler method at the same point. I expected the error to be halved at each iteration. However, surprisingly, the error at $n=2$ is in the order of $1\cdot10^{-16}$. After that, the error behaves as expected. My results are shown in the table below:

n u Euler u Exact Error Ratio
1 1.0 0.5 0.5 -
2 0.4999999999999999 0.5 1.1102230246251565e-16 4503599627370496.0
4 0.5083560943603516 0.5 0.008356094360351562 1.328638687821671e-14
8 0.5045486124686074 0.5 0.0045486124686073826 1.83706447142328
16 0.5022642864188357 0.5 0.0022642864188356926 2.0088503074387125
32 0.5011213922644928 0.5 0.0011213922644928154 2.0191742805179653
64 0.5005571541057451 0.5 0.0005571541057450835 2.012714710220388
128 0.5002775936902161 0.5 0.0002775936902160714 2.0070849064019067
256 0.5001385392778465 0.5 0.00013853927784646114 2.0037183283409346
512 0.5000692038113869 0.5 6.920381138686427e-05 2.0019024251713047

Why is this happening at $n=2$?

cascavelho
  • 11
  • 1

2 Answers2

1

This seems to be only a coincidence of numbers since Euler's method gives out the exact solution for $n=2$. The Python output is only not exact due to floating point rounding errors $$ k=1, \quad n=2^{k}=2, \quad h=\frac{\sqrt{20}}{2^{k}}=\frac{\sqrt{20}}{2}, \quad T=\sqrt{20}\\ t_k = \frac{k}{n}(T-t_0)+t_0\\ t_0 = 0, \quad t_1 = \frac{\sqrt{20}}{2}, \quad t_2 = \sqrt{20}\\ u'(t_k) = \frac{-2t_ku(t_k)^2}{20}, \quad u(t_0 = 0) = 1\\ u(t_{k+1})=u(t_k)+hu'(t_k)\\ u(t_1)=u(t_0)+h\frac{-2t_0u(t_0)^2}{20}=1+\frac{\sqrt{20}}{2}\frac{-2\cdot 0\cdot 1^2}{20}=1\\ u(t_2)=u(t_1)+h\frac{-2t_1u(t_1)^2}{20}=1+\frac{\sqrt{20}}{2}\frac{-2\cdot \frac{\sqrt{20}}{2}\cdot 1^2}{20}=1-\frac{1}{2}=\frac{1}{2}\\ $$ And, since the exact solution is known and substituting $t_2 = \sqrt{20}$: $$ u(t) = \frac{1}{1+t^2/20}\\ u(\sqrt{20}) = \frac{1}{1+\frac{\sqrt{20}^2}{20}} = \frac{1}{1+1} = \frac{1}{2}\\ $$

cascavelho
  • 11
  • 1
0

This is quite normal, absent the coincidence that the error is this small in the largest step sizes.

The linear reduction of the error according to the order 1 of the method does only occur where the first term in the error expansion in powers of $h$ dominates all the other terms. For large $h$ these other terms however will be larger. For very small $h$ the accumulation of the small step updates will fall below the capabilities of the number format (summation tricks effectively change the number format for a higher precision). This gives a typical V-shape in a loglog plot of error vs. step size, with a wavy end at large step sizes.

See

Lutz Lehmann
  • 131,652
  • Thank you for your answer. However, I have found what I believe to be the cause in this case. For some coincidence of numbers, the Euler method gives out the exact solution for $n=2$ and the Python output is not exact due to floating point rounding errors. – cascavelho Jun 22 '24 at 19:03
  • This looks correct. The first floating-point error is from the square root in the step size, thus unavoidable. – Lutz Lehmann Jun 23 '24 at 07:32