For loop with linspcace - for a multiple plots?

1 回表示 (過去 30 日間)
STP
STP 2019 年 2 月 22 日
コメント済み: Star Strider 2019 年 3 月 4 日
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 :)

採用された回答

Star Strider
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 件のコメント
STP
STP 2019 年 3 月 4 日
P1 = (V1 - U_in1).^2
which is plot from 0 to 4.2 so if i want to plot all the values of P1 for different betas at 4.2 vs the beta.
for
beta-4-6
P1= ..
plot(t1,P1) // here the value of P1 at 4.4 (end of t1))= stored in a variable)
end
plot(all beta's used, variable which has all P1 values at 4.2 stored) <-- objective
Hopefully the above helps; nevertheless Thanks for your reply!:)
Star Strider
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 ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by