MATLAB code help

Thread Starter

mojo_risin

Joined Jul 3, 2013
35
Hello

I am a first time MATLAB user, because of some homework. I have to plot some signals, and I am not sure If I am doing it as it is supposed to be. Please take a look at my code:

Code:
% Plots the signal x[n] = 3.8u[n + 7] in the interval -10 to 10
% Unfinished!
clear all
clc

n = -10:1:10;
x = 3.8*((n+7)>=0);

stem(n, x, 'filled');
axis([-10 10 -0.5 5.5]);
ylabel ('Magnitude');
xlabel ('Sample Number');
title ('Signal');
legend('x[n] = 3.8u[n + 7]');
and that code:

Code:
% Plots the signal x[n] = 2.7d[n - 20] in the interval 0 to 25
% Unfinished!
clear all
clc

n = 0:1:25;                                         % sample vector
x = 2.7*(n-20==0);            % signal vector

stem(n, x, 'filled');
axis([0 25 -0.5 2]);
ylabel ('Magnitude');
xlabel ('Sample Number');
title ('Signal');
legend('x[n] = 2.7\delta[n - 20]');
Thanks!
 

WBahn

Joined Mar 31, 2012
30,077
Have you tried running your code? Does it produce the results you expect? If not, what is wrong with what it does versus what you expect it to do?
 

Thread Starter

mojo_risin

Joined Jul 3, 2013
35
I believe it does; I have been proven wrong many times, though :) Are there any good practices, naming conventions or something like that?
 

WBahn

Joined Mar 31, 2012
30,077
You want your titles and labels to be informative and complete. Don't make the reader infer what is meant. So, for the axes, use a title that relates directly to the values on the axis and, where appropriate, include the units. In your case, simply saying "Sample Number" is probably sufficient for you x-axis, but in light of what I'm going to recommend next I would recommend using "n" or perhaps "n (sample #)". For your y-axis, just saying "magnitude" is not too unreasonable on a problem this simple, but in general it is too vague -- magnitude of what? It's the magnitude of x[n], so use "x[n]" as the axis title (which why you want "n" as the axis title of the x-axis". Since there are no units associated with this problem, you can leave them off. For the plot title, what you have, "Signal", is way too generic. It is what my old boss referred to as a "Horse Title", meaning that you have a figure of a horse and you use the title "A Horse" above it. Try to use a title that tells the reader something useful that is not obvious at first glance and that, if possible, it related to why this figure is there in the first place. For homework, at least use the identifier for the problem if nothing else, so something like "Problem 4.2(a)". In this case, you could put the equation in the title instead of the legend, so perhaps "x[n] = 3.8u[n + 7]". There's nothing that says you can't do both, "Problem 4.2(a): x[n] = 3.8u[n + 7]".
 
Top