Clear Filters
Clear Filters

How on earth to graph discrete time signals?

3 views (last 30 days)
I'm trying to plot this discrete time function in MATLAB. I missed the last several of my classes due to sickness and as such have quite literally no idea what I'm doing. This is as far as I've gotten-
n = -5:1:5;
x = ((1 / 7) ^ -n) * heaviside(-n) + ((2 / 3) ^ n) * heaviside(n) + ((3/5) ^ n) * heaviside(n);
figure(1);
plot(t,y, '-b')
axis( [-1 10 0 1]);
This has been slowly cobbled together from other forum posts, and as such I don't understand it and it doesn't work. I'm pretty quickly running out of things to try. Any help?

Accepted Answer

Star Strider
Star Strider on 8 Sep 2021
The only problems were in not using element-wise operations (use the ‘dot operator’ with the exponentiation an d multiplication operators), and presenting the correct arguments to plot. With those corrections (the only ones I made to the posted code), the original code works. See Array vs. Matrix Operations to understand about element-wise operations.
n = -5:1:5;
x = ((1 / 7) .^ -n) .* heaviside(-n) + ((2 / 3) .^ n) .* heaviside(n) + ((3/5) .^ n) .* heaviside(n);
figure(1);
plot(n, x, '-b')
Another way to plot this is to use the stem fucntion —
figure(2);
stem(n, x, '-b', 'filled')
That sometimes makes more sense when plotting discrete values.
..
  3 Comments
Star Strider
Star Strider on 8 Sep 2021
Thank you!
(For the record, I’m a physician. That’s what we do!)
.
Paul
Paul on 8 Sep 2021
Edited: Paul on 8 Sep 2021
One needs to be careful using the heaviside() function for discrete time problems. Unless the sympref is changed from the default, we see that
heaviside(0)
ans = 0.5000
Consequently, the code returns 1.5 at n=0.
n = -5:1:5;
x = ((1 / 7) .^ -n) .* heaviside(-n) + ((2 / 3) .^ n) .* heaviside(n) + ((3/5) .^ n) .* heaviside(n);
[n;x]
ans = 2×11
-5.0000 -4.0000 -3.0000 -2.0000 -1.0000 0 1.0000 2.0000 3.0000 4.0000 5.0000 0.0001 0.0004 0.0029 0.0204 0.1429 1.5000 1.2667 0.8044 0.5123 0.3271 0.2094
However, it's possible, perhaps even likely, that @Alex Miller's course uses the typical convention (at least in signals and controls) that u[0] = 1, in which case the result should be x[0] = 3. In either case, beware of using the heaviside() function for discrete time problems; make sure that heavisde(0) is consistent with the definition of the unit step function in the problem statement.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!