A function that computes the sum of a geometric series.
85 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Patrick Bradford
am 14 Mär. 2017
Bearbeitet: Jan
am 30 Mär. 2017
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 Kommentar
John D'Errico
am 14 Mär. 2017
Bearbeitet: John D'Errico
am 14 Mär. 2017
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.
Akzeptierte Antwort
John D'Errico
am 14 Mär. 2017
Bearbeitet: John D'Errico
am 14 Mär. 2017
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 Kommentare
John D'Errico
am 14 Mär. 2017
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.
Weitere Antworten (1)
John BG
am 14 Mär. 2017
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 Kommentare
John D'Errico
am 17 Mär. 2017
Bearbeitet: John D'Errico
am 17 Mär. 2017
"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.
Siehe auch
Kategorien
Mehr zu Surrogate Optimization finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!