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.
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 :)
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.
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!
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.
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에서 Loops and Conditional Statements에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
