Sum of a geometric series help

조회 수: 5 (최근 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