MATLAB Answers

generate y(n)=y(n-1)+x(n)

255 views (last 30 days)
hello i want to generate y(n)=y(n-1)+x(n). by using a for loop I can find the last value of y, but i need to store all values of y from n =1 till n. Who do I do that?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 20 Apr 2017
n=10
y0=0; % Initial conditions
y(1)=y0+x(1);
for k=2:n
y(k)=y(k-1)+x(k)
end
  1 Comment
Marwan Malaeb
Marwan Malaeb on 20 Apr 2017
that is right, thanks Azzi.

Sign in to comment.

More Answers (1)

Roger Stafford
Roger Stafford on 20 Apr 2017
That is precisely what the matlab ‘cumsum’ function does:
y = cumsum(x);

Tags

Community Treasure Hunt

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

Start Hunting!