How do I put the code below into one big loop so I do not have to copy and paste no more?

1 回表示 (過去 30 日間)
h = 0.01;
A = 8000/11;
T(1)=80;
x(1)=0;
prompt = ('Guess initial Z value');
Z(1) = input(prompt);
for i=2:5
x(i) = x(i-1)+h;
T(i) = T(i-1) + h*Z(i-1)
Z(i) = Z(i-1) + h*(A*(T(i-1)-20));
end
Z1(1) = Z(1) - Z(4)
T1(1)= 80
for j=2:5
x(j) = x(j-1)+h;
T1(j) = T1(j-1) + h*Z1(j-1)
Z1(j) = Z1(j-1) + h*(A*(T1(j-1)-20));
end
Z2(1) = Z1(1) - Z1(4)
T2(1)= 80
for k=2:5
x(k) = x(k-1)+h;
T2(k) = T2(k-1) + h*Z2(k-1)
Z2(k) = Z2(k-1) + h*(A*(T2(k-1)-20));
end
Z3(1) = Z2(1) - Z2(4)
T3(1) = 80
for p=2:5
x(p) = x(p-1)+h;
T3(p) = T3(p-1) + h*Z3(p-1)
Z3(p) = Z3(p-1) + h*(A*(T3(p-1)-20));
end
  2 件のコメント
Jan
Jan 2019 年 1 月 4 日
You forgot to mention, what you want to change inside the loop. But actually, either for or while will solve your problem.
Damian Sztangierski
Damian Sztangierski 2019 年 1 月 4 日
Thanks for your reply, what I would like to change is the initial condition for the next iteration. Right now I have to write Z(1), Z1(1), Z2(1) and so on manually, I need a for loop that does it for me.

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

回答 (1 件)

Shane Gibbs
Shane Gibbs 2019 年 1 月 4 日
have you tried a while loop?
  1 件のコメント
Damian Sztangierski
Damian Sztangierski 2019 年 1 月 4 日
I have had a go but when I use a while loop, the first expression requires values that have not yet been calculated which resulted in me getting an error

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by