How to change circuit parameters during simulation ⚙️

A common question when running models in SPS Software is whether it is possible to tune parameters on the fly without stopping the simulation.

Here is how SPS Software handles parameter changes and how you can automate multiple scenarios using MATLAB® scripts.

:stop_sign: General Rule: Re-initialization

Each time you change a parameter of an SPS Software library block (like a resistance, inductance, or machine inertia), you generally have to restart the simulation. This is because the software needs to re-evaluate the state-space model and update the matrices for the linear and nonlinear parts of your circuit during the initialization phase.

:high_voltage: The Exception: Source Parameters

You can change the parameters of electrical sources during the simulation!
If you modify the Magnitude, Frequency, or Phase of an AC or DC source block, the modification takes place immediately as soon as you click Apply or close the block menu. This is great for manually testing voltage dips or frequency variations on the fly.


:rocket: Pro-Tip: Automating Parametric Studies

If you need to test multiple values for a passive component (like finding the worst-case scenario for an inductor), you don’t need to change it manually and click “Run” every single time.

You can enter a MATLAB variable (e.g., L1) in the block’s parameter dialog instead of a fixed number, and use a script to loop through the simulation.

Example: Finding the worst-case overvoltage
Suppose you have a model named my_circuit and you want to test an inductance (L1) from 10mH to 100mH to see which value causes the highest overvoltage (V1).

Here is a clean MATLAB script you can use to automate this parametric study:

% 1. Define the range of values to test (10 mH to 100 mH in 10 steps)
L1_vec = (10:10:100) * 1e-3; 

% 2. Initialize the maximum voltage tracker
V1_max = 0;

% 3. Loop through each value
for i = 1:10
    
    % Assign the current value to the workspace variable used in the block
    L1 = L1_vec(i);
    fprintf('Test No %d: L1 = %g H\n', i, L1);
    
    % Run the simulation
    sim('my_circuit'); 
    
    % Memorize the worst case (assuming V1 is saved to the workspace via ToWorkspace block)
    if max(abs(V1)) > V1_max
        imax = i;
        V1_max = max(abs(V1));
    end
    
end

% 4. Display the final result
fprintf('Maximum overvoltage %g V occurred for L1 = %g H\n', V1_max, L1_vec(imax));

This approach saves you hours of manual work and ensures you never miss a critical operational point in your design!

Hi, does this method work for real-time simulation execution in RT-LAB?

Hello
Great question! The short answer is: the same logic applies in RT-LAB, but with some important real-time constraints to keep in mind.

Passive component changes still require a restart.

Just as in SPS Software offline simulation, modifying parameters of passive library blocks (resistances, inductances, capacitances, machine inertia, etc.) in an RT-LAB model requires stopping the simulation and reloading the model onto the target. This is because RT-LAB compiles the Simulink model into C code and deploys it to the real-time target; the state-space matrices are fixed at load time and cannot be restructured mid-execution.

Source parameters can be changed on the fly

The live-edit exception for source Magnitude, Frequency, and Phase also holds in RT-LAB. You can modify these through RT-LAB’s visualization and control panel during execution, and the change takes effect immediately — no reload required. This is particularly useful for testing voltage dips or frequency deviations in HIL scenarios.

Automating parametric sweeps in RT-LAB

The MATLAB workspace variable approach described above does work with RT-LAB, with one key adaptation: instead of calling sim(‘my_circuit’) directly, you use the RT-LAB API (available in MATLAB, Python, or C/C++) to control the simulation lifecycle on the target. The general pattern becomes:

% Loop through parameter values
for i = 1:10
L1 = L1_vec(i);
% Load and start the model on the RT-LAB target
rtlab_load(‘my_circuit’);
rtlab_start();
% Wait for simulation to complete or reach steady state
pause(sim_duration);
% Acquire signals via RT-LAB API or OpComm/ToWorkspace blocks
% … signal acquisition logic here …
rtlab_stop();
end
The exact API calls will depend on your RT-LAB version and whether you are using the MATLAB API, the Python rtlabapi package, or a custom interface. Refer to the RT-LAB Comprehensive API documentation for the current function signatures.

One additional consideration for real-time execution

Because RT-LAB runs on a deterministic real-time OS (OPAL-RTLinux), each load → start → acquire → stop cycle carries a small overhead for compilation (if the model has changed) and target synchronization. For large parametric sweeps, it is worth pre-compiling the model once and only varying the workspace variable between runs to avoid repeated compilation delays.

@Parija Thank you for your reply. I am currently working with the RTLAB API. I have a question: Are there specific API functions available to modify parameters of the eHS circuit in real-time during execution? I understand that modifying parameters solely in the MATLAB workspace is not sufficient, as the generated code runs within RTLAB. Therefore, there should be relevant API functions to adjust the passive component parameters of the circuit to support this operation.