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?

采纳的回答

Ameer Hamza
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 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