How to graph discrete equation: y(n) = y(n-1) + 1 for 1000 samples

3 ビュー (過去 30 日間)
Laura Rosas
Laura Rosas 2021 年 3 月 10 日
コメント済み: Laura Rosas 2021 年 3 月 10 日
I need to graph this equation, and I have no idea how to, because as soon as I declare y(n) = y(n-1) + 1, it throws results until 1000+ not having any regards of the n value.
Code used:
n = 1;
x = [];
y (1) = 1;
while n <= 1000;
n = n+1;
x = [n,y];
y(n) = y(n-1) + 1;
end
stem(x);
  2 件のコメント
Rik
Rik 2021 年 3 月 10 日
You don't provide any details of what you did, so the only possible answer is this: write your code differently.
Have a read here and here. It will greatly improve your chances of getting an answer.
Laura Rosas
Laura Rosas 2021 年 3 月 10 日
I tried to update it, I included the code I’m using, the only info I was given, was that y=1 when n = 1 and was told to graph y(n) = y(n-1) + 1
I’m sorry, I’m really new at matlab

サインインしてコメントする。

採用された回答

Rik
Rik 2021 年 3 月 10 日
I would do something like this:
%create a vector of the correct size
y=NaN(1000,1); % by using NaN we should notice it if we skip a value
y(1) = 1;
for n=2:numel(y)
y(n)=y(n-1)+1;
end
%now we have a vector we can plot:
plot(y,'*')
In this case we could have taken a shortcut:
y=ones(1000,1);
y=cumsum(y);
plot(y,'*')
  1 件のコメント
Laura Rosas
Laura Rosas 2021 年 3 月 10 日
Thank you very much! It worked :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by