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]