Can someone explain how to plot y[n]

I'm very new to MATLAB and I need someone to explain how to plot y[n] over the range -2<n<6. These are the equations I am given:
x[-1]=1
x[0]=2
x[1]=2
x[2]=1
x[3]=4
y[n]=x[n]+y[n-1};
-2<n<6
I have tried but my problem is that I can't use negative indexes in arrays

2 Comments

  • matlab indexing starts at 1
  • how is x(n) defined for n > 4
  • how is y(n-1) defined?
  • x(n) for n>4 isnt defined, I just assumed that every value of x(n) other that x(-1) to x(3) is zero.
  • y(n-1) isn't defined either, I also just assumed that y(-2) is zero

Sign in to comment.

Answers (1)

It's not defined unless you define it and again, Matlab arrays are 1-based (and, unfortunately, inflexible in that).
To handle a case such as this where the axis is negative, you must keep the indices as 1:N but instead of using the value of the timestep or increment as the index, keep a second vector of time values as well as the y vector.
t=-2:6; % the time steps (or use _n_ if prefer)
Now evaluate y for each value of t, then
plot(t,y)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 25 Feb 2015

Answered:

dpb
on 25 Feb 2015

Community Treasure Hunt

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

Start Hunting!