Python practice
RL circuit simulator
Apply a DC voltage to a resistor and inductor. Change the parameters and watch the current approach its steady state, right in your browser.
Model: ideal series RL circuit, i(0) = 0, constant voltage from t = 0. Exact solution: i(t) = (U/R)·(1 − exp(−tR/L)). At τ, current reaches 63.2% of its limit. Changing parameters starts a new experiment; playback shows 5τ in 6 seconds.
Explore the equation in lesson 3 →Move from reproducing an example to designing an experiment. Python is not needed to read the lessons. Download the calculation package to change parameters and regenerate plots.
Set up an environment
Install Python 3.11 or later. Extract the archive into a separate folder, open a terminal there and create a virtual environment to isolate course dependencies.
python -m venv .venv
# Windows:
.venv\Scripts\activate
# Linux / macOS:
source .venv/bin/activate
python -m pip install -r requirements.txt
python experiments.py --lesson 1Use lesson numbers 1–10. Without arguments, the command regenerates the entire base package. SVG and PNG outputs are saved in figures beside the script. SVG remains sharp when enlarged.
How the examples are organized
experiments.py contains common parameters, the motor right-hand side, interval integration and lesson_01 through lesson_10. common.py holds shared operations. Each experiment makes two figures, sometimes with coordinated panels. verify.py independently checks the RL response, motor equilibrium, integral energy balance and PI steady-state error. Run it after changing common equations; update reference conditions if you intentionally change the physical model.
python experiments.py
python verify.pyA route through the experiments
| Experiment | Change | Check |
|---|---|---|
| 1. Time constant | τ from 0.1 to 0.5 s | 2% settling time |
| 2. Derivative | Initial speed | Initial acceleration sign |
| 3. RL circuit | L and R separately | Current limit and transient duration |
| 4. Rotation | Inertia | Acceleration duration at unchanged equilibrium |
| 5. Conversion | Speed and current | ei = Meω |
| 6. Motor | J and voltage | Analytical equilibrium |
| 7. Numerical method | Euler step | Convergence and stability boundary |
| 8. Load | Step size | Speed droop and current increase |
| 9. Braking | Voltage mode | Torque, speed and power signs |
| 10. Controller | Kp, Ki and reference | Error, saturation and peak current |
Calculations for lessons 11–15
Download advanced-experiments.py: RL identification, an exact PWM period, cascaded-loop tuning, free two-mass oscillations and discrete stability. Run python advanced-experiments.py with NumPy and SciPy installed. It prints numerical checkpoints; the lessons specify plotting and experiment extensions.
An experiment record
Record the objective, equations, parameters, initial conditions, inputs, solver settings and plots. Then explain what changed, why, and which measured numbers support the conclusion. Instead of “the curve became slower”, name a metric and its unit.
For example: doubling inertia left equilibrium speed unchanged but increased the 90% rise time because the same net torque produces less acceleration. Obtain the numbers from your own parameter set.
Final mini-project
Build a drive maintaining 60 rad/s under a 0.1 N·m load step. Compare constant voltage, P control and PI control using the same time interval. Plot current, speed and voltage and verify the required equilibrium fits within ±12 V. Increase R by 20%, then separately J by 50%. Explain which changes affect equilibrium, transients or both before combining factors.
Troubleshooting
Missing module: install libraries with the same interpreter that runs the script, using python -m pip.
A nearly flat plot: check the time scale; a 10 ms response disappears on a 100 s axis.
Divergence: check units and resistance/friction signs; reduce Euler step or inspect solve_ivp success and message.
Text and modified plots disagree: lesson values describe the original parameters. Label your changed experiment explicitly.
Check your understanding
Test the fundamentals before opening the generator laboratory. Check your answers, read explanations and revisit the relevant lessons. Answers are not sent or saved.