For loop with linspcace - for a multiple plots?
1 回表示 (過去 30 日間)
古いコメントを表示
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 :)
0 件のコメント
採用された回答
Star Strider
2019 年 2 月 22 日
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. .
11 件のコメント
Star Strider
2019 年 3 月 4 日
As always, my pleasure!
Try this:
N = 5;
beta= linspace(2, 6, N);
omega = 2*pi*2856;
Qo = linspace(7E+4, 1.2E+5, N);
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:numel(beta)
for k2 = 1: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
subplot(3,1,1)
plot(t1,V1)
ylabel('V_1')
title(sprintf('\\beta = %.1f Q_o = %6d', beta(k1),Qo(k2)))
subplot(3,1,2)
Aout1=V1-1;
plot(t1, Aout1)
ylabel('A_{out}')
title(sprintf('\\beta = %.1f Q_o = %6d', beta(k1),Qo(k2)))
subplot(3,1,3)
P1 = (V1 - U_in1).^2;
plot(t1, P1)
xlabel('t')
ylabel('P_1')
title(sprintf('\\beta = %.1f Q_o = %6d', beta(k1),Qo(k2)))
P42(k1,k2,:) = P1(end);
end
end
figure
plot(beta, P42)
grid
xlabel('\beta')
ylabel('\itP\rm')
lgnd = sprintfc('Q_o = %.2E', Qo);
legend(lgnd, 'Location','NW')
I believe the last plot is the one you want. The new ‘P42’ matrix stores the values of ‘P1’ at the last value of each integration vector (where ‘t1=4.2’). The rows are ‘beta’, and the columns are ‘Qo’.
Make appropriate changes otherwise.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!