5
$\begingroup$

Bug introduced in 5.0, persisting through 14.0.


Using the RSolve function to find the general term of the sequence is as follows:

sol = First@
   RSolve[{a[1] == 1, a[2] == 2, 
     a[n + 2] == (2 + RealAbs[Cos[(n π)/2]]) a[n] - 
       RealAbs[Sin[(n π)/2]]}, a[n], n] // FullSimplify
a[n_] = a[n] /. sol

After running it, use the Table function to view the first 100 terms of the sequence,The result is incorrect::

enter image description here

The reason why the sum of the first 2024 terms of this sequence is incorrect?

Using the method from the above URL by @Daniel Huber to find the first 100 terms yields the correct results:

a[1] = 1
a[2] = 2
a[n_] /; n > 2 := 
 a[n] = (2 + RealAbs[Cos[((n - 2)    π)/2]])   a[n - 2] - 
   RealAbs[Sin[((n - 2)    π)/2]]

enter image description here


By comparing the two methods, the general term formula obtained using the RSolve function gives incorrect results for all odd-numbered terms, while the even-numbered terms are correct.

Why is the general term of the sequence obtained using the RSolve function incorrect?

$\endgroup$
4
  • 1
    $\begingroup$ It is some kind of bug. $\endgroup$ Apr 15 at 12:47
  • 1
    $\begingroup$ This seems bad practice: a[n_] = a[n] /. sol. $\endgroup$
    – Michael E2
    Apr 15 at 12:52
  • 1
    $\begingroup$ Consider sol[[1, 2]] /. n -> 3 This evaluates to 2, what is wrong. I would mail this to "[email protected]" $\endgroup$ Apr 15 at 12:53
  • $\begingroup$ I think RSolve has a problem with the fact that the sequence consist in fact of two independent sequences. Odd terms depend only on previous odd term and even terms only on previous even term. But that does not change the fact that it is a bug. $\endgroup$ Apr 15 at 13:09

3 Answers 3

4
$\begingroup$

I think it's safe to say it's a bug. The following is the minimal sample I can find:

Clear[a];
sys = {a[1] == 1, a[n + 2] == Cos[(n π)/2] a[n]};
parta[n_] = a[n] /. First@RSolve[sys, a[n], n]
(*
(1/QPochhammer[-1, 1, 1/2])2^(-n/2) E^(
 1/8 I (-2 + n) n π) ((-1)^(1/8) Sqrt[2] + 
   C[2] QPochhammer[-1, 1, 1/2] + (-1)^
    n C[2] QPochhammer[-1, 1, 1/2]) QPochhammer[-1, 1, n/2]
 *)

Those familiar with QPochhammer may already notice that the parta doesn't make sense: QPochhammer[-1, 1, 1/2] doesn't converge according to the definition mentioned in the document. Quick check:

qp[a_, q_, n_] := qp[a, q]/qp[a  q^n, q]
qp[a_, q_] := Product[1 - a  q^k, {k, 0, Infinity}]

qp[-1, 1]

Product::div: Product does not converge.

(* Product[2, {k, 0, Infinity}] *)

If we combine parta with FullSimplify to force simplification, we obtain the follows:

parta[Range[1, 12, 2]] // FullSimplify
(* {1, I, 1, I, 1, I} *)

This is not too surprising, because as mentioned in Possible Issues section of document of FullSimplify, FullSimplify may give incorrect result.

For comparison, the following is the correct result obtained with RecurrenceTable

RecurrenceTable[sys~Join~{a[2] == ccc}, a, {n, 12}][[1 ;; ;; 2]]
(* {1, 0, 0, 0, 0, 0} *)

BTW, the bug is introduced in version 7:

enter image description here

The result in version 5 is also incorrect, but in another manner:

enter image description here

It's in version 5 that RSolve is introduced i.e. RSolve cannot handle this type of problem well from the beginning. Please report this to WRI.

$\endgroup$
5
$\begingroup$

The workaround:

$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear[a];
a[1] = 1;
a[2] = 2;
a[n_] := a[n] =
  (2 + RealAbs[Cos[((n - 2)  π)/2]])  a[n - 2] - 
   RealAbs[Sin[((n - 2)  π)/2]]

seq = a /@ Range[20];

Clear[a2];
a2[n_] = FindSequenceFunction[seq, n] // FullSimplify

(* 1/2 (1 - (-1)^n) + 3^(-1 + n/2) (1 + (-1)^n) *)

Clear[a3];
a3[n_] = Piecewise[{Simplify[a2[n], #], #} & /@ {Mod[n, 2] == 0, 
    Mod[n, 2] == 1}]

enter image description here

For a continuous function,

a4[n_] = a2[n] /. (-1)^n :> Cos[n*Pi]

(* 1/2 (1 - Cos[n π]) + 3^(-1 + n/2) (1 + Cos[n π]) *)

All of the functions are equal for positive integers.

And @@ Table[a[n] == a2[n] == a3[n] == a4[n], {n, 40}]

(* True *)

Graphically,

Show[
 LogPlot[a4[n], {n, -1, 10},
  PlotRange -> All,
  PlotLegends -> Placed[
    {HoldForm[a4[n]]}, {.3, .7}]],
 DiscretePlot[a2[n], {n, 0, 10},
  PlotStyle -> Red,
  ScalingFunctions -> "Log",
  PlotRange -> {0.5, 120},
  Filling -> 0.5,
  PlotLegends -> Placed[
    {HoldForm[a2[n]]}, {.31, .8}]]]

enter image description here

$\endgroup$
4
$\begingroup$

As I wrote it the comment of OP - even terms are independent of odd terms and that is what probably cause a problem to RSolve but that is not an apology for a bug.

The sequence is riffle of two independent sequences:

RecurrenceTable[{a[1] == 1, a[n + 1] == 2 a[n] - 1}, a, {n, 1, 10}]
RecurrenceTable[{a[1] == 2, a[n + 1] == 3 a[n]}, a, {n, 1, 10}]

Riffle[%%, %]
% == RecurrenceTable[{a[1] == 1, a[2] == 2, 
   a[n + 2] == (2 + RealAbs[Cos[(n π)/2]]) a[n] - 
     RealAbs[Sin[(n π)/2]]}, a, {n, 1, 20}]

{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

{2, 6, 18, 54, 162, 486, 1458, 4374, 13122, 39366}

{1, 2, 1, 6, 1, 18, 1, 54, 1, 162, 1, 486, 1, 1458, 1, 4374, 1, 13122, 1, 39366}

True
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.