Nested for loop problem

1 次查看(过去 30 天)
Justin Hayes
Justin Hayes 2020-5-9
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.

回答(1 个)

Ankriti Sachan
Ankriti Sachan 2020-5-12
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by