A function that computes the sum of a geometric series.
88 次查看(过去 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
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
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 个评论
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
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!