Sum of a geometric series help

25 views (last 30 days)
Chris
Chris on 30 Sep 2013
Answered: Matt J on 30 Sep 2013
(a) Write a function file that accepts the values of r, a and n as arguments and uses a for loop to return the sum of the first n terms of the geometric series. Test your function for a = 3, r = 1=2 and n = 10. (b) Write a function file that accepts the values of r, a and n as arguments and uses the built in command sum to find the sum of the first n terms of the geometric series. Test your function for a = 3, r = 1=2 and n = 10. Hint: Start by defining the vector e=0:n-1 and then evaluate the vector R = r.^e. It should be easy to figure out how to find 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

Answers (1)

Matt J
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.

Community Treasure Hunt

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

Start Hunting!