How can I define the recursive function?

Consider the relations

$$g_{k}(x) = c(x)\cdot g_{k-1}(x)+d(x), \quad f_{k}(x) = a(x)\cdot f_{k-1}(x)+c(x) g_{k-1}(x)$$ With $$g_{0} = G, f_{0}=F$$.

How can I get an arbitrary element $$g_{k}(x)$$ using Mathematica syntax?

• g[k_,x_]:=If[k>0,c[x]*g[k-1,x]+d[x],G];g[4,Pi]
– Bill
Jun 25 '20 at 22:19
• You could you Nest[c[x] # + d[x] &, g0[x], k] or you could solve this problem with a RecurrenceTable Jun 25 '20 at 22:19

Using RSolve

Clear["Global`*"]

sol = RSolve[{g[k] == c*g[k - 1] + d, f[k] == a*f[k - 1] + c*g[k - 1],
g == G, f == F}, {f, g}, k][] /. {a -> a[x], c -> c[x],
d -> d[x]};

f[k_, x_] = f[k, x] /. sol // FullSimplify

(* (F a[x]^(2 + k) (-1 + c[x]) - a[x]^(1 + k) (-1 + c[x]) (F + (F - G) c[x]) +
a[x]^k (-1 + c[x]) c[x] (F - G + d[x]) +
a[x] c[x] (d[x] - c[x]^k (G (-1 + c[x]) + d[x])) +
c[x] (-c[x] d[x] + c[x]^k (G (-1 + c[x]) + d[x])))/((-1 + a[x]) (a[x] -
c[x]) (-1 + c[x])) *)

g[k_, x_] = g[k, x] /. sol // FullSimplify

(* (-d[x] + c[x]^k (G (-1 + c[x]) + d[x]))/(-1 + c[x]) *)

Verifying,

{g[k, x] == c[x]*g[k - 1, x] + d[x],
f[k, x] == a[x]*f[k - 1, x] + c[x]*g[k - 1, x],
f[0, x] == F, g[0, x] == G} // Simplify

(* {True, True, True, True} *)

The recursion strategy seems independent of the x dependence, so I omit it.

My solution is not efficient, but there are many explanations of how to define Factorial recursively for efficiency (storing values already computed). I omit that small step for clarity too. The Simplify below is not necessary.

Define

g = G; f = F
g[k_Integer] := Simplify[c*gk[k-1] + d]
f[k_Integer] := Simplify[a+f[k-1]+c*g[k-1]]

Then

f //InputForm

Gives

2*a + F + c^2*G + c*(d + G)

Rather than Integer, you could test for positive integers.

Bob's answer is better as it solves the recurrence relations once and for all. Depends on what you want.