Step Functions and Shifting
Many inputs switch on or off: a voltage applied at , a pump that runs for ten minutes, a force that stops. The unit step function (Heaviside function)
describes them, and the Laplace transform handles it with one rule.
Building piecewise functions
- Switch on at : .
- On between and : .
Example. A force of 3 between and , then 0: .
The time-shift rule
A delay by in time is multiplication by in the -domain. In particular .
Careful: the function must be shifted too. To transform , write , so .
import sympy as sp
t, s = sp.symbols("t s", positive=True)
H = sp.Heaviside
for f in (H(t - 2), (t - 1)**2 * H(t - 1), t**2 * H(t - 1), 3 * H(t - 1) - 3 * H(t - 4)):
print(f"L{{{f}}} =", sp.simplify(sp.laplace_transform(f, t, s, noconds=True)))L{Heaviside(t - 2)} = exp(-2*s)/s
L{(t - 1)**2*Heaviside(t - 1)} = 2*exp(-s)/s**3
L{t**2*Heaviside(t - 1)} = (s**2 + 2*s + 2)*exp(-s)/s**3
L{-3*Heaviside(t - 4) + 3*Heaviside(t - 1)} = (3*exp(3*s) - 3)*exp(-4*s)/s
Example: a mass pushed for a while
, . Transforming: , so
The shift rule gives
After the force is gone and the mass oscillates freely.
import numpy as np
from scipy.integrate import solve_ivp
force = lambda t: 1.0 if 1 <= t < 4 else 0.0
sol = solve_ivp(lambda t, Y: [Y[1], force(t) - Y[0]], (0, 8), [0.0, 0.0],
t_eval=[0.5, 2, 4, 6, 8], max_step=0.01, rtol=1e-10, atol=1e-12)
u = lambda t, a: np.where(t >= a, 1.0, 0.0)
exact = lambda t: (1 - np.cos(t - 1)) * u(t, 1) - (1 - np.cos(t - 4)) * u(t, 4)
for t, x in zip(sol.t, sol.y[0]):
print(f"t = {t}: solver {x: .6f}, formula {exact(t): .6f}")t = 0.5: solver 0.000000, formula 0.000000
t = 2.0: solver 0.459698, formula 0.459698
t = 4.0: solver 1.989992, formula 1.989992
t = 6.0: solver -0.699809, formula -0.699809
t = 8.0: solver -1.407546, formula -1.407546
max_step=0.01 keeps the solver from stepping over the switching times; for exact switching, solve each interval separately or use events.
Inverse transforms with exponentials
When appears in , invert the rest and shift: .
import sympy as sp
t, s = sp.symbols("t s", positive=True)
Y = (sp.exp(-s) - sp.exp(-4 * s)) / (s * (s**2 + 1))
print(sp.inverse_laplace_transform(Y, s, t))cos(t - 4)*Heaviside(t - 4) - cos(t - 1)*Heaviside(t - 1) - Heaviside(t - 4) + Heaviside(t - 1)
In software
- Simulink and similar tools have "step" blocks for exactly this; controller tuning is judged by the "step response".
- In numerical simulation, discontinuous inputs require care: adaptive solvers must stop at the switching times (
eventsor splitting the integration interval), or they waste steps and lose accuracy.
Exercises
Exercise 1: Write with steps
Write for , for , for using step functions.
Answer
.
Solution
is 1 exactly on ; multiply by .
Exercise 2: Transform
Find and .
Answer
and .
Solution
The first is the shift rule with . For the second, write .
Exercise 3: Switched RC circuit (code)
, (an RC circuit with , battery connected at ). Find and .
Answer
; .
Solution
, and .
import sympy as sp
t, s = sp.symbols("t s", positive=True)
q = sp.inverse_laplace_transform(sp.exp(-2 * s) / (s * (s + 1)), s, t)
print(q, "|", round(float(q.subs(t, 3)), 4))-exp(2 - t)*Heaviside(t - 2) + Heaviside(t - 2) | 0.6321
Test yourself
is…
A factor in means the corresponding part of …
- is multiplied by
- is delayed by 2 and switched on at
- is divided by 2
- is zero
The function equals 1…
- for
- for
- for
- never