Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Some ODEs are hard (or impossible!) to solve analytically... or maybe you just don’t want to! Numerical approximations to the solution can be very valuable in these cases.

For example, let’s solve the initial value problem:

yy=t,y(0)=0y' - y = t, \qquad y(0) = 0

using Euler’s method with a step size Δt=0.2\Delta t = 0.2. First, rewrite in explicit form:

y=t+y=f(t,y) y' = t + y = f(t, y)

Then, form a table and update

nnttyyff
0000
10.200.2
20.40.040.44
30.60.1280.728
40.80.2741.074
51.00.489

Let’s compare our numerical result to the analytical solution to the same ODE. This solution can be obtained using an integrating factor:

F=e1d ⁣t=etF = e^{\int -1 \d{t}} = e^{-t}

Integrate by parts:

Frd ⁣t=tetd ⁣t=tet+etd ⁣t=(t+1)et\begin{align} \int F r \d{t} &= \int t e^{-t} \d{t} \\ &= -t e^{-t} + \int e^{-t} \d{t} \\ &= -(t + 1) e^{-t} \end{align}

So the general solution is:

y(t)=et[(t+1)et+c]=cext1y(t) = e^t \left[-(t + 1) e^{-t} + c \right] = c e^x - t - 1

Solve for c using the initial condition:

y(0)=c1=0c=1y(0) = c - 1 = 0 \to c = 1

so

y(t)=ett1y(t) = e^t - t - 1

Comparing the numerical and analytical solutions at the same time points by computing the absolute error between them gives:

ttNumericalAnalyticalError
0000
0.200.0210.021
0.40.040.0920.052
0.60.1280.2220.094
0.80.2740.4260.152
1.00.4890.7180.229

The error increases as more timesteps are run due to accumulation of error. A smaller timestep typically produces a better solution, but at the cost of doing additional calculations. We will discuss next how the error depends on the step size.

Error in approximation

Because we are using truncated Taylor series, we accrue an error:

Euler's method error

The error in a single step is O(Δt2)O(\Delta t^2). This is called the local error. To cover a finite time t, t/Δtt/\Delta t steps are required so the global (total) error is O(Δt)O(\Delta t). To improve accuracy, you should decrease Δt\Delta t, but this takes more work. Some Δt\Delta t will also totally fail! This is the field of numerical stability (not covered here).

Skill builder problems

Solution to Exercise 1

The ODE is already in explicit form, so f(t,y)=y+5sin(2πt)f(t, y) = y + 5 \sin(2\pi t).

Euler’s method with Δt=0.1\Delta t = 0.1:

nnttyyff
00.01.0001.000
10.11.1004.039
20.21.5046.257
30.32.1306.895
40.42.8185.757
50.53.3943.394
60.63.7330.795
70.73.813-0.942
80.83.719-1.037
90.93.6150.676
101.03.683

gives y(1)3.683y(1) \approx 3.683.

Euler’s method with Δt=0.2\Delta t = 0.2:

nnttyyff
00.01.0001.000
10.21.2006.955
20.42.5915.330
30.63.6570.576
40.83.772-1.155
51.03.322

gives y(1)=3.322y(1) = 3.322.

To find the analytical solution, note that this is a linear first-order ODE

yy=5sin(2πt)y' - y = 5 \sin(2\pi t)

with p=1p = -1 and r=5sin(2πt)r = 5 \sin (2\pi t). The integrating factor and required integral are:

F=epd ⁣t=etFrd ⁣t=5etsin(2πt)d ⁣t=5et4π2+1[sin(2πt)2πcos(2πt)]\begin{align} F &= e^{\int p \d{t}} = e^{-t} \\ \int F r \d{t} &= 5 \int e^{-t} \sin(2\pi t) \d{t} \\ &= \frac{5 e^{-t}}{4\pi^2 + 1}\left[ -\sin(2\pi t) - 2\pi \cos(2\pi t)\right] \end{align}

where the integral was evaluated using a table of integrals. Then, solve for y

y=1F[Frd ⁣t+c]=54π2+1(sin(2πt)+2πcos(2πt))+cet\begin{align} y &= \frac{1}{F} \left[\int F r \d{t} + c \right] \\ &= -\frac{5}{4\pi^2 + 1} \left(\sin(2\pi t) + 2\pi \cos(2\pi t)\right) + c e^{t} \end{align}

Finally, apply the initial condition:

y(0)=10π4π2+1+c=1y(0) = \frac{-10 \pi}{4\pi^2 + 1} + c = 1

so

y=(1+10π4π2+1)et54π2+1[sin(2πt)+2πcos(2πt)]y = \left(1 + \frac{10\pi}{4\pi^2 + 1}\right) e^{t} - \frac{5}{4\pi^2 + 1} \left[\sin(2\pi t) + 2\pi \cos(2\pi t)\right]

Evaluating this at t=1t = 1 gives y(1)=4.052y(1) = 4.052.

The absolute error is 0.369 for Δt=0.1\Delta t = 0.1 and 0.730 for Δt=0.2\Delta t = 0.2. Hence the error decreases by about a factor of 2 when Δt\Delta t is also decreased by a factor of 2.