Question

In: Electrical Engineering

Write a for loop from 1 to 3 using i as the variable. For each value...

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)')

Solutions

Expert Solution

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.


Related Solutions

Write a loop that subtracts 1 from each element in lowerScores. If the element was already...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. please bold the solution import java.util.Scanner; public class StudentScores { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int SCORES_SIZE = 4; int[] lowerScores = new int[SCORES_SIZE]; int i; for (i = 0; i < lowerScores.length; ++i) {...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
write a for loop that uses the loop control variable to take on the values 0...
write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10. Execute the program by clicking the Run button at the bottom of the screen. Is the output the same?
Checkpoint 8.1 Assume the variable name references a string. Write a for loop that prints each...
Checkpoint 8.1 Assume the variable name references a string. Write a for loop that prints each character letter in the string. The actual print statement is provided. # What goes here? print(letter) Checkpoint 8.11 Write an if statement using the in operator that determines whether 'd' is in my_string. A print statement that might be placed after the if statement is included. # What goes here? print('Yes, it is there.') Checkpoint 8.13 Write the first if statement to complete the...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
1.write a small program using a loop to add a series of numbers 2.write a function...
1.write a small program using a loop to add a series of numbers 2.write a function called "main" that performs several given steps. Be sure to call the main() function so that its code executes In python and doesn't have to be long. just long enough to do what it says. Thank you.
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while loop To Understand what the Fibonacci series is: The Fibonacci sequence is a series of numbers where a number is the sum of the previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Q2: Write a Java program to find the Factorial of a number using a while loop. To...
Write a C++ code using while loop which is getting the two integers from the user,...
Write a C++ code using while loop which is getting the two integers from the user, and output how many numbers are multiples of 5, and how many numbers are multiples of 7 between the two integers (inclusive).
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3};...
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3}; (NOTE: please consider braces {} here as usual square brackets for array index). Assume that i is stored in register $s1, and the base address of the array A is in $s2. Fill in the multiple blank spaces in the following MIPS program that is supposed to be compiled from the above C loop: loop: sll $t0, $s1,                add $t0, $t0,                lw $t1, 20($s2)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT