3
$\begingroup$

I am not very experienced with Mathematica. I want to calculate the series from recurrence solutions. These solutions have no closed-form. I want to improve the approximation and hence need a good number of terms. I know I can use RecurrenceTable command in Mathematica, but it is not computational efficient(calculations are going on from 2 days). I know this is because of the reason that every-time it has to calculate one new term, it has to go for all the previous terms.
The function looks like:

$$\text{RecurrenceTable}\left[\left\{p(n+1)=\frac{2 \beta p(n)+2 p(n-1)-\alpha q(n)}{n+1},q(n+1)=\frac{\alpha p(n)+2 \beta q(n)+2 q(n-1)}{n},p(0)=-\frac{2 \beta }{\alpha },p(1)=\frac{\alpha ^2+4 \beta ^2}{\alpha },q(0)=-1,q(1)=0\right\},\{p(n),q(n)\},\{n,0,100\}\right] $$

I want to know what would be the best way to calculate the same.

$\endgroup$
  • $\begingroup$ You need memoization. Check out a similar problem here. And, does you parameters, $\alpha$ etc. have any numeric values? $\endgroup$ – corey979 Oct 3 at 0:45
4
$\begingroup$

As mentioned by corey979, memorization will speed up the calculations

Clear["Global`*"]

p[0] = -2 β/α; p[1] = (α^2 + 4 β^2)/α;
p[n_Integer?Positive] := p[n] =
   (2 β*p[n - 1] + 2 p[n - 2] - α*q[n - 1])/n // Simplify;

q[0] = -1; q[1] = 0;
q[n_Integer?Positive] := q[n] =
   (α*p[n - 1] + 2 β*q[n - 1] + 2 q[n - 2])/(n - 1) // Simplify;

AbsoluteTiming[p[50];]

(* {36.9799, Null} *)

Almost all of the time was spent doing the simplifications.

Length@DownValues[p]

(* 52 *)

Since all but the last of the corresponding q values were already calculated in evaluating the p values, q values go much faster.

AbsoluteTiming[q[50];]

(* {0.853231, Null} *)

Length@DownValues[q]

(* 52 *)

Looking at examples

p@10

(* (1/(1625702400 α))β (30563 α^10 - 
   2 α^8 (962581 + 480430 β^2) + 
   8 α^6 (4280439 + 3642776 β^2 + 205756 β^4) + 
   64 α^4 (-2925591 - 2408022 β^2 + 870180 β^4 + 
      283784 β^6) - 
   256 α^2 (-980385 + 250644 β^2 + 2610624 β^4 + 
      1030352 β^6 + 88976 β^8) + 
   28672 (-945 + 2130 β^2 + 8520 β^4 + 4368 β^6 + 
      688 β^8 + 32 β^10)) *)

q@10

(* (1/3657830400)(4096 α^10 - α^8 (335872 + 
     556135 β^2) + α^6 (8454144 + 26321138 β^2 + 
     5401420 β^4) + 
  8 α^4 (-9424896 - 39546855 β^2 - 9479064 β^4 + 
     1439780 β^6) - 
  64 α^2 (-3283968 - 15113973 β^2 + 5114814 β^4 + 
     7943308 β^6 + 1088920 β^8) + 
  256 (-483840 - 2446233 β^2 + 6587220 β^4 + 7425312 β^6 + 
     1797968 β^8 + 114064 β^10)) *)
$\endgroup$
  • $\begingroup$ Thanks a lot for your help. $\endgroup$ – Shankar_Dutt Oct 3 at 21:51
2
$\begingroup$

I recommend using Do in place of RecurrenceTable. It should be faster and now memoization is automatic.

With[{m = 3},
  Clear[p, q];
  p[0] = -2 β/α;
  p[1] = (α^2 + 4 β^2)/α;
  q[0] = -1;
  q[1] = 0;
  Do[
    p[n + 1] = (2 β p[n] + 2 p[n - 1] - α q[n])/(n + 1); 
    q[n + 1] = (α p[n] + 2 β q[n] + 2 q[n - 1])/n,
    {n, 1, m}]]

Then

Definition[p]

p_values

Definition[q]

q_values

Going up yo n = 100 symbolically will take a lot memory and fair amount of time.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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