How do we test for infinite series convergence or divergence in MATLAB

20 views (last 30 days)
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
convergent
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
Sam Chak
Sam Chak on 2 Oct 2023
Indeed, very peculiar!
syms n
Test #1: integer 0
s = symsum(1/n^sym('0'), n, 1, Inf)
s = 
z = zeta(sym('0'))
z = 
Test #2: decimal form
s = symsum(1/n^sym('0.5'), n, 1, Inf)
s = 
z = zeta(sym('0.5'))
z = 
Test #3: fraction form
s = symsum(1/n^sym('1/2'), n, 1, Inf)
s = 
s = vpa(s)
s = 
z = zeta(sym('1/2'))
z = 
z = vpa(z)
z = 
Test #4: integer 1
s = symsum(1/n^sym('1'), n, 1, Inf)
s = 
z = zeta(sym('1'))
z = 
Paul
Paul on 2 Oct 2023
I only have a guess explanation for your examples. First define alpha.
syms n integer
alpha1 = sym(1)/sym(2)
alpha1 = 
alpha2 = sym('0.5')
alpha2 = 
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)
S1 = 
Using the latter
S2 = symsum(1/n^alpha2,n,1,inf)
S2 = 
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)
V2 = 
And with alpha1 we get the same result, presumably because vpa(alpha1) == alpha2.
isAlways(vpa(alpha1) == alpha2)
ans = logical
1
V1 = vpasum(1/n^alpha1,n,1,inf)
V1 = 
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)
S(alpha) = 
Note that S(alpha) is defined using piecewise and the otherwiseVal is not specified.
Evaluate S at alpha1.
S(alpha1)
ans = 
NaN
It returns NaN, which is consistent with its piecewise definition, but is inconsistent with S1.
Evaluate S at alpha2.
S(alpha2)
ans = 
NaN
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)
S = 
subs(S,[alpha1 alpha2])
ans = 
Define V(alpha) using vpasum
V(alpha) = vpasum(1/n^alpha,n,1,inf)
V(alpha) = 
Evaluate and show consistency with V2
V(alpha1)
ans = 
V(alpha2)
ans = 
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)
term = 
s = symsum(term,n,1,inf);
vpa(s)
ans = 

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 2 Oct 2023
I suppose with some effort I could find a series where convergence is not known. But it is early in the morning, and mental effort is just asking too much. :)
I would point out that the series you show, thus
syms n
term = 1/n^.5;
y = symsum(term, n, 1, inf)
y = 
In fact, does diverge This is easy enough to show, since we can see it dominates the similar series symsum(1/n,1,inf) term by term. And we do know the harmonic series diverges.
But then you tried:
vpa(y)
ans = 
which is a finite value. In fact, it is the same value that @Dyuman Joshi pointed out, as
zeta(1/2)
ans = -1.4604
It looks like VPA merely looks at that sum, and recognizes that zeta(s) is the same as that sum for general s, as long as real(s)>1. And so it returns zeta(1/2). Since zera(1/2) is finite, your test failed to produce the result you expected.
That is what happens, though I cannot really excuse VPA for having made that choice. Arguably, something in that chain should produce a result that indicates divergence. Feel free to send it into tech zupport and suggest that VPA might have known better.
However, is there some simple test one can apply that will always robustly indicate convergence or not, for some completely general series? I don't know of one.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!