How to create a function to find the sum of a geometric series?
Show older comments
I want to create a function to find the sum of a geometric series and the number of terms is defined by n. I'm new to MATLAB so I haven't worked with this program a lot. My initial idea going in would be to create a loop depending on the variable n and find the sum for the values.
2 Comments
Sebastian
on 6 Feb 2017
Geoff Hayes already provided an answer to this question, see https://de.mathworks.com/matlabcentral/answers/136667-sum-of-geometric-series-without-loop
SHUBHAM PHOOLARI
on 1 Nov 2017
Edited: SHUBHAM PHOOLARI
on 1 Nov 2017
Let's make it simple for you
function s = gseriessum(r,n)
%function to calculate sum of geometric series
nvector = 1:n ; %create a vector from 1 to n
gseries = 1+r.^nvector ; %create vector of terms in series
s = sum(gseries);
%run the function gseriessum(r,n) on the command window with your inputs for 'r' and 'n'
Answers (1)
Walter Roberson
on 6 Feb 2017
Let the first term of the geometric series be C, and let each one after that be f times greater than the one before. So C, (C)*f = C*f, (C*f)*f = C*f^2, (C*f^2)*f = C*f^3 and so on. Then the K'th term is C*f^(K-1) if you number starting from 1, and you have
syms C f K N
simplify(symsum(C*f^(K-1),K,1,N))
There is an important geometric series identity that will get used here, giving the result
C*(f^N-1)/(f-1)
Now you can just substitute in the initial term and the multiple per term and the number of terms.
2 Comments
Adam Wanliss
on 6 Feb 2017
Walter Roberson
on 6 Feb 2017
result = C*(f^N-1)/(f-1) where C is the initial value, f is the multiple for the terms, and N is the number of terms.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!