Azzera filtri
Azzera filtri

Nested for loop problem

2 visualizzazioni (ultimi 30 giorni)
Justin Hayes
Justin Hayes il 9 Mag 2020
Risposto: Ankriti Sachan il 12 Mag 2020
time_range = 1:10
k_cotton = 0.04:0.02:0.08
for k = 1:length(k_cotton)
for t = 1:1:length(time_range)
insert multiple variables as a function of (t)
code.....
Q(k) = k_cotton(k) .* numbers
Tskin_forearm(t) = Q .* t .* Tskin_forearm(t-1)
code......
end
figure(1)
plot(time_range, Tskin_forearm)
grid on
xlabel('Time (seconds)')
ylabel('Temperature (Kelvin)')
legend('T skin .04','T skin .06','T skin .08')
title('Temperature of forearm over time')
end
I believe that when running the nest loop, values from k = 0.04 are being carryed over and used to calculate values for k = 0.06. I think this is because Tskin_forearm(t-1) from k =0.04 is being used to begin the k = 0.06 loop. How do you prevent the values from being used in the second and third part of the nest loop? Thank you.

Risposte (1)

Ankriti Sachan
Ankriti Sachan il 12 Mag 2020
From your query, what I understood is that the value of Tskin_forearm(t-1), calculated where k=1 and k_cotton=0.04, is being used in the second iteration of the external loop, where k=2 and k_cotton=0.06.
If this understanding is correct, then to solve your issue, you can probably keep storing the value of Tskin_forearm(t), in some variable completely outside the loops, immediately after the internal loop is completed for k_cotton=0.04. Also, reset the variable Tskin_forearm(t) before beginning the internal loop for k_cotton=0.06.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by