Timedora

Numerical Solutions of Linear Systems

A practical guide to advancing, checking, and interpreting numerical solutions of linear differential systems.

Euler Steps and Accuracy

Euler’s matrix update

For a linear system

x′(t)=Ax(t),x(0)=x0,\mathbf{x}'(t)=A\mathbf{x}(t),\qquad \mathbf{x}(0)=\mathbf{x}_0,

Euler’s method estimates the next state by using the slope at the current state. With step size hh and times tn=nht_n=nh, the slope is AxnA\mathbf{x}_n, so

xn+1=xn+hAxn=(I+hA)xn.\mathbf{x}_{n+1}=\mathbf{x}_n+hA\mathbf{x}_n=(I+hA)\mathbf{x}_n.

Thus, each step is a matrix–vector multiplication. Repeating the update advances the approximate solution through time. The exact solution, when available, is x(t)=eAtx0\mathbf{x}(t)=e^{At}\mathbf{x}_0, which can be used as a benchmark; Euler’s estimates generally differ from it.

Worked example: two decaying modes

Consider the diagonal system x′=[−100−2]x\mathbf{x}'=\begin{bmatrix}-1&0\\0&-2\end{bmatrix}\mathbf{x}, with initial state (1,1)(1,1). Its exact solution is (e−t,e−2t)(e^{-t},e^{-2t}). For a diagonal matrix, the Euler update scales each component independently: with h=0.2h=0.2, the factors are 1−0.2=0.81-0.2=0.8 and 1−2(0.2)=0.61-2(0.2)=0.6. After five steps, at t=1t=1, the components are therefore 0.85=0.327680.8^5=0.32768 and 0.65=0.077760.6^5=0.07776. The exact values are approximately (0.36788,0.13534)(0.36788,0.13534). The components decrease as expected, but the Euler values are lower.

Reducing the step to h=0.1h=0.1 requires ten steps to reach t=1t=1. The factors become 0.90.9 and 0.80.8, giving (0.910,0.810)≈(0.34868,0.10737)(0.9^{10},0.8^{10})\approx(0.34868,0.10737). This is closer to the exact result for both components.

Euler and exact values at t = 1
Step size hEuler resultExact result
0.2(0.32768, 0.07776)(0.36788, 0.13534)
0.1(0.34868, 0.10737)(0.36788, 0.13534)
Euler approximations for the two-mode decay system at t = 1, alongside the exact solution.

Step size and error

The step size sets the spacing between computed states. A smaller hh usually improves accuracy, but reaching the same final time then requires more steps and more computation. is the error introduced in one step; is the accumulated difference from the exact solution over the interval. In this example, halving hh moves both Euler values closer to the exact values.

Comparing computations with hh and h/2h/2 is a practical accuracy check. If the results change substantially, try a smaller step or a more accurate method. Euler’s is first order: over a fixed interval, it typically decreases in proportion to hh, so halving the step typically roughly halves the when the computation is in its convergent range. This is a typical behavior, not a guarantee outside that range.

Is Not Accuracy

Deriving the decay condition

For a scalar eigenmode with eigenvalue λ\lambda, one Euler step multiplies its value by 1+hλ1+h\lambda. Repeated steps therefore scale the mode repeatedly by this same factor. To keep a decaying mode decreasing in magnitude, the factor must satisfy

∣1+hλ∣<1.|1+h\lambda|<1.

For real λ<0\lambda<0 and positive step size hh, this becomes

−1<1+hλ<1⟹−2<hλ<0⟹0<h<2∣λ∣.-1<1+h\lambda<1 \quad\Longrightarrow\quad -2<h\lambda<0 \quad\Longrightarrow\quad 0<h<\frac{2}{|\lambda|}.

In the two-mode example, the faster mode has λ=−2\lambda=-2, so its condition is 0<h<10<h<1. The step h=0.2h=0.2 satisfies it: that mode is multiplied by 0.60.6 each step and decays numerically. But its value at t=1t=1, 0.077760.07776, still differs noticeably from the exact value, about 0.135340.13534.

and accuracy answer different questions. This condition tells whether the mode’s numerical magnitude decays; it does not promise that its computed value closely matches the exact solution. A step can be stable and still yield noticeable error.

Solving, Plotting, and Interpreting Trajectories

Integrating a vector-valued system

A vector-valued system can be passed to SciPy’s solve_ivp interface. For the example system, provide the right-hand side AxA\mathbf{x}, the initial state, the time interval, and—if values are wanted at particular times—an evaluation-time grid. The solver can return the solution at those requested times. The relative and absolute tolerances, rtol and atol, control its estimated . Adaptive solvers adjust their step sizes using estimated error; Runge–Kutta methods usually provide better accuracy than Euler’s method for a given step count.

Here is the source example, with both numerical and exact solutions computed for comparison:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

A = np.array([[-1.0, 0.0],
              [0.0, -2.0]])
y0 = np.array([1.0, 1.0])
t = np.linspace(0.0, 1.0, 101)

solution = solve_ivp(
    lambda time, y: A @ y,
    (t[0], t[-1]),
    y0,
    t_eval=t,
    rtol=1e-9,
    atol=1e-11,
)

exact = np.vstack((np.exp(-t), np.exp(-2.0 * t)))

fig, axes = plt.subplots(1, 2, figsize=(10, 4))
axes[0].plot(solution.y[0], solution.y[1], label="Numerical trajectory")
axes[0].plot(exact[0], exact[1], "--", label="Exact trajectory")
axes[0].set(xlabel="x₁", ylabel="x₂", title="Phase plane")
axes[0].legend()

axes[1].plot(t, solution.y[0], label="Numerical x₁")
axes[1].plot(t, solution.y[1], label="Numerical x₂")
axes[1].plot(t, exact[0], "--", label="Exact x₁")
axes[1].plot(t, exact[1], "--", label="Exact x₂")
axes[1].set(xlabel="Time t", ylabel="State", title="Components over time")
axes[1].legend()

plt.tight_layout()
plt.show()

The right-hand side uses the current state y to compute AxA\mathbf{x}; y0 sets the initial condition. The interval runs from the first to the last requested time, while t_eval=t asks for returned values on the grid. The tolerances are solver settings, not additional initial conditions. The plotting commands use the returned component values and the exact solution to make both comparisons.

Plotting and checking the result

Two plots answer different questions. A places x1x_1 against x2x_2, showing the state’s path without displaying time directly. A component-versus-time plot shows how each state component changes and decays over time. In the source example, plotting the solver’s numerical values alongside the exact solution makes it possible to compare the paths and the component histories.

Eigenvalues provide a qualitative check on such plots: negative real eigenvalues indicate decay, positive real eigenvalues indicate growth, and complex eigenvalues commonly produce oscillations. For this example, eigenvalues −1-1 and −2-2 predict that both components decay, with the second mode decaying faster. If a computed trajectory conflicts with the expected qualitative behavior, check the step size, solver tolerances, and implementation. This check can reveal problems, but the qualitative prediction alone does not establish that every computed value is accurate.

  • : A numerical solver that changes step sizes based on estimated error.