Plotting nested for loop
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Ameer Hamza
2020-5-8
编辑:Ameer Hamza
2020-5-8
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!