How do we test for infinite series convergence or divergence in MATLAB
Show older comments
I am very new to MATLAB and still learning the ropes. I am looking for a generic method that tests a series for convergence or divergence for infinite series. This is what I tried (I am using MATLAB online):
syms n
term = 1/n^.5;
y = vpa(symsum(term, n, 1, inf));
if isinf(y)
fprintf('divergent\n');
else
fprintf('convergent\n');
end
But this seems to give an incorrect answer! Can someone please explain how I can test for a generic series convergence in MATLAB?
14 Comments
Dyuman Joshi
on 2 Oct 2023
But the sum of square root of Natural numbers, which corresponds to zeta(1/2), is convergent.
Did you mean to test for the sum of negative 5th power of Natural numbers?
Someone Someone
on 2 Oct 2023
Someone Someone
on 2 Oct 2023
sum_{n=1}^{n=Inf} 1/n^alpha
converges for alpha > 1 and diverges for alpha <= 1.
John D'Errico
on 2 Oct 2023
@Someone Someone - please be nice to people who are trying to help you. A comment is NOT an answer, merely a comment on your question in an attempt to resolve a point of confusion.
Someone Someone
on 2 Oct 2023
Someone Someone
on 2 Oct 2023
syms n
term = 1/n^.5;
s = symsum(term, n, 1, inf)
y = vpa(s)
Verify statement by @Dyuman Joshi
z = zeta(sym(1)/sym(2))
vpa(z)
So vpa certainly seems to be doing something more than just coming up with a VPA value for s using ordinary VPA arithmetic. And if vpa "knows" the result (if we can call it that) is zeta(1/2), why doesn't symsum?
But this works as expected
s = symsum(n,n,1,inf)
As opposed to being not evaluated and then being able to use vpa(s) to get zeta(-1) = -1/12
zeta(sym(-1))
which seeemed to be all the rage on the internet a few years ago.
Someone Someone
on 2 Oct 2023
Based on my very limited experience with symsum, I'm not so sure that I would rely on it coming up with a finite, closed-form expression as a test for convergence. That is, I'm pretty sure that I've seen symsum not find a closed-form expression when I know one exists. But then again, my use of symsum typically involves expressions that contain variables in addition to the summing variable, which is different than what you're trying to do. YMMV.
I don't know whether or not to think this is a bug. It's certainly unexpected and undocumented behavior of vpa IMO, and I'd like to know why vpa seems to be so clever (and if it will produce other "unexpected" results). vpasum also seems to be more clever than symsum
syms n
term = 1/n^.5;
s = vpasum(term, n, 1, inf)
Even though
vpasum(term,n,1,1e5)
To be clear, I don't like how vpa and vpasum work for this problem.
Really strange.
syms n
s = symsum(1/n^sym('0.5'),n,1,Inf)
s = symsum(1/n^0.5,n,1,Inf)
syms alpha
s = symsum(1/n^alpha,n,1,Inf)
assume(real(alpha)<=1)
s = symsum(1/n^alpha,n,1,Inf)
Indeed, very peculiar!

syms n
Test #1: integer 0
s = symsum(1/n^sym('0'), n, 1, Inf)
z = zeta(sym('0'))
Test #2: decimal form
s = symsum(1/n^sym('0.5'), n, 1, Inf)
z = zeta(sym('0.5'))
Test #3: fraction form
s = symsum(1/n^sym('1/2'), n, 1, Inf)
s = vpa(s)
z = zeta(sym('1/2'))
z = vpa(z)
Test #4: integer 1
s = symsum(1/n^sym('1'), n, 1, Inf)
z = zeta(sym('1'))
I only have a guess explanation for your examples. First define alpha.
syms n integer
alpha1 = sym(1)/sym(2)
alpha2 = sym('0.5')
We see that alpha1 and alpha2 aparently have two different internal represnetations, even though they are both of type sym. alpha1 looks like a symbolic reprensentation of "one half" while alpha2 looks like a VPA form of the decimal representation of the number 1/2. Using the former
S1 = symsum(1/n^alpha1,n,1,inf)
Using the latter
S2 = symsum(1/n^alpha2,n,1,inf)
My guess, and it's only a guess, is that because alpha2 is a VPA variable, the call to symsum actually results in a call to vpasum under the hood
V2 = vpasum(1/n^alpha2,n,1,inf)
And with alpha1 we get the same result, presumably because vpa(alpha1) == alpha2.
isAlways(vpa(alpha1) == alpha2)
V1 = vpasum(1/n^alpha1,n,1,inf)
But the situation gets messier, i.e., inconsistent, if we treat alpha as a parameter and delay evaluation of S, either as a symfun with a parameter or as a sym expression followed by subs.
Define S as a symfun
syms alpha real
S(alpha) = symsum(1/n^alpha,n,1,inf)
Evaluate S at alpha1.
S(alpha1)
It returns NaN, which is consistent with its piecewise definition, but is inconsistent with S1.
Evaluate S at alpha2.
S(alpha2)
Consistent with S(alpha1), which is good, but inconsistent with S2.
Using subs with sym expressions yields the same results
S = symsum(1/n^alpha,n,1,inf)
subs(S,[alpha1 alpha2])
Define V(alpha) using vpasum
V(alpha) = vpasum(1/n^alpha,n,1,inf)
Evaluate and show consistency with V2
V(alpha1)
V(alpha2)
So vpasum appears to be self-consistent in these cases, even if the result is sketchy IMO.
OTOH, symsum seems to be a mess.
The more I think about this, the more surprised I am that, IF zeta(1/2) is an acceptable answer for this problem, then symsum doesn't yield zeta(1/2) but vpasum does. IOW, if anything I'd expect symsum would have all of the fancy rules and tables for symbolic sums, whereas vpasum would just add up the terms until meeting criteria for either convergence or divergence.
Presumbaly this call using vpa is actually implemented using vpasum.
term = 1/n^sym(.5)
s = symsum(term,n,1,inf);
vpa(s)
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


