Skip to content
Tactiverge

School/□ Systems Engineer/Week 10

Is the code right? Is the model right?

Your simulation of a cooling cup is bug-free and matches the equation perfectly. It is still wrong by 10 °C. How can both be true?

About 35 min · 3 goals · Checked by automated tests

1.Why should I care?

Every engineering simulation can fail in two separate ways: the code can be wrong, or the physics can be wrong. Engineers check both, in that order, and write down which they checked. A model that passes only one is not yet evidence.

  • ◇ Define verification (solving the equations correctly) and validation (solving the right equations).
  • ◇ Verify a simulation against an exact solution and a step-size convergence check.
  • ◇ Validate a model against data that was not used to fit it.

2.Predict

A simulation agrees with the exact solution of its equation to four decimal places. What has that proved?

Guess first. Being wrong here is how the surprise works.

3.Understand

Verification: solving the equations right

Check the code against cases with known answers: an exact solution, a conserved quantity, a limit like 'no input means no change'. Then halve the time step: if the answer still changes noticeably, the step was too big. These checks say nothing about the real world.

Validation: solving the right equations

Compare the model with measurements, ideally from runs not used to fit its parameters. Fitting τ to one cooling curve and then predicting a second cup at a different starting temperature is a real test; matching the curve you fitted to is not.

Order matters, and so does honesty

Verify first: a bug can masquerade as a physics problem. Then validate, report the error, and list the assumptions the model makes (constant room temperature, uniform cup temperature). Saying 'validated for these conditions' is honest; 'validated' alone rarely is.

Explain it another way

Verification asks whether you followed the recipe correctly. Validation asks whether the recipe makes the cake you wanted.

Worked example 1

Euler's method on dy/dt = −y/2 from y(0) = 1 gives y(2) = 0.3487 with dt = 0.2 and 0.3660 with dt = 0.02. The exact value is e^(−1) = 0.3679. What does this tell you?

The error falls from 0.019 to 0.0019 when dt shrinks ten times: first-order convergence, as expected for Euler. The code is verified for this case. It says nothing yet about whether dy/dt = −y/2 describes the real system.

Common mix-ups

  • If the simulation matches the exact solution, the model is right.
    ✓ That only verifies the code. The equations can still miss real physics.
  • A model either is or isn't valid.
    ✓ Validity has a domain: a cooling model can be good from 80 °C to 40 °C and poor near room temperature.

4.Discover

Investigation: in the feedback lab, set a first-order plant with K = 1, τ = 2 s under P control with Kp = 1 and a set point of 1. Theory says the loop settles at 0.5 with a time constant of 1 s, so y(1 s) ≈ 0.5 × 0.632 = 0.32. Read y at t = 1 s from the plot. If it agrees, you have a verification check; write down what it does not tell you about a real motor.

Explorer 02 — Feedback control

Measure, compare, correct

A controller compares the setpoint with what the sensor reports and pushes the plant to close the gap. Tune the gains and watch speed, overshoot and accuracy trade against each other.

OUTPUT y vs SETPOINT r0.00.51.0Ts 1.30sCONTROL EFFORT u0.02.0012345678910TIME (s)
  • Setpoint r = 1
  • Closed-loop output
  • Open loop (u = r/K)
  • ±2 % band
Controller

Gains

2.0
1.5
0.20

Applies the command the model says is right, u = r / K, without ever measuring.

Plant

Plant model

Like motor speed or an oven: τ·ẏ = −y + K·u. K = 1.

1.0 s

Real-world effects

0.000

Noise only affects the closed loop: the derivative term amplifies it.

Rise 10–90 %
0.72 s
Overshoot
0.0 %
Settling 2 %
1.30 s
Steady-state err
0.333
theory r/(1+Kp·K) = 0.333

State

P control, Kp 2.0 on a first-order plant with τ 1.0 s. Rise time 0.72 s, overshoot 0.0 %, 2 % settling 1.30 s, steady-state error 0.333 (theory for P: 0.333). Peak control effort 2.00. Open loop: steady-state error 0.000.

P control, Kp 2.0 on a first-order plant with τ 1.0 s. Rise time 0.72 s, overshoot 0.0 %, 2 % settling 1.30 s, steady-state error 0.333 (theory for P: 0.333). Peak control effort 2.00. Open loop: steady-state error 0.000.

How to read this

The top panel shows the setpoint (dashed line at 1) and the plant's true output after a step command at t = 0. The solid line is the controlled system; the dotted line is open loop. The bottom panel shows the control effort u the actuator is asked to deliver.

Rise time is how long the output takes to go from 10 % to 90 % of where it ends up. Overshoot is how far it passes that value. Settling time is when it stays inside ±2 % for good. Steady-state error is the gap left at the end: P control always leaves one on these plants; integral action removes it.

Assumptions & limitations
  • Linear plant models with known, constant parameters (K = 1). Real motors and heaters have friction, dead zones and parameters that drift with temperature and wear.
  • Controller runs every 10 ms and holds its output between samples; the plant is integrated with 4th-order Runge–Kutta at the same step. There is no transport delay, which real systems almost always have.
  • The derivative acts on the measurement (not the error) through a 50 ms low-pass filter, a common practical choice that avoids a spike when the setpoint jumps.
  • Sensor noise is white Gaussian and seeded for repeatability; it is added only to the measurement, never to the true output.
  • Metrics are computed on the true output up to the disturbance time (or 10 s); a response still moving at the end of the window is reported as not settled.
  • Open loop uses the exact plant gain to pick its command. That flatters it: any modelling error would show up as steady-state error.
Can't use the interactive? Read a description instead

In the feedback lab, a first-order plant under P control (K = 1, τ = 2 s, Kp = 1) rises smoothly towards 0.5 and is at about 0.32 after 1 s, matching the formula. That agreement is a verification check.

5.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

You compare your cooling model with a thermometer log from a second cup. Which activity is that?

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2

Why validate on data you did not fit to?

Practice 3

Halving the simulation time step changes your answer by 15 %. What should you conclude?

Practice 4

Euler's error for a problem is 0.020 at dt = 0.1 s. Euler's error is proportional to dt. What error do you expect at dt = 0.025 s?

Practice 5 · a fresh version every time

You simulate τ·dy/dt + y = 0 with explicit Euler, τ = 1.5 s. Above what time step dt (in seconds) does the simulation become unstable?

s

7.Apply it

Write a half-page verification and validation plan for a model of your choice (cooling drink, falling paper cone, charging phone). List two verification checks, one validation dataset you could collect, and the error you would accept.

8.Reflect

Concepts: Simulation and time stepping · First-order systems and the time constant · Overfitting and held-out tests