Write a 'for' loop from 1 to 3 using 'i' as the variable. For each value of 'i', create a vector 'x' and vector 't' to plot.

17 views (last 30 days)
Write a 'for' loop from 1 to 3 using 'i' as the variable. For each value of 'i': Create a vector 'x' of 10 random numbers between 0 and 1. Create a second vector 't' which is equal to ten integers from 1 to 10. Plot 'x' versus 't' in figure 1. Use 'hold on' to keep each plot. Use a different color for the line for each value of 'i'. At the very end, add the text 'time' using 'xlabel' to the horizontal axis, and the text 'f(t)' using 'ylabel' to the vertical axis. This is what I have so far:
% Creating a 'for' loop from 1 to 3 using 'i' as the variable
for i=1:3
% For each value of 'i', creating a vector 'x' of 10 random numbers between 0 and 1
x=rand(1,10);
% For each value of 'i', creating a second vector 't' which is equal to 10 integers from 1 to 10
t=randi([1,10],10);
end
% Plotting 'x' versus 't' in figure 1
figure(1),plot(x,t)
% Using 'hold on' to keep each plot
hold on
% Using a different color for the line for each value of 'i'
%add code
% Adding the text 'time' using 'xlabel' to the horizontal axis
xlabel('time')
% Adding the text 'f(t)' using 'ylabel' to the vertical axis
ylabel('f(t)')

Answers (1)

James Tursa
James Tursa on 5 Mar 2018
Move the plotting and "hold on" inside the loop. For the t vector, the instructions did not say "random", so probably what was meant was just t = 1:10. And finally, since the xlabel is time and the ylabel is f(t), you should be using plot(t,x) instead of plot(x,t).

Community Treasure Hunt

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

Start Hunting!