92.7k views
1 vote
Given a sequence of any size called x.(example: x=[ 2 3 −1 4 −5 7 −8 9 4 3 7 8];)

Write code to:
a) Find the length of the sequence and call it N.
b) Make two overlapped plots where the horizontal axis values should run from 0 to N−1, the plots should be as follows:
- Plot the sequence x values using the MATLAB stem command using red color.
- Hold that plot, and plot the same sequence again but using the plot command and blue color. This should give overlapped plots of different colors.
- Include a title line that should read: Example of Stem Versus Plot Commands using MATLAB. (use any font).
- Include a legend somewhere in the plot-box that reads:
Red Color: stem
Blue Color: plot
Include code and results of your plotting efforts.

User Btmcnellis
by
8.2k points

1 Answer

1 vote

Final answer:

To create overlapped plots in MATLAB, utilize the 'stem' command for a red-colored plot, followed by 'plot' command in blue. The length of the sequence 'N' is used as the range for the horizontal axis. Add a title and legend for clarity.

Step-by-step explanation:

To plot a sequence x using MATLAB and to obtain two overlapped plots - one using stem command in red and another using plot command in blue - you can follow the below MATLAB code:


% Define the sequence x
x = [2 3 -1 4 -5 7 -8 9 4 3 7 8];

% Find the length of the sequence N
N = length(x);

% Create a horizontal axis from 0 to N-1
t = 0:N-1;

% Start the first plot with stem command in red color
stem(t, x, 'r');
hold on; % Keep the plot for overlapping the next plot

% Add the plot command in blue color
plot(t, x, 'b');

% Add title and legend
title('Example of Stem Versus Plot Commands using MATLAB');
legend('Red Color: stem', 'Blue Color: plot');

% Turn off hold
hold off;

This code initializes the sequence x, calculates its length N, creates a time vector t for the horizontal axis, uses stem command to create the first plot, then holds it and adds the second plot using the plot command. It finishes by adding a title and a legend to the graph.

User Glanden
by
8.4k points