How to create a function to find the sum of a geometric series?

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

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'

Sign in to comment.

Answers (1)

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

How can I do this without the symbolic math toolbox? I don't have access to syms
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.

Sign in to comment.

Asked:

on 6 Feb 2017

Edited:

on 1 Nov 2017

Community Treasure Hunt

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

Start Hunting!