1
$\begingroup$

I have a second-order recurrence equation that I want to plot. I've used two different methods.

The first uses RecurrenceTable, the second uses a traditional For loop. The two don't give the same answer. It's the For loop that gives the right result. Why does RecurrenceTable fail?

My code:

Clear["Global`*"];

a0 = 1; a1 = 2; a2 = 1;
b0 = 1; b1 = -1.8; b2 = 0.81;

sol = RecurrenceTable[
   Reduce[{x[n + 2] == x[n + 1], 
     y[n + 2] == 
      a0 x[n + 2] + a1 x[n + 1] + a2 x[n] - b0 y[n + 2] - 
       b1 y[n + 1] - b2 y[n], x[0] == 1, y[0] == 0, 
     y[1] == 1}, {x[n + 2], y[n + 2]}], y, {n, 0, 100}, 
   DependentVariables -> {x, y}];

ListPlot[sol, Joined -> True, PlotRange -> Full]

capN = 100; (*N is a predefined MMA function so use capN instead*)
x = Table[1, {capN}];
y = Table[0, {capN}];
For[n = 3, n <= capN, n++, 
 y[[n]] = a0*x[[n]] + a1*x[[n - 1]] + a2*x[[n - 2]] - 
          b0*y[[n]] - b1*y[[n - 1]] - b2*y[[n - 2]]]

ListPlot[y, Joined -> True] 
$\endgroup$

2 Answers 2

3
$\begingroup$
Clear[yy]

a0 = 1; a1 = 2; a2 = 1; b0 = 1; b1 = -1.8; b2 = 0.81;
nn = 100;
x = Table[1, nn];
y = Table[0, nn];

Do[y[[n]] = 
  a0*x[[n]] + a1*x[[n - 1]] + a2*x[[n - 2]] - b0*y[[n]] - 
   b1*y[[n - 1]] - b2*y[[n - 2]], {n, 3, nn}]

y == RecurrenceTable[{yy[1] == 0, yy[2] == 0, 
   yy[n] == a0 + a1 + a2 - b1*yy[n - 1] - b2*yy[n - 2]}, 
  yy, {n, 1, nn}]

ListLinePlot[
 RecurrenceTable[{yy[1] == 0, yy[2] == 0, 
   yy[n] == a0 + a1 + a2 - b1*yy[n - 1] - b2*yy[n - 2]}, 
  yy, {n, 1, nn}]]

True

enter image description here

$\endgroup$
4
  • $\begingroup$ Thank you very much, azerbajdzan. $\endgroup$
    – Pascal77
    Commented Apr 17 at 7:26
  • $\begingroup$ In fact, there is a mistake in the recurrence equations. The good ones are : ``` y[[n]] = (a0/b0)*x[[n]] + (a1/b0))*x[[n - 1]] + (a2/b0)*x[[n - 2]] - (b1/b0)*y[[n - 1]] - (b2//b0)*y[[n - 2]]] ``` and ``` y[n + 2] == (a0/b0) x[n + 2] + (a1b0 x[n + 1] + (a2/b0) x[n] - (b1b0) y[n + 1] - (b2b0) y[n] ``` $\endgroup$
    – Pascal77
    Commented Apr 17 at 8:46
  • 1
    $\begingroup$ @Pascal77 It is your mistake not mine. My post correctly answers your question with the original equation. $\endgroup$ Commented Apr 17 at 8:58
  • $\begingroup$ yes, you are right. It is my mistake and your answer was very relevant. Thank you again. $\endgroup$
    – Pascal77
    Commented Apr 17 at 9:04
2
$\begingroup$

…It's because Set (=) and Equal (==) are different.

As mentioned in the document of RecurrenceTable:

RecurrenceTable[eqns, expr, {n, nmax}] generates a list of values of expr for successive n based on solving the recurrence equations eqns.

But y[[n]] = a0*x[[n]] + a1*x[[n - 1]] + a2*x[[n - 2]] - b0*y[[n]] - b1*y[[n - 1]] - b2*y[[n - 2]]] is not a recurrence equation. Why? Because both sides involve y[[n]], and the y[[n]] will be overwritten based on the old y[[0]] in the calculation. A much simpler example showing the same issue:

y = 123;
y = y + 1
(* 124 *)
y == y + 1
(* False *)

"OK, then how to fix the code? " It can be fixed in various manner of course, one possible solution is:

Clear[x, y, n]

yold[n_] = 0;
x[n_] = 1;

sys = {y[n + 2] == a0  x[n + 2] + a1  x[n + 1] + a2  x[n] - 
                   b0  yold[n + 2] - b1  y[n + 1] - b2  y[n], 
       y[0] == 0, y[1] == 0};

RecurrenceTable[sys, y, {n, 0, 100}] // ListPlot

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you very much for your answer, xzczd. In fact, there is a mistake in the recurrence equation. The good one is the following one : y(n)=(a0/b0)*x(n)+(a1/b0)*x(n-1)+(a2/b0)*x(n-2)-(b1/b0)*y(n-1)-(b2/b0)*y(n-2). $\endgroup$
    – Pascal77
    Commented Apr 17 at 7:25

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