Summing An infinite Series

93 views (last 30 days)
Hi !
I'm a little new to MATLAB but am trying to use it to check the values for part of an infinite sum I have to implement in Python.
This is the sum in question.
And this is my code:
function the_sum = p_sum(l,m,n,alpha,beta,gamma)
syms p;
the_sum = syssum(factorial(l+m+n+p+2)/(factorial(l+1+p)*(l+m+2+p)) * (alpha/(alpha + beta + gamma))^p,p,0,Inf);
end
But when I run it I get an error ! I imagine I'm doing something rather simple that is wrong and would be so grateful if someone could point me in the right direction !
  1 Comment
David Hill
David Hill on 4 Mar 2020
Are l, m,n, alpha, beta, gamma all scalar? Why not do the calculation numerically? What are the ranges for your scalars?

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 4 Mar 2020
Edited: John D'Errico on 4 Mar 2020
I might start by using the correct function. In MATLAB, that is symsum, not syssum. :) These dumb computers, they refuse to read our minds.
You don't tell us what error you got. It is almost always best to show the ENTIRE error message, as that will usually tell what the problem is immediately. Here, it should probably have said something about syssum not existing. But that is only a wild guess on my part, since you also don't show enough to really test what you are doing.
As for doing the computation itself in numerical form, you could probably play around with Stirlings approximation for those factorials. However, symsum seems to handle it well enough.
l = 2;
m = 1;
n = 2;
alp = .3;
bet = .7;
gam = .4;
symsum(factorial(l+m+n+p+2)/(factorial(l+1+p)*(l+m+2+p)) * (alp/(alp + bet + gam))^p,p,0,Inf)
ans =
3757404/14641
Be careful in defining variables named alpha, beta, and gamma, as they are already the names of useful functions in MATLAB. That will cause problems later on.
I would also strongly suggest that you NOT use a variable name of lower case L, thus l. As you can see, it is quite easily mistaken for the number 1, depending on the font used. One day, you will trip over a bug in your code, and not have a clue what is wrong, because you mistyped the variable name as a 1, or the opposite. As such, I STRONGLY recommend you avoid use of the variable names: lower case L (l), upper case i (I), or either case o or O.
  1 Comment
Charlie Hetherington
Charlie Hetherington on 5 Mar 2020
Thank-You ! The worse thing is that I had that moment of "Oh god, I've been an idiot" just after I submitted this ! Thanks also for the advice on variable names and 'l' misidentities - this is a brave new world from Python !

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!