Nested for loop problem

조회 수: 1 (최근 30일)
Justin Hayes
Justin Hayes 2020년 5월 9일
답변: Ankriti Sachan 2020년 5월 12일
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