# Recursive function, if and which statements

I am given this question: Let a function $$T_n(x)$$ be defined by: $$T_{n+1}(x) = \frac{1}{x} T_{n-1}(x) - \frac{2}{7} T_n(x),$$ where $$T_0(x) = 1, T_1(x) = x$$.

I need to construct a recursive function for $$T_n(x)$$, one way by using If and Which and another way using function overloading.

t[n_, x_] := Which[n = 0, 1, n = 1, x, n > 1, 1/x*(t[n - 2]) - 2/7*(t[n - 1])]

First, I have made this function, however, I'm not sure how to use it such that it will store the values for $$T_1$$ and $$T_0$$, at the moment it doesn't store these and therefore won't calculate the value of $$T_n$$.

Thanks!

• How can I use function overloading to solve this? – 12345 Feb 9 at 11:52

## 4 Answers

Clear["Global`*"]

eqns = {t[n + 1] == 1/x t[n - 1] - 2/7 t[n], t[0] == 1, t[1] == x};

For comparison, use RSolve to obtain the general solution

sol = RSolve[eqns, t, n][[1]];

t[n] /. sol

(* (1/(2 Sqrt[
49 + x]))7^-n (-Sqrt[x] (-1 - Sqrt[49 + x]/Sqrt[x])^n -
7 x^(3/2) (-1 - Sqrt[49 + x]/Sqrt[x])^n +
Sqrt[49 + x] (-1 - Sqrt[49 + x]/Sqrt[x])^n +
Sqrt[x] (-1 + Sqrt[49 + x]/Sqrt[x])^n +
7 x^(3/2) (-1 + Sqrt[49 + x]/Sqrt[x])^n +
Sqrt[49 + x] (-1 + Sqrt[49 + x]/Sqrt[x])^n) *)

Verifying the result

eqns /. sol // Simplify

(* {True, True, True} *)

Defining with Which

Clear[t]

t[n_Integer?NonNegative] := t[n] =
Which[
n == 0, 1,
n == 1, x,
n > 1, 1/x*(t[n - 2]) - 2/7*(t[n - 1])]

m = 8;

seq = t /@ Range[m] // Simplify

(* {x, 1/x - (2 x)/7,
1 - 2/(7 x) + (4 x)/49, -(4/7) + 1/x^2 + 4/(49 x) - (8 x)/343, (-1372 +
2345 x + 588 x^2 + 16 x^3)/(
2401 x^2), -(32/343) + 1/x^3 + 12/(49 x^2) - 2042/(2401 x) - (32 x)/
16807, (-100842 + 106673 x + 57400 x^2 + 3920 x^3 + 64 x^4)/(
117649 x^3), -((-823543 - 403368 x + 913752 x^2 + 191632 x^3 + 9408 x^4 +
128 x^5)/(823543 x^4))} *)

Comparing the results with the general solution from RSolve

seq == ((t /. sol) /@ Range[m]) // Simplify

(* True *)

Using FindSequenceFunction to generalize from the sequence

sol2 = FindSequenceFunction[seq, n]

(* (1/(2 Sqrt[
49 + x]))7^-n (-Sqrt[x] (-1 - Sqrt[49 + x]/Sqrt[x])^n -
7 x^(3/2) (-1 - Sqrt[49 + x]/Sqrt[x])^n +
Sqrt[49 + x] (-1 - Sqrt[49 + x]/Sqrt[x])^n +
Sqrt[x] (-1 + Sqrt[49 + x]/Sqrt[x])^n +
7 x^(3/2) (-1 + Sqrt[49 + x]/Sqrt[x])^n +
Sqrt[49 + x] (-1 + Sqrt[49 + x]/Sqrt[x])^n) *)

Comparing the RSolve result with the FindSequenceFunction result

(t /. sol)[n] == sol2

(* True *)

You can find the exact solution directly:

RSolve[{t[n + 1] == t[n - 1]/x - 2/7 t[n], t[0] == 1, t[1] == x}, t[n], n] // FullSimplify

(*    {{t[n] -> (7^-n (Sqrt[49 + x] (-1 - 1/Sqrt[x/(49 + x)])^n -
Sqrt[x] (1 + 7 x) (-1 - 1/Sqrt[x/(49 + x)])^n +
Sqrt[49 + x] (-1 + 1/Sqrt[x/(49 + x)])^n +
Sqrt[x] (1 + 7 x) (-1 + 1/Sqrt[x/(49 + x)])^n))/(2 Sqrt[49 + x])}}    *)

RSolve might be the best worker for this job and also do not forget RecurrenceTable.

But here I offer a solution via the method of transfer matrix. Denoting $$v_n = (T_{n-1}\quad T_n)^\top$$ so that $$v_1 = (1\quad x)^\top$$, and the transfer matrix $$M$$, $$M = \begin{pmatrix} 0 & 1 \\ 1/x & -2/7 \end{pmatrix},$$ the recursive relation can be rewritten as $$v_{n+1} = M\cdot v_n = M^2\cdot v_{n-1} = \cdots = M^n\cdot v_1.$$

So now programming gets involved, the final expression of $$T_n$$ can be directly obtained by MatrixPower:

T[0, x] = 1;
T[1, x] = x;
T[n_, x_] := T[n, x] = 1/x T[n - 2, x] - 2/7 T[n - 1, x]

T[4, x] // Simplify