Nested for loop needed

Hi, I am trying to use nested for loop resuly(K),Ms(K), and Temp(i)
p1 = [6.78,2.79,9.8,11.93,7.81,8.83,5.81,12.85,10.94,7.84,5.29,4.83,9.77];
T1 = [23,23,22.5,23,22,22,22,22.5,22.5,22.5,24.5,22.5,24];
for K = 1:length(p1)
for i = 1:length(T1)
p4 = 29.85;
fun = @(p2) p2/p4*(1 -((gamma-1)*(p2/p1(K)-1))./(2*gamma*(2*gamma+(gamma+1)*(p2/p1(K)-1))).^.5).^(-2.*gamma./(gamma-1))-1;
result(K) = fzero(fun,10)/p1(K) ;
Ms(K) = sqrt (((gamma+1)/(2.*gamma))*(result(K)-1)+1);
fun1 = @(T2) (result(K)*T1(i)/T2)*(((gamma+1)/(gamma-1))+result(K))/(1+(((gamma+1)/(gamma-1))*result(K)))-1;
Temp(i) = fzero(fun1,10);
end
end
%However, it somehow finds untill 8th column of result(K) and Ms(K) and cannot even find Temp(i).
%It gives me an error of this "Function value at starting guess must be finite and real" for Temp(i) = fzero(fun1,10).
%So, I was wondering how I could fix to obtain every value [1x13] for result(K), Ms(K), and Temp(i).

3 commentaires

darova
darova le 7 Mar 2020
Function value at starting guess must be finite and real
Looks like fun returns NaN or Inf somewhere at p2=10
Can't check without gamma
Sun Kyoo Choi
Sun Kyoo Choi le 7 Mar 2020
gamma is 1.4 fixed.
darova
darova le 7 Mar 2020
Try to change initial guess (increase)
result(K) = fzero(fun,p1(K)+10)/p1(K) ;

Réponses (1)

Walter Roberson
Walter Roberson le 7 Mar 2020

1 vote

p1 = [6.78,2.79,9.8,11.93,7.81,8.83,5.81,12.85,10.94,7.84,5.29,4.83,9.77];
T1 = [23,23,22.5,23,22,22,22,22.5,22.5,22.5,24.5,22.5,24];
fun_guess = 20;
fun1_guess = 10;
for K = 1:length(p1)
for i = 1:length(T1)
p4 = 29.85;
fun = @(p2) p2/p4*(1 -((gamma-1)*(p2/p1(K)-1))./(2*gamma*(2*gamma+(gamma+1)*(p2/p1(K)-1))).^.5).^(-2.*gamma./(gamma-1))-1;
result(K) = fzero(fun,fun_guess)/p1(K) ;
Ms(K) = sqrt (((gamma+1)/(2.*gamma))*(result(K)-1)+1);
fun1 = @(T2) (result(K)*T1(i)/T2)*(((gamma+1)/(gamma-1))+result(K))/(1+(((gamma+1)/(gamma-1))*result(K)))-1;
Temp(i) = fzero(fun1,fun1_guess);
end
end
However, notice that each iteration of for i you write into Temp(i) which is a location independent of K, so for example Temp(7) would reflect only the last time Temp was written into, when K was the final value. You are not saving Temp for each combination of p1 and T1.
Likewise you write to result(K) and Ms(K) independent of the value of i, so for any given K value, the value saved in result or Ms will reflect only the value written there on the last for i iteration. You are not saving result or MS for each combination of p1 and T1.

1 commentaire

Sun Kyoo Choi
Sun Kyoo Choi le 8 Mar 2020
Then, is it just a matter of location of Temp(i)?
Because when I do a hand calculation, the range of temp should be around 20-30, but the results give me around 3e-06 ...

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by