For Loop That Uses A Found Value To Complete the Next Loop

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.

 採用された回答

VBBV
VBBV 2023 年 5 月 1 日
編集済み: VBBV 2023 年 5 月 1 日

0 投票

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 件のコメント

Jordan David
Jordan David 2023 年 5 月 2 日
Hi VBBV! I did a poor job of communicating my needs. Apologies for that. This is my trial run in programming.
The base code looks like
P2=(P1).*exp(-(50)./(a.*T));
z is a column vector from 0 to 17000 at intervals of 50. So I subbed a constant of 50 into the equation above.
P1 starts out as a scalar of value 9.794199100000000e+04.
a is a constant of 29.3.
T is the average of two values taken from a 341 X 1 column vector of data. For example T=(T_(2,:))+(T_(1,:)).\2;
The goal is to use the above base code to find P2 and then subsitute that value in for a new P1 while running the matching T.
For example
P2=(9.794199100000000e+04).*exp(-(50)./(a.*T);
The first run of this would use the above figures and the T_(2,:) and T_(1,:) indicies.
The second run would take the P2 from the first and sub it in where the P1 slot is. And then the T would update to use indicies T_(3,:) and T_(2,:). This goes on until the loop reaches the upper bound of z=1700 which correlates to indice 341 of the column vector.
VBBV
VBBV 2023 年 5 月 2 日
z = 0:50:1700;
size(z)
ans = 1×2
1 35
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
P2 = 341×35
1.0e+03 * 0.0201 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.6714 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1453 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0152 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0189 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0154 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0156 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0376 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0655 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0293 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Jordan David
Jordan David 2023 年 5 月 6 日
Thank you for taking time out of your day to help me understand this!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2023 年 5 月 1 日

コメント済み:

2023 年 5 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by