For Loop That Uses A Found Value To Complete the Next Loop
1 次查看(过去 30 天)
显示 更早的评论
Hello! How can I use a for loop to run a code that uses a found value from the previous loop, to compute the next loop?
The base equation for this loop looks like
P2=(P1)(exp(-(z2-z1)/(R*T))
where Z has bounds a and b and has intervals of 50. P1 and T are referenced indicies of a known vector. The next iteration of the loop would then subsitute the previous P2 in for P1 and run again.
0 个评论
采纳的回答
VBBV
2023-5-1
编辑:VBBV
2023-5-1
Do you mean something like this ?
z2 = 10;
Z1 = 4;
P2 = zeros(1,length(P1));
for J = 1:Array_range_max
% P1 computed in this loop
P1 = rand(1,10);
% assume z1 and z2 are scalars
for k = 1:length(P1)-1
P2(k) = P1(k)*exp(-(z2-z1)./(R*T(k)));
P1(k+1) = P2(k);
end
end
3 个评论
VBBV
2023-5-2
z = 0:50:1700;
size(z)
P1 = 9.79419e4;
a = 29.3;
% assume average of (T_(2,:))+(T_(1,:)).\2
T = randi([0 240],341,1);
P2 = zeros(341,length(z));
%outer loop
% for Tavg = 1:array_size_temperature
% T = randi([0 240],341,1);
for J = 1:size(P2,2)
% P1 computed in this loop
P2(:,J) = P1.*exp(-z(J))./(a.*T);
P1 = P2(:,J);
end
%end // end of outer loop
P2
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!