In this tutorial you’ll learn how to

  • set up implicit integration schemes

For this notebook you need matplotlib in addition to the simframe requirements.

Implicit Integration

In this example we revisit the differential equation from the first tutorial and the last tutorial about adaptive scheme.

For this tutorial we revisit the problem of the first tutorial.

  • \(\frac{\mathrm{d}Y}{\mathrm{d}x} = b\ Y\)

  • \(Y \left( 0 \right) = A\)

  • \(Y \left( x \right) = A\ e^{bx}\)

But this time we increase the step size, such that the 1st-order Euler scheme is unstable.

Analytic solution

import numpy as np
def f(x, A, b):
    return np.exp(b*x)

Model parameters

A  =   1.
b  =  -1.
dx =  10.

This time we chose a large step size and set up the frame.

from simframe import Frame
sim_expl = Frame(description="Explicit integratioon")
sim_expl.addfield("Y", A)
sim_expl.addintegrationvariable("x", 0.)
def fdx(frame):
    return dx
sim_expl.x.updater = fdx
sim_expl.x.snapshots = [10.]
def diff_expl(frame, x, Y):
    return b*Y
sim_expl.Y.differentiator = diff_expl
from simframe import Integrator
from simframe import Instruction
from simframe import schemes
sim_expl.integrator = Integrator(sim_expl.x)
sim_expl.integrator.instructions = [Instruction(schemes.expl_1_euler, sim_expl.Y)]
from simframe import writers
sim_expl.writer = writers.namespacewriter
sim_expl.writer.dumping = False
sim_expl.writer.verbosity = 0
Execution time: 0:00:00

Reading data and plotting

data_expl =
import matplotlib.pyplot as plt

def plot(ls):
    fig, ax = plt.subplots(dpi=150)
    x = np.linspace(-1., 11, 100)
    ax.set_xlim(x[0], x[-1])
    ax.plot(x, f(x, A, b), label="Solution")
    for sim, d in ls:
        ax.plot(sim.x, sim.Y, "o", label=d)
plot([(data_expl, "Explicit Euler 1st-order")])

In this case the step width is way too large to produce a useful result.

The only help would be to reduce the step size or to go for implicit integration.

Background: Implicit integration

For explicit integration the derivative \(f\) of the differential equation is evaluated at the current point in time or space:

\(\frac{\Delta Y}{\Delta x} = \frac{Y_{n+1} - Y_n}{\Delta x} = f\left( Y_n \right)\),

which leads to the simple 1st-order Euler scheme.

\(Y_{n+1} = Y_n + \Delta x\ f\left( Y_n \right)\).

Implicit integration means that the derivative is evaluated at the future point in time or space:

\(\frac{Y_{n+1} - Y_n}{\Delta x} = f\left( Y_{n+1} \right)\).

What looks ridiculous at first is mathematically sound.

Imagine that the derivative can be written as a matrix equation.

\(f\left( \vec{Y} \right) = \mathbf{J} \cdot \vec{Y}\),

with the Jacobian matrix \(\mathbf{J}\).

Plugging this into our differential equation yields

\(\vec{Y}_{n+1} - \vec{Y_n} = \Delta x\ \mathbf{J} \cdot \vec{Y}_{n+1}\)

\(\Leftrightarrow \left( \mathbb{1} - \Delta x\ \mathbb{J} \right) \cdot \vec{Y}_{n+1} = \vec{Y}_n\)

\(\Leftrightarrow \vec{Y}_{n+1} = \left( \mathbb{1} - \Delta x\ \mathbb{J} \right)^{-1} \cdot \vec{Y}_n\)

The solution can be found by inverting the matrix \(\mathbb{1} - \Delta x\ \mathbb{J}\).

In our simple case this translates to

\(\mathbb{J} = \begin{pmatrix} b \end{pmatrix}\)


\(Y_{n+1} = \frac{1}{1-\Delta x\ b} Y_n\)

For large step sizes \(\left( \Delta\ x \rightarrow \infty \right)\) this goes to zero \(\left( Y_n \rightarrow 0 \right)\) as it should compared to the exact solution. The integration scheme is “unconditionally stable”.

Setting up implicit integration

Setting up implicit integration is similar to explicit integration. We therefore just copy our frame and reset the values.

import copy
sim_impl = copy.deepcopy(sim_expl)
sim_impl.x = 0
sim_impl.Y = A

The important difference is now, that instead of the derivative we have to provide the Jacobian \(\mathbb{J}\) to our field \(Y\), which is in our case very simple. The function for the Jacobi matrix needs the parent frame object as first and the integration variable as second positional argument.

def jac_impl(sim, x):
    return np.array([b])
sim_impl.Y.jacobinator = jac_impl

We can now use an implicit scheme in our instruction set.

sim_impl.integrator.instructions = [Instruction(schemes.impl_1_euler_direct, sim_impl.Y)]

Now we can rerun the simulation.

Execution time: 0:00:00
data_impl =
plot([(data_expl, "Explicit Euler 1st-order"),(data_impl, "Implicit Euler 1st-order")])

Since implicit schemes involve matrix inversions, it can be very costly. The method shown here uses numpy.linalg.inv() to compute the inverse matrix, which is basically Gaussian elimination with LU factorization. There are other methods that might be more suitable for your problem.

Note: If the jacobinator is set, but not the differentiator, simframe will try to calculated the derivative from the Jacobi matrix by assuming

\(\vec{Y}' = \mathbb{J} \cdot \vec{Y}\)

sim_impl.Y.differentiator = None    # unsetting the differentiator

Only if neither the differentiator, nor the jacobinator are set, Field.derivative() will return zeros in the shape of the field, i.e., the derivative is zero.

sim_impl.Y.jacobinator = None