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

1 visualización (últimos 30 días)
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 comentarios
Jan
Jan el 4 de En. de 2019
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 el 4 de En. de 2019
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.

Iniciar sesión para comentar.

Respuestas (1)

Shane Gibbs
Shane Gibbs el 4 de En. de 2019
have you tried a while loop?
  1 comentario
Damian Sztangierski
Damian Sztangierski el 4 de En. de 2019
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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by