Plotting nested for loop

16 views (last 30 days)
Justin Hayes
Justin Hayes on 8 May 2020
Commented: Ameer Hamza on 8 May 2020
time_range = 1:10;
y = zeros(1,10);
hold on
for x = 0.5:0.5:1
for t = 1:1:length(time_range)
x = 0.5;
y(t) = x .* t;
end
plot(time_range,y)
grid on
xlabel('Time (seconds)')
ylabel('J/s')
legend('Q skin')
title('Q over time')
end
I would like to have 2 separate graphs. One graph for x = 0.5 and one graph for x = 1. When I run the code I only get one graph. How do I fix this?

Accepted Answer

Ameer Hamza
Ameer Hamza on 8 May 2020
Edited: Ameer Hamza on 8 May 2020
Remove this line
x = 0.5;
from the for-loop.
Correct code is
time_range = 1:10;
y = zeros(1,10);
hold on
for x = 0.5:0.5:1
for t = 1:1:length(time_range)
y(t) = x .* t;
end
plot(time_range,y)
grid on
xlabel('Time (seconds)')
ylabel('J/s')
legend('Q skin')
title('Q over time')
end
  2 Comments
Justin Hayes
Justin Hayes on 8 May 2020
Thank you very much!
Ameer Hamza
Ameer Hamza on 8 May 2020
I am glad to be of help.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!