Info
此问题已关闭。 请重新打开它进行编辑或回答。
Nested for loop needed
1 次查看(过去 30 天)
显示 更早的评论
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).
回答(1 个)
Walter Roberson
2020-3-7
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 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!