Extract Data from for loop

I am attempting to plot SFC and TSFC vs The PRC values based on each value of Mach. I am trying to find a way to extract all calculated values of SFC and TSFC from the for loop for plotting. Any help would be appreciated
U = Mach*sqrt(gamman*R*Ta);
Toa = Ta.*((1+(((gammad-1)/2).*(Mach^2))));
To2 = Toa;
To2sTa = (1+((etad.*((To2./Ta)-1))));
Po2 = Pa.*((To2sTa).^(gammad/(gammad-1)));
for PRC = 5:5:100
Po3 = PRC.*Po2;
To3 = To2.*(1+((1./etac).*(PRC.^((gammac-1)/gammac))-1));
for To4 = [1500 1600 1700]
Cpb = ((R*gammab)/(gammab-1));
f = ((To3.*((To4./To3)-1))./((QR/Cpb)-To4));
Po4 = Po3;
To5 = To4-To3+To2;
Po5 = (Po4.*(1-(1./etat)*(1-(To5/To4))).^(gammat/(gammat-1)));
U7 = sqrt((2*etan*R*(gamman/(gamman-1)))*To5*((1-(Pa/Po5)).^(gamman-1/(gamman))));
SFC = ((1+f)*U7-U)/1000; %((kN*s)/(kg))
TSFC = f*SFC; %((kg)/(kN*s))
end
end
end

1 Commento

Look at
doc meshgrid
for "the Matlab way" to evaluate a function over a rectangular grid

Accedi per commentare.

 Risposta accettata

Imran
Imran il 22 Ott 2014

0 voti

After
SFC = ((1+f)*U7-U)/1000; %((kN*s)/(kg));
Use
SFC = [[];SFC];
But define
SFC = [];
Before loop
Same for TSFC

1 Commento

dpb
dpb il 22 Ott 2014
Modificato: dpb il 22 Ott 2014
SFC = [[];SFC];
This is the most inefficient way possible; "growing" the array by constant reallocation.
If not going to use the builtin facility of Matlab to evaluate over the 2D mesh via meshgrid at least preallocate the array...
Before the loop add...
N=length(5:5:100); % length of one dimension
SFC(N,3)=0; % array of Nx3 created automagically by reference
and initialize the row,column indices...
iP=0;
for PRC = 5:5:100
iP=iP+1; % increment outer (row)
...
iT=0; % initialize inner (column)
for To4 = [1500 1600 1700]
iT=iT+1; %
...
SFC(iP,iT) = ((1+f)*U7-U)/1000; %((kN*s)/(kg))
...
But again, see
doc meshgrid
and can eliminate loops entirely

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 21 Ott 2014

0 voti

You either have to add indexes to the variables so that you're not overwriting the same thing each time, or else you have to put the call to plot() inside the loop. Which way do you want to do it?

5 Commenti

G
G il 21 Ott 2014
I would like to save the data but I wasn't sure how to do that.
Image Analyst
Image Analyst il 22 Ott 2014
I just returned to this now. It looks like you've accepted Imran's answer so I don't need to answer this. Good luck with that approach - though it's not what dpb and I would recommend.
dpb
dpb il 23 Ott 2014
"You can lead a horse to water but..." :(
Image Analyst
Image Analyst il 23 Ott 2014
The weird thing is SFC = [[];SFC]; doesn't even do anything. [] is null so it's the same as SFC = [SFC]; which is the same as SFC=SFC, which does absolutely nothing. Oh well, whatever.
Yeah, Imran intended to write
SFC=[];
for PRC = 5:5:100
...
for To4 = [1500 1600 1700]
...
SFC = [SFC;((1+f)*U7-U)/1000; %((kN*s)/(kg))];
...
I gave him a pass on that as he did say to initialize to null outside the loop that the following then was just a faux pas.
But the other thing bum about this solution besides the lousy performance is it's a 1D vector (of course, can fix that by reshape but as is it isn't particularly useful for OP's purpose and OP's experience level is likely such won't understand what result is where in the vector).

Accedi per commentare.

Categorie

Tag

Richiesto:

G
G
il 21 Ott 2014

Modificato:

il 15 Gen 2016

Community Treasure Hunt

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

Start Hunting!

Translated by