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 Center および File ExchangeTesting Frameworks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by