For loop with linspcace - for a multiple plots?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
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 件のコメント
Jan
2019 年 2 月 22 日
Even 4141 figures will exhaust the resources of the computer massively.
Yeah, Thanks! it is exhausting for the resources. But I could reduce the span of the parameters and then use subplot!. If I want to add more parts on the same plot for eg:
omega = 2*pi*2856;
Qo = 100000;
beta=5
QL = Qo./(1+beta);
alfa = (2*beta)./(1+beta);
omega_half = omega/(2*QL);
U_in1 = 1;
y1 = 0;
t1 = [0 4.2]
dV1dt = @(t,V) ((U_in1*omega*beta)/Qo) - omega_half*V;
[t1 V1] = ode15s(dV1dt, t1, y1)
% hold on ;
t2 = [4.2 5]
U_in2 = -1;
y2= V1(end);
dV2dt = @(t,V) ((U_in2*omega*beta)/Qo) - omega_half*V;
[t2, V2] = ode15s(dV2dt, t2, y2)
t3 = [5 8]
U_in3 = 0;
y3= V2(end);
dV3dt = @(t,V) ((U_in3*omega*beta)/Qo) - omega_half*V;
[t3, V3] = ode15s(dV3dt, t3, y3)
V = [V1; V2; V3];
t = [t1; t2; t3];
plot(t,V);
How is this achieved in the same way? I tried but its just overlapping on one time period when it should be in 3 different timeperiods.
Star Strider
2019 年 2 月 22 日
To use subplot and your variable vectors, do 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)))
end
end
This produces
figures, each with 3 subplot axes.
Experiment to get the result you want.
Hi Star Strider. Thankyou :)
Star Strider
2019 年 2 月 25 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
Hi, Its a little similar- so I am commenting here instead of creating a new question. For the loop - if I wish to plot for all values in a single plot- which is usually achieved by 'hold on' instead of using figures or subplots- so as to see how the curve changes for diff values in one plot. For the same thing how can index or like a small descripion be applied on the fig so as to know what parameter is giving which output as with hold on; it comes all together.
Star Strider
2019 年 2 月 27 日
I am not certain what you want, so I will hazard a guess.
I have been experimenting, and came up with this idea:
N = 10;
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];
t0 = linspace(0, 4.2, 50);
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(k1, k2,:)] = ode15s(@(t,V)dV1dt(t,V,beta(k1),Qo(k2),omega_half), t0, y1);
end
end
figure
surfc(t1, beta, squeeze(mean(V1,2)))
xlabel('t')
ylabel('\beta')
figure
surfc(t1, Qo, squeeze(mean(V1,1)))
xlabel('t')
ylabel('Q_o')
NQo = 1;
figure
surfc(t1, beta, squeeze(V1(:,NQo,:)))
xlabel('t')
ylabel('\beta')
title(sprintf('Qo = %3.1E', Qo(NQo)))
Nbeta = 10;
figure
surfc(t1, Qo, squeeze(V1(Nbeta,:,:)))
xlabel('t')
ylabel('Q_o')
title(sprintf('\\beta = %.2f', beta(Nbeta)))
It reduces the 3D ‘V1’ matrix to 2D by taking the mean the other parameter (first two figures), or simply chooses one value of the other parameter (last two figures). I use surfc here to also draw sontours beneath the plot. That gives a bit more information.
Experiment to get the result you want.
STP
2019 年 3 月 4 日
Hi,
I have a question on similar terms- so I am putting it here. As you showed me how to vary Q and beta's both in a loop. I pt Q as one value and I vary beta and plot. Now if I wish to plot P vs beta; I will have to store the value of P at 4.2 for all beta's in a variable, and then plot it with the beta's on x axis - so as to see for what beta am I getting max P. How to achieve that? Again it needs to be part of the loop. Many thanks!
Star Strider
2019 年 3 月 4 日
I am lost. What is ‘P’? It does not appear anywhere.
Since I have no idea what you are now doing, I suggest that you experiment with the code I posted to get the result you want.
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!:)
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 件)
カテゴリ
ヘルプ センター および File Exchange で Annotations についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
