Sum of geometric series without loop
Ältere Kommentare anzeigen
Hey! For the function geo(r,varargin) i am trying to find the sum of the following
1 + r + r^2 + r^3 + r^4 + ... + r^n
where n = nargin.
I have searched and lurked at every corner both here and in the "help" in Matlab, but i found no way of "writing"/"setting up" the geometric series without loops, which is what i am trying to accomplish. Finding the sum i can do, but i cannot find a way to write above series without too much effort. Any ideas? What obvious built-in function am i missing?
Akzeptierte Antwort
Weitere Antworten (1)
Roger Stafford
am 20 Jun. 2014
A simpler method:
result = (1-r^(n+1))/(1-r);
2 Kommentare
Kenan Hoyt
am 21 Jun. 2014
Roger Stafford
am 21 Jun. 2014
It is easy to see why this works. Call the sum s:
s = 1 + r + r^2 + r^3 + r^4 + ... + r^n
Then
s*r = r + r^2 + r^3 + r^4 + r^5 + ... + r^(n+1)
Therefore when you subtract s*r from s, all the terms cancel except the first and last ones:
s - s*r = 1 - r^(n+1)
Hence if you divide both sides by 1-r you get
s = (1-r^(n+1))/(1-r)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!