How to plot graphs for a loop for all the different values you've selected for the loop

I've used a for loop to change the temperature where an event code happens to change a value in a dT/dt derivative. Does anyone know how to code so that all the graphs for the 6 different derivatives against time can be plotted? I need all the graphs because I need to observe when the reactor blows up and at what temperature I selected. Any help would be greatly appreciated.

Respuestas (1)

Do you want all the plots in one graph? If so use hold on:
for k = 1 : 6
% Get x and y, then call plot
plot(x, y, '-');
hold on;
end
legend;
Or do you want 6 different plots? If so use subplot():
for k = 1 : 6
% Get x and y, then call plot
subplot(2, 3, k);
plot(x, y, '-');
end
legend;

5 comentarios

Heres part of my code, the six different variables I want plotted against time are below, y(:,1) etc., I want the concentrations on the same plot (ca, cb, cs, cd) and then P and T_2 on seperate. The code below shows a for loop which changes the temperature that the event occurs which changes an equation. Do you know what code to use to plot these 3 plots I just mentioned for each different temperature the event occurs at (because of the loop)?
function twoc
tspan = [0 10]; %just as an example
initial_conditions = [4.3 5.1 3 0 4.4 422]; %its the values of the variables you are solving for at t=0
thermostat_setpoints = 455:2.5:500;
ntemps = length(thermostat_setpoints);
for tempidx = 1 : ntemps
current_setpoint = thermostat_setpoints(tempidx);
options = odeset('Events',@(t,y)myEventsFcn(t,y,current_setpoint));
[t1,y1,te,ye,ie] = ode15s(@(t,y)fun(t,y(1),y(2),y(3),y(4),y(5),y(6),1), tspan, initial_conditions, options);
[t2,y2] = ode15s(@(t,y)fun(t,y(1),y(2),y(3),y(4),y(5),y(6),2), [te tspan(end)], ye);
t = [t1;te;t2];
y = [y1;ye;y2];
ca = y(:,1);
cb = y(:,2);
cs = y(:,3);
cd = y(:,4);
P = y(:,5);
T_2 = y(:,6);
You didn't answer either of my two questions. Why not?
To plot them all on one plot, I think you can use
plot(t, y, '-', 'LineWidth', 3);
my bad, a bit of both i want all the concentrations against time on the same plot, so y(1), y(2), y(3) and y(4) and then I want P (y(5)) and T_2 (y(5)) on seperate plots. Also I want these for every single loop aswell, this is the bit im struggling with.
If this is awkward to do because of the sheer volume of graphs that need to load, could you please help me put a condition in where when the reactor temperature reaches 500K or 45 atm stop the loop and let me know what temperature the cooling water change occurs at for the one before the temp that makes the reactor blow up (reach 500 K or 45 atm).
Exactly which variable is the temperature at each iteration?

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 17 de En. de 2022

Comentada:

el 18 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by