A10 · Lesson 25 · Lab: Laplace and Bode in code
Poles, Bode plots, margins and responses by computer
Lessons 18 to 24 described systems by their transfer functions: poles and zeros, Bode plots, impulse and step responses. A transfer function is just two polynomials, and NumPy handles polynomials directly. In this lab you'll compute a Bode plot and read a feedback loop's crossover and phase margin from it, measure how far the straight-line asymptotes miss, find an RLC filter's poles and damping from its polynomial, invert a transform with complex poles and check it by simulation, and compute a pulse response by convolution.
- The curves are the gain in dB of against , and its straight-line asymptotes.
- Raise : the curve moves up and crosses 0 dB at a higher frequency, where the two poles have used up more phase.
- The readout gives the phase margin at the crossover, which falls as rises: more gain means a less damped closed loop.
Predict
At a single pole's corner frequency, how far below the straight-line asymptotes is the true gain?
3 dB: at the corner, or dB, while both asymptotes meet at 0 dB there. An octave away, the error is about 1 dB, and a decade away it's under 0.05 dB. The asymptotes are a sketch; computing the curve takes one line.
Name it
Transfer functions as polynomials
Store numerator and denominator coefficients highest power first: is num = [1, 3], den = [1, 3, 2]. Then:
np.polyval(num, s) / np.polyval(den, s)evaluates at any complex , so on a frequency grid gives the Bode plot;np.roots(den)gives the poles, andnp.roots(num)the zeros;- residues at each simple pole give the impulse response .
Margins
For a loop gain , the gain crossover is where ; the phase margin is plus the phase there. A phase margin of 45° to 60° gives a well-damped closed loop; near 0° it rings and nearly oscillates.
Second-order poles
has poles at . From computed poles: and .
Worked example
Find the gain crossover and phase margin of .
Show the solution
- Set the gain to 1. means
- Solve for the crossover. That's a quadratic in , with root , so
- The phase there.
- The phase margin. a comfortable loop.
Answer the gain crossover is at 78.2 rad/s, and the phase margin is 59.3°.
Worked example
A series RLC has = 100 Ω, = 10 mH and = 1 µF, with the output across . Find its poles, and .
Show the solution
- The transfer function.
- Normalize the denominator. Dividing by gives
- The poles. Its roots are s⁻¹, at distance from the origin.
- The natural frequency. It's that distance:
- The damping ratio.
- The overshoot. The step response overshoots by
Answer poles at s⁻¹, rad/s and ; the step response overshoots by 16%.
Check
A single pole . What is its exact gain at 1 times its corner frequency?
Check
A series RLC with = 100 Ω, = 10 mH and = 1 µF. Computing its poles , what is ?
Check
For , compute and its phase on 200 001 log-spaced points from 1 to 10 000 rad/s. Set wc to the frequency where the gain is closest to 1 and pm to the phase margin there, in degrees.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
For a single pole , set err1 and err2 to the exact gain minus the asymptote, in dB, at = 1 and 2 (the asymptote is 0 dB below the corner and above).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
For the RLC with = 100 Ω, = 10 mH, = 1 µF (output across ), find the poles of with np.roots and set w0 and zeta from them.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Invert by residues at the roots of its denominator (they include a complex pair) and set y1 to . Then simulate from rest with RK4 ( = 0.001) and set y1_sim.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
An RC low-pass with = 1 ms gets a 1 V pulse lasting 2 ms. Convolve its impulse response with the pulse on a grid of = 0.001 ms out to 6 ms, and set v2 and v4 to the output at 2 and 4 ms.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is a phase margin of about 60° considered safe for a feedback loop, and what goes wrong as it shrinks?
At the crossover frequency the loop gain has magnitude 1, so the phase margin measures how far the loop is from the condition , where a signal would come back around the loop with the same size and inverted sign and sustain itself: oscillation. With a 60° margin, the closed loop behaves roughly like a second-order system with around 0.6, a small overshoot and quick settling. As the margin shrinks, the closed-loop poles move toward the imaginary axis: overshoot and ringing grow, the response near the crossover peaks, and any drift in component values that adds phase or gain can push the loop over into oscillation. That's why margins are specified, not just stability.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.