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
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
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 CenterFile Exchange 中查找有关 Loops 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