Effacer les filtres
Effacer les filtres

Nested For Loop Help

1 vue (au cours des 30 derniers jours)
Jacey Allen
Jacey Allen le 18 Mar 2019
Commenté : Star Strider le 18 Mar 2019
I am trying to graph 9 plots. 3 for each altitude, with given T0 and P0, for Mach (M0) against F,spillage, and N_corr4. I had to find an M0 for each altitude(i), each mdotf(j), and each operating line that has the given parameters from the mdot_corr2 to N_corr2 section of the code(k). I'm pretty sure I have a good concept for finding the needed values to come up with my plots, but I am not confident in how I indexed the variables, used fsolve with the indexing, nested the for loops, and how I made the plots inside of the for loops. When I ran this code, it took forever and gave me over 400 plots when I stopped it. Any help would be much appreciated!!
alt=[0,5000,10000];
T0=[288,255.65,223.26];
P0=[101325,54019.9,26500];
mdotf=[mdotfR*.2:mdotfR*.2:mdotfR*3];
mdot_corr2=[mdot_corr21,mdot_corr22,mdot_corr23,mdot_corr24,mdot_corr25,mdot_corr2R,mdot_corr26,mdot_corr27];
total_temp_ratio_42=[total_temp_ratio_42_1,total_temp_ratio_42_2,total_temp_ratio_42_3,total_temp_ratio_42_4,total_temp_ratio_42_5,total_temp_ratio_42_R,total_temp_ratio_42_6,total_temp_ratio_42_7];
tc=[tc1,tc2,tc3,tc4,tc5,tcR,tc6,tc7];
pc=[pc1,pc2,pc3,pc4,pc5,pcR,pc6,pc7];
tt=[tt1,tt2,tt3,tt4,tt5,ttR,tt6,tt7];
pt=[pt1,pt2,pt3,pt4,pt5,ptR,pt6,pt7];
N_corr2=[N_corr21,N_corr22,N_corr23,N_corr24,N_corr25,N_corr2R,N_corr26,N_corr27];
for i=1:3
for j=1:15
for k=1:8
fun3=@(M0) (((pdR*P0(i)*(1+((g-1)/2)*M0^2)^(g/(g-1)))/Pstp)*sqrt(Tstp)*mdot_corr2(k)*cp*sqrt((T0(i)*(1+((g-1)/2)*M0^2)))*(total_temp_ratio_42(k)-tc(k))-mdotf(j)*h);
M0_0=.5;
M0(i,j,k)=fsolve(fun3,M0_0);
%Station 0%
u0(i,j,k)=M0(i,j,k)*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k)*(1/(pc(k)*pbR));
N_corr4(i,j,k)=turb_xaxis(k)/mdot_corr4(i,j,k);
if pc(k)>15.6
break
end
if Tt4(i,j,k)>2200
break
end
if N_corr2(k)>25000
break
end
if M0(i,j,k)<0 && M0(i,j,k)>2.4
break
end
figure
hold on
scatter(M0(i,j,k),F(i,j,k))
hold off
figure
hold on
scatter(M0(i,j,k),spillage(i,j,k))
hold off
figure
hold on
scatter(M0(i,j,k),N_corr4(i,j,k))
hold off
end
end
end
  2 commentaires
dpb
dpb le 18 Mar 2019
Overload!!!!
We don't have your data, don't "know nuthnk!" about your problem space so have essentially "no chance!" to fix your problem as given.
As a start, I would recommend you compute what data you need and get it organized so you can retrieve that which needs to be plotted together by addressing it either as indices in arrays or by variables in a table or other data structure.
Once you have that, decide what is to be plotted on a given figure and arrange to create that number of figures and pull the necessary data for each...
Trying to do everything in one set of deeply nested loops is just too much complexity to keep straight.
Stephen23
Stephen23 le 18 Mar 2019
Modifié(e) : Stephen23 le 18 Mar 2019
Start by moving your scatter after the two inner loops, and preallocate the plotted data arrays before the two inner loops (pseudocode):
for ii = 1:3
A = nan(...); % preallocate
B = nan(...); % preallocate
C = nan(...); % preallocate
...
for jj = 1:5
for kk = 1:8
.... your code
end
end
figure()
subplot(...)
scatter(A,...)
subplot(...)
scatter(B,...)
subplot(...)
scatter(C,...)
end
That will give you exactly nine plots in three figures.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 18 Mar 2019
If you only want plots for the three ‘i’ indices, I would remove the scatter calls from the innermost loop, change them to plot calls, and put them in the outermost loop instead:
for i=1:3
for j=1:15
for k=1:8
... CODE ...
end
end
figure
hold on
plot(squeeze(M0(i,:,:)),squeeze(F(i,:,:)))
hold off
figure
hold on
plot(squeeze(M0(i,:,:)),squeeze(spillage(i,:,:)))
hold off
figure
hold on
plot(squeeze(M0(i,:,:)),squeeze(N_corr4(i,:,:)))
hold off
end
The scatter function wants only vectors, however plot will accept 2D matrices. The squeeze function eliminates the first (‘i’ dimension), so plot will not throw an error on matrices with dimensions greater than 2.
NOTE — This is UNTESTED CODE. I can’t run your code to check this, however some experiments I did suggest that this will work.
Experiment to get the result you want.
  2 commentaires
Jacey Allen
Jacey Allen le 18 Mar 2019
Thank you so much! This gives me the 9 plots I need.
Star Strider
Star Strider le 18 Mar 2019
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by