Overview
Week 1 computed the balance of the campus hour by hour, as if each hour stood still. Physical quantities do not stand still. A battery empties while it delivers power, and a building cools at night. The frequency of an island falls the moment a load switches on. This week describes such changes with differential equations and simulates them with the Euler method. Three models follow: The state of charge of the battery, the temperature of the library and the frequency of the campus in island mode. The campus microgrid reaches stage 2, its first dynamic models. The Python step introduces simulation loops, and the week closes with machine learning that respects physics.
Week at a glance
flowchart LR A["Differential equations"] --> B["The battery"] B --> C["The library"] C --> D["The frequency of the island"] D --> S["The campus microgrid, stage 2"] S --> FT["In the field: A heat meter"] FT --> H["Learning that respects physics"]
Every method on this page is also written as Python code in the Colab notebook of the week. Its code is divided into numbered sections. Each orange box on this page names the section whose code carries out what the text above the box explains. The box says what to run and what to look at in that section, and its button opens the Colab notebook at that place. The virtual laboratories, the practice parts and the self-assessment of the week are in the interactive lab. Each blue box opens one part of the interactive lab. The part ends with a link back to its box on this page, and the Colab notebook is always opened from the orange boxes here.
Python step 2: Simulation loops with NumPy
Python code in the Colab notebook, right after Section 0 (setup). Open the Colab notebook and run this step cell by cell: It consists of short pieces of Python code with their explanations, a quick check and three exercises. Topics: Arrays and time vectors · Arithmetic on whole arrays · The Euler step in a loop · Plots of trajectories.
The step continues with NumPy and Matplotlib [1, 2]. It creates arrays of time points and computes with them as a whole. It then writes the Euler step in a loop and plots the trajectory next to the exact solution, the pattern of every simulation of this course. Students who have never programmed start with the start-here notebook of the course.
Week 1 introduced the campus with its solar plant, wind turbine, battery, five buildings and car park. It computed the balance of a spring day and a battery rule that cut the peak import from 585.7 to 450.0 kW.
This week gives three parts of the campus their dynamics: The battery in the evening, the library at night and the frequency of the island.

Differential equations: How physical quantities change
The state of a physical system is the set of quantities that determines its future, such as a temperature or a state of charge. A differential equation gives the rate of change of the state from the state itself and the inputs [3]. Many quantities of the campus approach a final value at a rate proportional to the remaining distance, which gives a first-order system:
\(\dot{x}\) is the rate of change of \(x\), \(x_{ss}\) the final or steady-state value, \(x_0\) the initial value and \(\tau\) the time constant. After one time constant, the system has covered 63.2 percent of the way, after three time constants 95 percent [4].
A computer cannot follow time continuously, so it advances the state in small steps \(\Delta t\). The Euler method takes the rate of change at the start of each step as constant over the step [3]:
The distance to the final value is multiplied by \(1 - \Delta t/\tau\) at every step, the same structure as the loop of Week 1. A small step follows the exact solution closely. For \(\tau < \Delta t < 2\tau\) the simulation overshoots and oscillates, and for \(\Delta t > 2\tau\) it grows without limit, although the physical system is perfectly stable.
A worked example. The cabinet of the inverter warms from 25 °C toward 45 °C with \(\tau = 20\) min. After 20 min, the exact temperature is \(45 - 20\,e^{-1} = 45 - 7.358 = 37.64\) °C. The Euler method with \(\Delta t = 5\) min gives 30.00, 33.75, 36.56 and 38.67 °C after four steps, so 1.03 K too warm. With \(\Delta t = 45\) min, the factor is \(1 - 45/20 = -1.25\), and the simulation diverges.
Animation. The exact warming of the cabinet from 25 °C toward 45 °C with \(\tau = 20\) min, and the Euler method with the chosen step. The slider sets the step \(\Delta t\). The readouts give the factor \(1 - \Delta t/\tau\), the first step against the exact solution and the behaviour of the simulation. Try 5, 20, 30 and 45 min.

Check your understanding. A first-order system has \(\tau = 4\) s. Which Euler step makes the simulation diverge?
Python code in the Colab notebook, Section 1. Open Section 1 of the Colab notebook and run it. The function exact carries out the solution of this section and euler the Euler method. The printout repeats the worked example for five step sizes and shows the factor \(1 - \Delta t/\tau\) of each, and a figure compares them.
- A simulation of a whole year with a step of one second is accurate but slow. How would you choose the step for each part of the campus?
- Why can a simulation diverge although the real system is stable? What does this mean for a digital twin?
The battery: State of charge as an integrator
The state of charge (SoC) of a battery is the share of its usable energy that it holds. It changes with the power that the battery delivers, and the losses of the converter and the cells make charging and discharging unequal. With the energy \(E\) and the efficiency \(\eta\) of each direction:
The state of charge integrates the power over time: It has no time constant and settles nowhere on its own. Its voltage follows a simple equivalent circuit [5]. An open-circuit voltage \(V_{oc}\) rises with the state of charge, and an internal resistance \(R_0\) lowers the terminal voltage in proportion to the current \(I\):
The pack of the campus has \(E = 500\) kWh, \(\eta = 0.95\) and \(R_0 = 0.04\) Ω. The linear open-circuit voltage is a simplification for the course; real cells have curved characteristics, which Week 6 uses to estimate the state of charge.
A worked example. The battery delivers 200 kW for 1.5 h from a state of charge of 0.80. The state of charge falls by \(200 \times 1.5 / (0.95 \times 500) \)\({}= 300 / 475 \)\({}= 0.632\) to 0.168. At the start, \(V_{oc} = 700 + 120 \times 0.8 = 796\) V and the current is \(200{,}000 / 796 = 251.3\) A. The terminal voltage is \(796 - 251.3 \times 0.04 = 785.9\) V.
Scene: Inside the battery container. The battery container of the campus, open on its long side: Five racks of modules, the inverter cabinet and the screen of the battery management system (BMS). The buttons let the battery discharge 200 kW, charge 100 kW or rest; one hour passes in four seconds. The modules change their colour with the state of charge, and the readouts show the state of charge, the open-circuit voltage, the current and the terminal voltage.

Check your understanding. The battery charges with 100 kW for 2 hours. How much does its state of charge rise?
Python code in the Colab notebook, Section 2. Open Section 2 of the Colab notebook and run it. The function d_soc carries out the first formula of this section and terminal_voltage the second. The code repeats the worked example and then integrates the evening discharge of Week 1 minute by minute.
- The state of charge never settles on its own. What does a controller of the battery have to watch that a controller of a room temperature does not?
- Why does the voltage of a battery drop at once when it starts to deliver a high power?
A building as a heat capacitor
A building stores heat in its walls, floors and air and loses heat through its envelope. The simplest useful model lumps it into one heat capacity \(C\) and one thermal resistance \(R\) to the outside, a resistor and a capacitor (RC) model [6]:
\(T\) is the indoor temperature, \(T_{out}\) the outdoor temperature and \(P\) the power of the heater in kW. \(C\) is in kWh per kelvin and \(R\) in kelvin per kW, so the time constant \(\tau\) is in hours. Bacher and Madsen found that such simple models describe the main heat dynamics of a building, and that more states improve the fit when data allow [6].
The model gives the time that a heater with constant power needs to bring the room from \(T_0\) to \(T_1\):
→ These formulas with numbers, step by step: Lab Part B, calculator, steps 1 to 4.
A worked example. The library has \(C = 30\) kWh/K and \(R = 0.4\) K/kW, so \(\tau = 12\) h. At \(T_{out} = 5\) °C, a heater of 40 kW holds \(T_{ss} = 5 + 0.4 \times 40 = 21\) °C. Starting from 15 °C, the room reaches \(21 - 6\,e^{-3/12} = 21 - 4.673 = 16.33\) °C after 3 h. With 60 kW, \(T_{ss} = 29\) °C, and the room needs \(t = 12 \ln(14/9) = 5.30\) h from 15 to 20 °C.

Check your understanding. Better insulation doubles the resistance \(R\) of the library. What happens to the time constant and to the heater power that holds 21 °C at 5 °C?
Interactive lab, Part A. The climate room of the library in 3D: Radiators with the heating controller, a thermostat and a data logger in a reading room, with a screen that draws the night. Find the power that holds 21 °C and the latest start of the heating. The results can be copied into the Colab notebook.
Interactive lab, Part B. The model of the building step by step: The time constant, the steady state, the temperature after some hours and the time to reach a target.
Python code in the Colab notebook, Section 3. Open Section 3 of the Colab notebook and run it. The function simulate_room steps the model of this section, and time_to_reach carries out the last formula. Paste the results of Part A into MY_NIGHTS, and the code compares each of your nights with the model.
- Why does a heavy stone building stay cool on a hot afternoon, and what does it mean for its heating in winter?
- A setback saves energy at night but needs more power in the morning. Who decides between comfort, energy and peak power?
Grid frequency: The swing of the island
The frequency of an alternating current grid shows the balance of power in real time. In a large grid, rotating generators store kinetic energy; when a load switches on, they slow down and the frequency falls [7]. An island such as the campus has little rotating mass, and its inverters must provide a virtual inertia. For the deviation \(\Delta f\) from 50 Hz, the aggregated swing equation reads:
\(\Delta P\) is a step of the load in kW, \(M\) the inertia in kW·s/Hz and \(D\) the damping in kW/Hz. The damping arises because motors and other loads draw less power at a lower frequency. \(H\) is the inertia constant in seconds, \(S\) the rated power and \(f_0 = 50\) Hz. For the campus with \(H = 2\) s on 1000 kVA, \(M = 80\) kW·s/Hz, and \(D = 12\) kW/Hz [7].
The equation gives three numbers. At the first moment, the frequency falls at the rate of change of frequency (RoCoF) \(-\Delta P/M\). Without control, it settles at \(-\Delta P/D\) with the time constant \(M/D\). A droop of the battery adds a gain \(K\) to \(D\), which Week 5 studies as a controller.
→ These formulas with numbers, step by step: Lab Part C, calculator, steps 1 to 5.
A worked example. Three chargers start with 66 kW. The RoCoF is \(-66/80 = -0.825\) Hz/s. Without control, the frequency would settle at \(-66/12 = -5.5\) Hz with \(M/D = 6.67\) s. An under-frequency relay set to 47.5 Hz trips after \(t = 6.67 \ln(5.5/3) = 4.04\) s. With a droop of \(K = 125\) kW/Hz, the deviation settles at \(-66/137 = -0.482\) Hz within a time constant of 0.584 s.

Check your understanding. The inertia of the island doubles, and nothing else changes. What happens to the initial RoCoF and to the final deviation without control?
Interactive lab, Part C. The flywheel bench in 3D, a model of the island at the scale 1:1000. It holds a flywheel set with adjustable inertia, a load bank with switches of 22 W, a frequency meter and the droop switch of the battery. Measure the RoCoF of your load steps, and the calculator of the swing equation. The results can be copied into the Colab notebook.
Python code in the Colab notebook, Section 4. Open Section 4 of the Colab notebook and run it. The function swing steps the equation of this section with a step of 1 ms and finds the time of a trip. Paste the results of Part C into MY_STEPS, and the code compares your measured RoCoF and final deviations with the model.
- Solar plants and batteries have no rotating mass. How can an inverter behave as if it had inertia?
- Why does a small island need faster protection and control than a large national grid?
Development templates of the week
The folder templates/ of the repository collects small working programs that carry out the methods of the course. Week 2 adds two of them, each with a description, its inputs and outputs and an example run.
| Template | What it does |
|---|---|
first_order_simulation.py | Simulates any model \(\dot{x} = f(t, x, u)\) with the methods of Euler, Heun and Runge-Kutta, and compares them with an exact solution |
building_preheat.py | Simulates a building with its RC model and a heating schedule, and finds the latest start of the heater for a target temperature by bisection |
The engineering side: Parameters and validation
A model is only as good as its parameters. Each model of this week has two or three, and each can be measured on the real system with a simple experiment. A model whose predictions have been compared with measurements that were not used to fit it is called validated, and only a validated model deserves trust in decisions.
| Model | Parameters | How to measure them |
|---|---|---|
| Battery | Energy \(E\), efficiency \(\eta\), resistance \(R_0\) | A full cycle of charge and discharge; a step of the current and the jump of the voltage |
| Library | Capacity \(C\), resistance \(R\) | A night with the heater off: the time constant from the cooling curve; a steady day with a known heater power |
| Frequency | Inertia \(M\), damping \(D\) | A known load step: \(M\) from the initial RoCoF, \(D\) from the final deviation without control |
A pot of hot soup cools fast at first and then more and more slowly, because the heat flows faster when the soup is much warmer than the room. A thick pot with a lid cools slower: More capacity and more resistance give a longer time constant, the same physics as the library.
- Which of the three models of this week would you trust least for a decision tomorrow, and why?
- How would you check the model of the library in winter if it was fitted with data from autumn?
The campus microgrid, stage 2: Dynamic models of the battery, the library and the frequency
Stage 2 applies the three models to questions of the campus. How far does the evening shaving of Week 1 drain the battery? When must the heater of the library start after a night setback? What does the largest load step do to the frequency of the island?
| Question | Result | Model |
|---|---|---|
| State of charge after the evening shaving, 17:00 to 21:00 | From 0.900 to 0.144; terminal voltage 715.2 V at the end | Battery |
| Latest start of 60 kW for 20 °C at 8:00, heater off from 20:00 | 6.27 h after 20:00, at 2:16 | Library |
| Heating energy of the night | 344.0 kWh with the setback, 480.0 kWh at 21 °C all night | Library |
| 66 kW in island mode | RoCoF -0.825 Hz/s; trip after 4.04 s without control; -0.482 Hz with droop | Frequency |
The setback saves 136.0 kWh, 28 percent of the heating energy of the night. In return, it asks the heater for 60 kW from 2:16 on, when the solar plant does not deliver. The island cannot survive a step of 66 kW without the droop of the battery, which makes the controller of Week 5 a matter of survival, not of comfort.

Check your understanding. Why does the island trip without control, although its final deviation would be only 5.5 Hz below 50 Hz?
Interactive lab, Part D. Stage 2 by hand: The state of charge after a discharge, the steady state of the library and the RoCoF of a load step.
Python code in the Colab notebook, Section 5. Open Section 5 of the Colab notebook and run it. The code collects the results of stage 2 in the dictionary STAGE2 and finds the latest start of the heater by bisection. A figure draws the night of the library and the frequency of the island.
In the field: Reading a heat meter
The heating power of Part A is measured by a heat meter on the heating circuit of the library. A flow sensor counts the water, and a pair of temperature sensors measures the supply and the return. The meter multiplies the flow by the difference of the temperatures and by the heat coefficient \(k\) of water. The European Norm (EN) 1434-1 defines this coefficient for thermal energy meters [8]:
\(P\) is the power in kW, \(Q\) the flow in m³/h and \(k\) about 1.15 kWh/(m³·K) for water near 50 °C. For example, a flow of 1.200 m³/h that cools from 65.0 to 45.0 °C with \(k = 1.150\) kWh/(m³·K) carries \(1.150 \times 1.200 \times 20.0 = 27.6\) kW. The meter adds the power up to energy in kWh or MWh, and its display shows one value after another.
Interactive lab, Part E. The field task: The heat meter of the heating circuit. Step through its display, compute the power from the flow and the temperatures, and check it with the energy register.
Your application
First-order models describe many systems beyond the campus. The Colab notebook holds an application switch with five further systems. They are a water tank, the battery of an electric car in its final charging phase, a server room after a failure of its cooling, a cold store and a greenhouse.
Python code in the Colab notebook, Section 6. Open Section 6 of the Colab notebook, set APP to one of the six names in the comment and run the cell. The code prints the trajectory of the system with its time constant.
Learning that respects physics
Machine learning can learn the dynamics of a system from data, without a physical model. Such a model may fit its training data well and still break physical laws outside them. A learned model of the library might, for example, predict that the room warms with the heater off. Physics-informed machine learning adds the known equations to the training, for example as a penalty for every violation of the energy balance [9].
For cyber-physical systems, the two approaches complement each other. The RC model of this week captures the main behaviour with two parameters. A learned model can correct what it misses, such as the sun through the windows or the people in the reading room. Week 12 builds a digital twin on this idea.
Suppose that the team trains a small neural network on a year of measurements to predict the temperature of the library. Its predictions are checked against the RC model: When the network predicts warming without heat, the RC model is used instead and the case is logged. Over time, the network learns only the difference between the RC model and the measurements.
- When would you prefer a learned model over a physical one, and when the other way around?
- A learned model was trained only on winter data. What could go wrong in the first hot week of summer?
Review cards
Select a card to turn it over.
References
[1] Harris, C. R., Millman, K. J., van der Walt, S. J., Gommers, R., Virtanen, P., Cournapeau, D., Wieser, E., Taylor, J., Berg, S., Smith, N. J., Kern, R., Picus, M., Hoyer, S., van Kerkwijk, M. H., Brett, M., Haldane, A., Fernández del Río, J., Wiebe, M., Peterson, P., Gérard-Marchant, P., Sheppard, K., Reddy, T., Weckesser, W., Abbasi, H., Gohlke, C., & Oliphant, T. E. (2020). Array programming with NumPy. Nature, 585(7825), 357-362. https://doi.org/10.1038/s41586-020-2649-2
[2] Hunter, J. D. (2007). Matplotlib: A 2D graphics environment. Computing in Science & Engineering, 9(3), 90-95. https://doi.org/10.1109/MCSE.2007.55
[3] Lee, E. A., & Seshia, S. A. (2017). Introduction to Embedded Systems: A Cyber-Physical Systems Approach (2nd ed.). MIT Press. https://ptolemy.berkeley.edu/books/leeseshia/
[4] Åström, K. J., & Murray, R. M. (2021). Feedback Systems: An Introduction for Scientists and Engineers (2nd ed.). Princeton University Press.
[5] Plett, G. L. (2004). Extended Kalman filtering for battery management systems of LiPB-based HEV battery packs. Journal of Power Sources, 134(2), 252-261. https://doi.org/10.1016/j.jpowsour.2004.02.031
[6] Bacher, P., & Madsen, H. (2011). Identifying suitable models for the heat dynamics of buildings. Energy and Buildings, 43(7), 1511-1522. https://doi.org/10.1016/j.enbuild.2011.02.005
[7] Kundur, P. (1994). Power System Stability and Control. McGraw-Hill.
[8] European Committee for Standardization. (2022). Thermal energy meters - Part 1: General requirements (EN 1434-1:2022). CEN.
[9] Karniadakis, G. E., Kevrekidis, I. G., Lu, L., Perdikaris, P., Wang, S., & Yang, L. (2021). Physics-informed machine learning. Nature Reviews Physics, 3(6), 422-440. https://doi.org/10.1038/s42254-021-00314-5