# Sum of a geometric series help

25 views (last 30 days)
Chris on 30 Sep 2013
Answered: Matt J on 30 Sep 2013
(a) Write a function ﬁle that accepts the values of r, a and n as arguments and uses a for loop to return the sum of the ﬁrst n terms of the geometric series. Test your function for a = 3, r = 1=2 and n = 10. (b) Write a function ﬁle that accepts the values of r, a and n as arguments and uses the built in command sum to ﬁnd the sum of the ﬁrst n terms of the geometric series. Test your function for a = 3, r = 1=2 and n = 10. Hint: Start by deﬁning the vector e=0:n-1 and then evaluate the vector R = r.^e. It should be easy to ﬁgure out how to ﬁnd the sum from there.
Alright so it is asking me to write a function file but i dont know why i would need to write a function file since i can do it all with a for loop. Here is what i have
g=0;
a=3;
r=1/2;
for n=0:10
g=g+a*r^n
end
Please help cause i have no idea where to go from here or even why i would use a function file

Matt J on 30 Sep 2013
You "need" to because that is what you were asked to do. And because you don't want to repeatedly write the for-loop every time you have a new set of parameters a and r to test.