A function that computes the sum of a geometric series.

조회 수: 33 (최근 30일)
Patrick Bradford
Patrick Bradford 2017년 3월 14일
편집: Jan 2017년 3월 30일
A FUNCTION that computes the sum of a geometric series 1 + r + r^2 + r^3 + r^4 + ... + r^n, for a given r and N. THe input to the function must be 'r' and 'n'
Not sure what I am doing wrong, but I was trying to take baby steps and work it into a function but that didn't execute.
% create a vector with n elements all identical to r
v = r*ones(1,n);
% calculate [r r^2 r^3….r^n]
v = cumprod(v);
% sum and add one
geoSum = 1 + sum(v);
  댓글 수: 1
John D'Errico
John D'Errico 2017년 3월 14일
편집: John D'Errico 2017년 3월 14일
Please get used to using the "{} Code" button to format your code.
1. Paste in the code in the edit window.
2. Click on the "{} Code" button.
Your code will now be readable.
I've fixed your code this time.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2017년 3월 14일
편집: John D'Errico 2017년 3월 14일
cumprod is a great idea. Well done for thinking of it.
You state that your function did not execute. This may be because it does not have a function header. All you have written is a script.
Have you confused n and N? The code you wrote assumes n.
So how did you call it? What error do you get? Show the full text of the error.
You've made a good start in what you did, so some variations that should work:
function S = geosum1(r,n)
S = sum(cumprod([1,r*ones(1,n)]));
function S = geosum2(r,n)
S = sum(cumprod([1,repmat(r,1,n)]));
It can also be done without cumprod.
function S = geosum3(r,n)
S = sum(r.^(0:n));
Of course, you can break it into multiple lines. That makes the code more readable, and MATLAB does not charge extra if you use an extra line. For example:
function S = geosum4(r,n)
% sum of a geometric series, up to r^n, as
% 1 + r + r^2 + ... + r^n
% Note there will be n+1 terms in the series.
% generate a vector to be then prodded together
v = [1,r*ones(1,n)];
% use cumprod, instead of using exponents
% to compute each term as r^k
p = cumprod(v);
% sum the terms
S = sum(p);
Comments are very important, as they help you to understand what you wrote, when you are forced to debug code written a year ago (or 30 years ago.) As well, too often once finds code written by a colleague, that you need to use. It can be crucial to be able to understand and follow their code if you will then use it and trust it.
Any of the above schemes will work. To verify that fact, we can even do this:
geosum3(sym('r'),3)
ans =
r^3 + r^2 + r + 1
  댓글 수: 2
Jan
Jan 2017년 3월 14일
편집: Jan 2017년 3월 15일
+1. I like your style of teaching, which is useful for the readers.
And for completeness: There is a formula to determine the sum without accumulating the terms:
if r == 1
S = n + 1;
else
S = (1 - r ^ (n + 1)) / (1 - r)
end
John D'Errico
John D'Errico 2017년 3월 14일
Yes. This is the classical solution for the sum of a geometric series, which is well worth understanding the derivation of, as the concept will appear more than once as a student learns mathematics.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John BG
John BG 2017년 3월 14일
Hi Patrick
clear all,clc
N=3
r=3
R=[1:1:r]
sr=0
for k=1:1:r
sr=sr+R(k).^[1:1:N]
end
result=sum(sr)
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help,
please click on the thumbs-up vote link,
thanks in advance
John BG
  댓글 수: 10
John D'Errico
John D'Errico 2017년 3월 17일
편집: John D'Errico 2017년 3월 17일
"It's correct when n and r are vectors."
No. It is not correct as you wrote it in your answer. Stop insisting that you could never possibly make a mistake, and think about what you wrote.
I find it interesting that you claim that r could be a vector. Yet your solution has completely invalid code when r is indeed a vector. Will the code that you wrote even execute if n or r are vectors? Even for scalar r and n, will it yield the correct answer? Try your own code. Think about what you see as a result.
And stop suggesting who should be marked the correct answer, yours or anyone.
Need I say it again? Answers is not a power struggle. It is not a place for those who would bully or control others into marking your answer as correct. It is not a race to see who can get every possible "point"awarded to you.
Answers is here to help the person who asks a question, or for the person who might look for an answer in the future. Please stop trying to make it into your narcissistic quest for glory.
Jan
Jan 2017년 3월 17일
편집: Jan 2017년 3월 30일
John BG, try it:
r = rand(1,3);
N = 3:5;
% r and N are vectors. Now run your code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by