Trouble making a parameter sweep for Simulink

27 visualizzazioni (ultimi 30 giorni)
Salvador
Salvador il 5 Dic 2025 alle 18:50
Commentato: Salvador il 6 Dic 2025 alle 11:56
Hi, I'm trying to make a parameter sweep program that fetches different output values of two logged signals for different values of a resistance in a Simulink circuit. When I simulate said circuit manually, inside Simulink, I get the expected results and everything works like it should. However, to realize said parameter sweep I must write an appropriate code in an m. file, and after a couple hours of research I was able to come up with the following:
model = 'simulacion_redes_bipuerta';
load_system(model)
R = linspace(0.1,5,10);
I = zeros(1,10);
V = zeros(1,10);
for k = 1:length(R)
set_param(['simulacion_redes_bipuerta','/Load'],'Resistance',num2str(R(k)));
sim(model);
I(k) = out.logsout{1}.Values.Data(end);
V(k) = out.logsout{2}.Values.Data(end);
end
P = V.*I;
plot(R,P)
The problem with this script is that when running it the 'out' object does not get saved to my workspace (nor any other kind of output variable), and thus I get an error saying "Unable to resolve the name 'out.logsout'." I have no idea why this could be happening. My Simulink model settings are set to default, by which a "Single simulation output" named 'out' should be saved to my workspace after each simulation. Any help would be very appreciated!
Edit: It seems that the 'out' variable gets saved only when I manually run the simulation inside Simulink itself, but then again this isn't enough for me as I'm supposed to perform the parameter sweep across at least 100 different values. Therefore, the problem seems to lie in the sim() command, which for some reason isn't simulating the model correctly.

Risposta accettata

Paul
Paul il 5 Dic 2025 alle 21:42
Modificato: Paul il 6 Dic 2025 alle 3:35
Try
out = sim(model);
More generally check out Simulink.SimulationInput and Optimize, Estimate, and Sweep Block Parameter Values and Running Multiple Simulations and linked doc pages for alternative approaches if interested.
Also, it's recommended to use string instead of num2str.

Più risposte (0)

Categorie

Scopri di più su Programmatic Model Editing in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by