Exercise 16.4

Exercise 16.4#

Compare Euler’s method, 2nd order Runge-Kutta methods (trapezoidal and midpoint) and the 4th order Runge-Kutta method by solving the differential equation:

\[ \frac{dy}{dx} + y = 1 \]

with an initial condition \(y(0) = 0\), and the same step size (\(h\)) for each.

  1. Plot each of your solutions on a separate set of axis, but each with the exact solution:

    \[ y(x) = 1 - e^{-x} \]

    plotted on the same axis. You may want to make use of a combination of dashed and solid lines in order to see both curves.

  2. Plot the global truncation error for each method at \(x = 1\) as a function of \(h\). Do these trends match the theoretical ones mentioned in the notes?