MATLAB: For loop with linspcace – for a multiple plots

for loopplot

beta=5;
omega = 2856;
Qo = 10000;
QL = Qo/(1+beta);
omega_half = omega/(2*QL);
U_in1 = 1;
y1 = 0;
alfa = (2*beta)./(1+beta);
t1 = [0 4.2]
dV1dt = @(t,V) ((U_in1*omega*beta)/Qo) - omega_half*V;
[t1 V1] = ode15s(dV1dt, t1, y1)
figure;
plot(t1,V1);
figure
Aout1=V1-1;
plot(t1, Aout1);
figure
P1 = (V1 - U_in1).^2
plot(t1, P1);
.. further more plots
Now If I wish to produce all such plots for different values fo 'beta' and 'Qo'; which has been stated at the top; how can I go about that? I read the for loop documentation but failed to apply it here.
for eg. beta from 2 to 6 like 2.1, 2.2 etc.. and for Qo from 70000 to 120000 with 500 spacing. Thanks in advance to all the volunteers 🙂

Best Answer

Use nested loops:
beta= 2 : 0.1 : 6;
omega = 2*pi*2856;
Qo = 7E+4 : 500 : 1.2E+5;
U_in1 = 1;
y1 = 0;
alfa = (2*beta)./(1+beta);
t0 = [0 4.2];
dV1dt = @(t,V,beta,Qo,omega_half) ((U_in1*omega*beta)/Qo) - omega_half*V;
for k1 = 1:2%numel(beta)
for k2 = 1:2%numel(Qo)
QL = Qo(k2)/(1+beta(k1));
omega_half = omega/(2*QL);
[t1 V1] = ode15s(@(t,V)dV1dt(t,V,beta(k1),Qo(k2),omega_half), t0, y1);
figure
plot(t1,V1)
xlabel('t')
ylabel('V_1')
title(sprintf('\\beta = %.1f Q_o = %6d', beta(k1),Qo(k2)))
figure
Aout1=V1-1;
plot(t1, Aout1)
xlabel('t')
ylabel('A_{out}')
title(sprintf('\\beta = %.1f Q_o = %6d', beta(k1),Qo(k2)))
figure
P1 = (V1 - U_in1).^2;
plot(t1, P1)
xlabel('t')
ylabel('P_1')
title(sprintf('\\beta = %.1f Q_o = %6d', beta(k1),Qo(k2)))
end
end
Note that this will produce 12423 figures! It will probably be better to combine all of them into one figure for each loop iteration using the subplot function, to reduce that to 4141 figures. .