Plotting nested for loop
16 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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
More Answers (0)
See Also
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!