In: Electrical Engineering
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)')
You have already done the code completely except a small misunderstanding. When they say, Create a second vector t which is equal to ten integers from 1 to 10 it means that just create integers (1 2 3 ...10) not random numbers using randi.You need to just change that line and, in the plot it is plot(t,x), move the figure inside loop and you are done.
clc;
close all;
clear all;
% 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=1:10;
% Plotting 'x' versus 't' in figure 1
figure(1),plot(t,x);
% 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)');
% Creating legend
legendInfo{i} = ['iteration number = ' num2str(i)];
end
legend(legendInfo);
hold off;
Output image:
Kindly note that the output image will ary every time you run the program because it is a random sequence.