Trouble making a parameter sweep for Simulink

19 次查看(过去 30 天)
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.

采纳的回答

Paul
Paul about 15 hours 前
编辑:Paul about 9 hours 前
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by