Day 04 Day 4

Day 4

Day 4

~1 hour Intermediate Hands-on Precision AI Academy

Today's Objective

Block diagrams, ODE solvers, continuous and discrete systems, and parameter sweeps. The visual programming environment that ships with every MATLAB license.

What Is Simulink?

Simulink is a graphical block-diagram environment for modeling and simulating dynamic systems. Instead of writing equations, you connect blocks — integrators, transfer functions, gains, sources, sinks — and Simulink solves the resulting differential equations for you. It's the standard tool in control systems, embedded systems, and aerospace simulation.

You can also drive Simulink from MATLAB code using the sim() function and set_param. All exercises in this lesson can be done programmatically without clicking in the GUI — this is the professional workflow for automation and parameter sweeps.

ODE Solvers in MATLAB (Foundation for Simulink)

Before building Simulink models, it helps to understand how MATLAB solves ODEs directly. This is exactly what Simulink does under the hood.

matlab_-_ode_solvers.txt
MATLAB — ODE SOLVERS
% Solve a second-order ODE: spring-mass-damper
% m*x'' + c*x' + k*x = F(t)
% State-space form: x1 = x, x2 = x'
% x1' = x2
% x2' = (F(t) - c*x2 - k*x1) / m

m = 1;    % mass (kg)
c = 0.5;  % damping (N·s/m)
k = 4;    % spring constant (N/m)
F = @(t) (t < 1);   % step force: 1 for t<1, then 0

% ODE function: state = [x; x_dot]
ode_fn = @(t, s) [s(2);
                   (F(t) - c*s(2) - k*s(1)) / m];

% Initial conditions: x=0, x'=0
s0   = [0; 0];
tspan = [0, 10];

% Solve with ode45 (Runge-Kutta 4/5 — default for smooth problems)
[t, s] = ode45(ode_fn, tspan, s0);

% For stiff systems, use ode15s instead
% [t, s] = ode15s(ode_fn, tspan, s0);

figure;
subplot(2,1,1);
plot(t, s(:,1), 'b', 'LineWidth', 1.5);
title('Position x(t)'); xlabel('Time (s)'); ylabel('x (m)');
grid on; xline(1, 'r--', 'Force off');

subplot(2,1,2);
plot(t, s(:,2), 'r', 'LineWidth', 1.5);
title('Velocity x''(t)'); xlabel('Time (s)'); ylabel('x'' (m/s)');
grid on;

Driving Simulink Programmatically

matlab_-_sim()_api.txt
MATLAB — SIM() API
% Run a Simulink model from MATLAB script
% (assumes 'spring_damper.slx' exists and has Workspace logging)

model = 'spring_damper';
load_system(model);

% Set parameters in the model's workspace
set_param(model, 'StopTime', '10');
set_param(model, 'Solver', 'ode45');
set_param(model, 'MaxStep', '0.01');

% Assign variables used by the model
assignin('base', 'mass', 1.0);
assignin('base', 'damping', 0.5);
assignin('base', 'spring_k', 4.0);

% Run the simulation
simOut = sim(model, 'ReturnWorkspaceOutputs', 'on');

% Access logged signals
t = simOut.get('tout');
x = simOut.get('yout');

figure;
plot(t, x.signals.values);
title('Simulink Output'); xlabel('Time (s)'); ylabel('Position (m)');
grid on;

% Parameter sweep: vary damping coefficient
damping_values = [0.1, 0.5, 1.0, 2.0];
figure; hold on;
for i = 1:length(damping_values)
    assignin('base', 'damping', damping_values(i));
    out = sim(model, 'ReturnWorkspaceOutputs', 'on');
    t_i = out.get('tout');
    x_i = out.get('yout');
    plot(t_i, x_i.signals.values, 'LineWidth', 1.5, ...
         'DisplayName', sprintf('c = %.1f', damping_values(i)));
end
hold off;
legend('Location', 'best');
title('Parameter Sweep: Damping Coefficient');
xlabel('Time (s)'); ylabel('Position (m)');
grid on;

Building a Model Programmatically with add_block

matlab_-_create_simulink_model_in_code.txt
MATLAB — CREATE SIMULINK MODEL IN CODE
% Create a new Simulink model programmatically
model = 'integrator_demo';
new_system(model);
open_system(model);

% Add blocks
add_block('simulink/Sources/Step',         [model '/Step'],       'Position', [50,50,80,80]);
add_block('simulink/Continuous/Integrator',[model '/Integrator'], 'Position', [150,45,200,85]);
add_block('simulink/Sinks/Scope',          [model '/Scope'],      'Position', [280,47,320,83]);

% Connect blocks: Step -> Integrator -> Scope
add_line(model, 'Step/1',       'Integrator/1');
add_line(model, 'Integrator/1', 'Scope/1');

% Configure step block
set_param([model '/Step'], 'Time', '1', 'After', '1', 'Before', '0');

% Set simulation parameters
set_param(model, 'StopTime', '5', 'Solver', 'ode45');

% Save and run
save_system(model);
sim(model);

% The Scope shows the integral of a step — a ramp
Use Simulink.SimulationInput for parallel parameter sweeps. When running many simulations, create an array of SimulationInput objects and call parsim() to run them in parallel — orders of magnitude faster than a for loop with sim().
Exercise
Simulate a PID-Controlled Temperature System
  1. Model a first-order thermal system: T' = (1/tau)*(T_env + K*u - T) where tau=30s, K=10, T_env=20°C. Solve with ode45 in MATLAB (no Simulink needed).
  2. Implement a discrete PID controller: compute the control signal u(t) based on error, integral of error, and derivative of error. Use Kp=2, Ki=0.1, Kd=0.5. Target setpoint: 70°C.
  3. Close the loop: feed the controller output back into the plant ODE and simulate for 300 seconds.
  4. Plot the temperature response vs setpoint. Annotate rise time (10%–90%), overshoot, and steady-state error.
  5. Sweep Kp from 0.5 to 5 in 5 steps. Plot all responses on one axes and identify the best Kp by minimum ITAE (integral of time-weighted absolute error).

Extend the PID exercise: add anti-windup protection to prevent integrator saturation when the control signal hits a saturation limit (u_max = 15). Compare the response with and without anti-windup on a plot. Then implement a Smith Predictor to compensate for a 5-second pure time delay in the system.

What's Next

The foundations from today carry directly into Day 5. In the next session the focus shifts to Day 5 — building directly on everything covered here.

Supporting Videos & Reading

Go deeper with these external references.

Day 4 Checkpoint

Before moving on, verify you can answer these without looking:

Live Bootcamp

Learn this in person — 2 days, 5 cities

Thu–Fri sessions in Denver, Los Angeles, New York, Chicago, and Dallas. $1,490 per seat. June–October 2026.

Reserve Your Seat →
Continue To Day 5
Day 5: Toolboxes