Borrar filtros
Borrar filtros

How do I plot in a for loop while using workspace?

2 visualizaciones (últimos 30 días)
Csaba Farkas
Csaba Farkas el 12 de Oct. de 2021
Respondida: Samay Sagar el 1 de Mzo. de 2024
I want to plot some equations in one figure while using Simulink. How do I plot it if there is an 'a' and 'b' in the equations and the end is results (in workspace)?
It is a working one but I cannot use in 2021.
%TKbe = 250:20:450;
TKbe = 300:10:400;
%vA = [(0.1/3600):(0.5/3600):(0.99/3600),(1/3600):(1/3600):(10/3600)];
vA = [(0.1/3600):(0.5/3600):(0.99/3600),(1/3600):(0.5/3600):(10/3600)];
cb=0;
nb=0;
konv=0;
tart=0;
for i=1:size(TKbe,2)
const_TKbe = TKbe(1,i)
set_param(block_TKbe{1},'Value', num2str(const_TKbe));
const_TAbe = TKbe(1,i)
set_param(block_TAbe{1},'Value', num2str(const_TAbe));
for j=1:size(vA,2)
const_vA = vA(1,j)
set_param(block_vA{1},'Value', num2str(const_vA));
sim(MODELLNEV);
cb(j, i) = cout.signals.values(end,2);
nb(j, i) = cout.signals.values(end,2)*vA(1,j);
konv(j, i) = (cout.signals.values(end,2)) / const_cabe_0;
tart(j, i) = (const_VR_0 * (hout.signals.values(end,1)) / const_h_0) / vA(1,j);
end
end
How can I make it simpler and working where I can get a table of cb?

Respuestas (1)

Samay Sagar
Samay Sagar el 1 de Mzo. de 2024
It will be helpful if you could share the exact issue you are facing when trying to run the model in MATLAB R2021a. Please note that to utilize the simulation outputs within your code, it is essential to enable the “ReturnWorkspaceOutputs” option during the simulation execution. You can achieve this by using:
sim('YourModelName', 'ReturnWorkspaceOutputs', 'on');
Assuming that you are using the “To Workspace” block to log your output, you can follow these steps to simplify your code:
  1. Run simulations in a loop, changing the parameters each time.
  2. Collect the results at the end of each simulation.
  3. Plot the results using MATLAB's plotting functions.
  4. Store the variable “cb” as a matrix and then convert it to a table using “array2table” function.
Here's a modified version of your code which you can use to plot the results of your simulation:
% Define the range of parameters
TKbe = 300:10:400;
vA = [(0.1/3600):(0.5/3600):(0.99/3600),(1/3600):(0.5/3600):(10/3600)];
% Initialize the results matrices
cb = zeros(length(vA), length(TKbe));
nb = zeros(length(vA), length(TKbe));
konv = zeros(length(vA), length(TKbe));
tart = zeros(length(vA), length(TKbe));
% Loop over the range of TKbe and vA parameters
for i = 1:length(TKbe)
const_TKbe = TKbe(i);
const_TAbe = const_TKbe; % Assuming TAbe is the same as TKbe
for j = 1:length(vA)
const_vA = vA(j);
% Set the parameters in the Simulink model
set_param('YourModelName/TKbeBlock', 'Value', num2str(const_TKbe));
set_param('YourModelName/TAbeBlock', 'Value', num2str(const_TAbe));
set_param('YourModelName/vABlock', 'Value', num2str(const_vA));
% Run the simulation
simOut = sim('YourModelName', 'ReturnWorkspaceOutputs', 'on');
% Collect the results from the simulation
cb(j, i) = simOut.get('cout').signals.values(end, 2);
nb(j, i) = simOut.get('cout').signals.values(end, 2) * const_vA;
konv(j, i) = simOut.get('cout').signals.values(end, 2) / const_cabe_0;
tart(j, i) = (const_VR_0 * simOut.get('hout').signals.values(end, 1) / const_h_0) / const_vA;
end
end
% Plot the results (example: plotting cb)
figure;
surf(TKbe, vA*3600, cb);
xlabel('TKbe');
ylabel('vA (1/h)');
zlabel('cb');
title('cb vs. TKbe and vA');
% Convert the cb matrix to a table
cbTable = array2table(cb, 'VariableNames', strcat('TKbe', string(TKbe)), 'RowNames', strcat('vA', string(vA*3600)));
For more information about “array2table” function, you can refer the documentation available here:

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by