Python practice

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.

Download Python examples ↓

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 1

Use 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.py

A route through the experiments

ExperimentChangeCheck
1. Time constantτ from 0.1 to 0.5 s2% settling time
2. DerivativeInitial speedInitial acceleration sign
3. RL circuitL and R separatelyCurrent limit and transient duration
4. RotationInertiaAcceleration duration at unchanged equilibrium
5. ConversionSpeed and currentei = Meω
6. MotorJ and voltageAnalytical equilibrium
7. Numerical methodEuler stepConvergence and stability boundary
8. LoadStep sizeSpeed droop and current increase
9. BrakingVoltage modeTorque, speed and power signs
10. ControllerKp, Ki and referenceError, 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.

Practice · 10 questions

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.

1/10 After one time constant, a first-order step response completes about…
2/10 What is the RL time constant for L = 0.2 H and R = 4 Ω?
3/10 With unchanged net torque, inertia doubles. Acceleration…
4/10 What is the kinetic energy for J = 0.2 kg·m² and ω = 10 rad/s?
5/10 A DC motor speeds up at constant flux. Its back EMF…
6/10 How can you check the effect of the integration step?
7/10 What does PI integral action provide in a stable, unsaturated regime?
8/10 What is synchronous speed at f = 50 Hz with two pole pairs?
9/10 For P = EV·sin(δ)/X with E = 1.2, V = 1 and X = 0.6, maximum power is…
10/10 In the swing equation, at Δω = 0 mechanical power exceeds electrical power. The rotor…

Select one answer for each question.

Apply your knowledge in the generator laboratory →

Synchronous generator laboratory

From per-unit quantities to transient stability: change parameters, compare responses and export your results.

Open the interactive model →

Lessons 16–20 explain the equations, assumptions and numerical checks behind the simulation.

Start the generator module →