# Recursive Function with conditionals [duplicate]

I am to solve for $$T_{12}(4.8), T_{24}(1.2)$$, using If and Which functions.

I started with this function and keep getting a recursion limit error:

t[n_] := (7/2 x) t[n - 1] - (7/2) t[n + 1]

• RSolve may help. Feb 10, 2020 at 10:53
• because you haven't set a boundary condition. Feb 10, 2020 at 12:46
• It seems a duplicate question is posted. Please see my solution here. Feb 10, 2020 at 14:15

T[x_, n_] := Block[{temp = n}, Which[
temp == 0, Return, temp == 1, Return[x], temp > 1,
1/x T[x, temp - 2] - 2/7 T[x, temp - 1]]]


T[1, 4] gives

167/343

Or as what you want.

T[n_] := Block[{temp = n},
Which[temp == 0, Return, temp == 1, Return[x], temp > 1,
1/x T[temp - 2] - 2/7 T[temp - 1]]]
T /. x -> 1


Try

T = 1;
T = x;
T[n_] := (1/x) T[n - 2] - (2/7) T[n - 1];

• I would recommend T[n_] := T[n] = (1/x).... Without memoization, this recursion is going to be awfully inefficient. Feb 10, 2020 at 14:13

Try

Clear[T, n, x]
Tn = T[n] /. RSolve[{T[n + 1] == 1/x T[n - 1] - 2/7 T[n], T == 1, T == x}, T, n][]

Tn /. {n -> 12, x -> 4.8}

(* -0.0153785 *)