Sum of a geometric series help

6 次查看(过去 30 天)
Chris
Chris 2013-9-30
回答: Matt J 2013-9-30
(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

回答(1 个)

Matt J
Matt J 2013-9-30
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.

类别

Help CenterFile Exchange 中查找有关 Testing Frameworks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by