# MATLAB code help

Discussion in 'Homework Help' started by mojo_risin, Nov 9, 2014.

1. ### mojo_risin Thread Starter Member

Jul 3, 2013
35
0
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 (Text):
1. % Plots the signal x[n] = 3.8u[n + 7] in the interval -10 to 10
2. % Unfinished!
3. clear all
4. clc
5.
6. n = -10:1:10;
7. x = 3.8*((n+7)>=0);
8.
9. stem(n, x, 'filled');
10. axis([-10 10 -0.5 5.5]);
11. ylabel ('Magnitude');
12. xlabel ('Sample Number');
13. title ('Signal');
14. legend('x[n] = 3.8u[n + 7]');
and that code:

Code (Text):
1. % Plots the signal x[n] = 2.7d[n - 20] in the interval 0 to 25
2. % Unfinished!
3. clear all
4. clc
5.
6. n = 0:1:25;                                         % sample vector
7. x = 2.7*(n-20==0);            % signal vector
8.
9. stem(n, x, 'filled');
10. axis([0 25 -0.5 2]);
11. ylabel ('Magnitude');
12. xlabel ('Sample Number');
13. title ('Signal');
14. legend('x[n] = 2.7\delta[n - 20]');
Thanks!

2. ### shteii01 AAC Fanatic!

Feb 19, 2010
3,922
601
Is there a problem?

3. ### WBahn Moderator

Mar 31, 2012
20,069
5,665
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?

4. ### mojo_risin Thread Starter Member

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

5. ### WBahn Moderator

Mar 31, 2012
20,069
5,665
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]".

6. ### mojo_risin Thread Starter Member

Jul 3, 2013
35
0
Thanks a lot! Will do that, it will definitely look better.