I am trying to recursively define a function which satisfies the following system of equations and which depends on two parameters $n$ and $l$,
$$ \begin{align} A(x, n, l) &= F[ A(x,n-1,l) ]\\ A(x, n, l) &= x^l \mbox{ if } l = n \\ A(x, n, l) &= 0 \mbox{ if } l < n \\ \end{align} $$
In order to do this I used the following code (here I make the expression for $F$ explicit):
A[x_, n_, l_]:=
Boole[n > l]((1 - x)/(1 + x^l) * (Sum[A[x, n-1-i, l] * x^i, {i,0,l-1}])
+ x^l/(1 + x^l)) + Boole[n == l] * x ^ l
The function defined in this way, $A(x, n, l)\;$, gives the probability that tossing a coin $n$ times gives exactly one run of $l$ consecutive $1$s, where $x$ is the probability getting $1$ on single toss and $(1-x)$ is the probability getting $0$ on single toss.
When I try to plot one of these functions, for instance,
Plot[A[x, 7, 7], {x, 0, 1}]
I encounter the following error:
RecursionLimit::reclim: Recursion depth of 256 exceeded.
Is my code wrong? Is there an other way to recursively define a function, taking into account the fact that the third condition must be satisfied for all the values $l<n$, where $l$ and $n$ are not specified at the moment of the definition.
NestWhile
to implement recursive functions. $\endgroup$A[x,0,l]
, and memoize your function, I think. $\endgroup$A[x,n-1,l]
as you write initially. Could you write out whatF[input_]
is supposed to be? $\endgroup$Boole[condition] * expr
causesexpr
to be evaluated whether condition isTrue
orFalse
. So when you evaluateA[x,7,7]
Mathematica will evaluate theSum
. The first term of theSum
isA[x,6,7]
, and of course evaluating that leads to aSum
whose first term isA[x,5,7]
and so on. $\endgroup$