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 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