recursion – How to solve for multiple (cascading) recurrence relations

I am working with recurrence relations with a simple base:

begin{equation}
Y_i = aY_{i-1} + (1-a)X_i quad mbox{and if $Y_0=0$ then} quad Y_n = (1-a)sum_{i=1}^n a^{i-1} X_i tag{1}
end{equation}

here, $X_i$ are identically-distributed independent random variables (although I do not want to specify a distribution type). I am then interested to characterise the series $Y_n$ (its moments, correlation structure, etc.) in terms of those of $X$ and the parameter $a$.

I have done this by hand for (1) and have obtained relationships between $mathbf{E}(X_n)$ and $mathbf{E}(Y_n)$, $mathbf{Var}(X_n)$ and $mathbf{Var}(Y_n)$, $mathbf{skew}(X_n)$ and $mathbf{skew}(Y_n)$, $mathbf{Kurt}(X_n)$ and $mathbf{Kurt}(Y_n)$ and so on, up to order 6 moments. The algebra becomes rather tedious, but just about manageable.

However, I am now interested to feed this recurrence through a subsequent equation:

begin{equation}
Z_{j} = bZ_{j-1} + (1-b)Y_j quad mbox{and if $Z_0=0$ then} quad Z_n = (1-b)sum_{j=1}^n b^{j-1} Y_j tag{2}
end{equation}

and then find its moments. Having done that, I want to do it again.

My question is this: how (can ?) I use Mathematica to help me do this?

python – Why do online compilers/interpreters use different limits for the maximum recursion depth error?

When using two different online Python compilers/interpreters for executing a program (this one, and this one), I found that both sites showed the maximum recursion depth error at $n = 998$, whereas the one which I had downloaded from python.org showed the error at $n = 1024$. Is there a reason why online compilers/interpreters have lower maximum recursion depth limits?

For example, this recursive function (which displays the first $n$ terms in the Fibonacci sequence) shows a recursion error at $n = 998$ in an online compiler/interpreter.

def fib_memo(n):
    if n in fib_cache:
        return fib_cache(n)
    if n == 1:
        value = 1
    elif n == 2:
        value = 1
    elif n > 2:           
        value =  fib_memo(n-1) + fib_memo(n-2)
    fib_cache(n) = value
    return value
fib_cache = {}
print (fib_memo(998))

But it shows a recursion error at $n = 1024$ in the Windows Python application.

python – Why do online compilers/interpreters use different limits for the maximum recursion depth error?

When using two different online Python compilers/interpreters for executing a program (this one, and this one), I found that both sites showed the maximum recursion depth error at $n = 998$, whereas the one which I had downloaded from python.org showed the error at $n = 1024$. Is there a reason why online compilers/interpreters have lower maximum recursion depth limits?

recursion – Find the min in an array and increment it by X with pure functions and no variable

Thanks for contributing an answer to Computer Science Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

Using an integral to solve a recursion

  1. This is about the Akrabazzi method, will work if the number of $a cdot f(n/b)$ is not finite? Meaning that we have a sum that depends on our input $n$ ?

What about this:

$$ f(n) = sum_{l=1}^{n} f(n/3^l) $$

Can I say that we need to find the $p$ so that

$$ sum_{j=1}^{n} (frac{1}{3^j})^p =1$$

And because $n rightarrow infty$ we can say it is an infinite geometric sum? Or because $n$ is our input we cant say that… And the akrabazzi fails to deal with this kind of stuff? There is no internet site I found that says $k$ (which in wikipedia is the number of $f(n/b)$ we have needs to be finite.

  1. Moreover, can I use the fact that:
    $$Theta( f(n)+g(n)) = Theta( max{ f(n) + g(n)})$$ to be only for $f(n)$‘s?

Meaning that if we have $$f(n)=f(n-1) + f(n-2) + f(n-3) + dots$$

$$Theta(f(n)) = Theta(f(n-1)) + Theta(f(n-2)) + dots $$

Or this is invalid as it is a sum of $f$ and not some other function $g$

Thank you!

differential equations – n-dimensional recursion

I am trying to set up a recursive function that generates n number of differential equations for Subscript(y, n)(t)

This function almost works.

Table({Subscript(y0, j) = 1}, {j, 50});(*initial conditions for Subscript(y, n) assuming n<=50*)
vars(n_) := {x, Table(Subscript(y, j), {j, n})};
sol(0)(T_, b_, d_, r_, n_) := 
 sol(0)(T, b, d, r, n) = 
  Flatten(vars(n)) /. 
   NDSolve(Flatten@{Join({x'(t) == 
          10^7 r - d x(t) - b*x(t)*(Sum(Subscript(y, k)(t), {k, n}))},
         Table(Subscript(y, j)'(t) == -d Subscript(y, j)(t) + 
           b x(t)*Subscript(y, j)(t), {j, n}), 
        Flatten(Join({x(0) == 0}, 
          Flatten(Table({Subscript(y, j)(0) == Subscript(y0, j)}, {j, 
             n})))))}, Flatten(vars(n)), {t, 0, T})((1))
sol(i)(T_, b_, d_, r_, n_) := 
 sol(i)(T, b, d, r, n) = 
  Flatten(vars(n)) /. 
   NDSolve(Flatten@{Join({x'(t) == 
          10^7 r - d x(t) - b*x(t)*(Sum(Subscript(y, k)(t), {k, n}))},
         Table(Subscript(y, j)'(t) == -d Subscript(y, j)(t) + 
           b x(t)*Subscript(y, j)(t), {j, n}), 
        Flatten(Join({x(0) == 0},(*next bit seems to be the problem*)
          Flatten(Table({Subscript(y, j)(0) == 
              sol(i - 1)(T, b, d, r, n)((j + 1))(T)}, {j, n})))))}, 
     Flatten(vars(n)), {t, 0, T})((1))

The initial condition sol(0)(T, b, d, r, n) works as expected and returns the interpolating functions:

T = 4; b = 10^-7; d = 0.25; r = 0.2; n = 4;
sol(0)(T, b, d, r, n) 

And also returns the value at t = T e.g.
sol(0)(T, b, d, r, n)((2))(4)

sol(i)(T, b, d, r, n) does not work for i>0.

It seems that the problem is where the solution from the previous iteration i-1 is used to set the initial conditions for current iterate i, as marked in the code.

I imagine this will be trivial to troubleshoot for someone on here. Any advice is much appreciated.

Generate Recursion Tree of Fibonacci Sequence

I’m using this example to study Mathematics.Example Demo. In this example, before generating recursion tree, she flats the function. But I have a difficultly to understand how flatten works, especially the 3rd line of the code below. Could someone teach me? Thank you very much.

f[___, 0] := {};
f[___, 1] := {};
f[m___, p_] := {{m, p} -> {m, p, p - 1}, {m, p} -> {m, p, p - 2}, //How to understand this line?
f[m, p, p - 1], f[m, p, p - 2]}
j[n_] := f[n] // Flatten

recursion – Iterative and recursive approach to generate Fibonacci sequence

I’m new to use Mathematica and using this demo project to understand Mathematic demo example.
I modified example code to only plot the runtime of recursive version of Fibonacci sequence runtime:

Clear[recFib];
recFib[n_] := recFib[n] = recFib[n - 1] + recFib[n - 2]
recFib[0] = 0; recFib[1] = 1;

timeComplex[n_] := 
    Plot[{recFib[x], x}, {x, 0, n}, PlotRange -> {{0, 10}, {0, 100}},
    PlotStyle -> {Thickness[0.01], Thickness[0.01]}, 
    PlotLabel ->  "time complexity", AxesLabel -> {n, time}, 
    PlotLegends -> 
    Placed[LineLegend[{"recursive"}, 
       LegendFunction -> "Frame", 
       LegendLayout -> "Column"], {{0.25, 0.5}, {0.25, 0.1}}], 
       ImageSize -> {200, 200}]


    Manipulate[
       GraphicsGrid[{{timeComplex[n]}}, Alignment -> {Center, Top}, 
       Frame -> All], {{n, 2, "Fibonacci number"}, 2, 10, 1, 
       Appearance -> "Labeled"}, ControlPlacement -> Top, 
       SaveDefinitions -> True]

However, the output graph still contains iterative runtime of Fibonacci. Could someone teach me where it comes from? Thanks.

recursion – What is a loop mathematically?

Here is a word problem

For every dollar Ben spent on bagels, David spent 25 cents less. Ben
paid $12.50 more than David. How much did they spend in the bagel store
together?

My programmer mentality said, Oh, this is a loop. Just loop on

x - .75x

incrementing x each step until the accumulator is ~ 12.50. That turns out to be x = 50. Then work backwards to get the other part, etc…

I know there is a close connection between recursion and math’s induction, but what have I got here mathematically? Is this a limit problem? It also looks like a fixed point issue. I don’t know enough about Analysis and series, but is this a type of series? In general, what part of math is a loop going toward an “eplison”?

recursion – Solution of differential equation using Differential transfrom method

U(0)=0
For(k=0,k<=n,k=k+1,
Print(k(k+1)Y(k+1)+2(k+1) Y(k+1)-(2 (Pi)^(k+1))/Factorial(k) Cos((k Pi)/2)+Pi^(k+1)/Factorial(k-1) Sin(((k-1)Pi)/2), “=”,0) (*prints all components *)
)

Using above code in Mathematica we generate individual components in Y(k) form.
Please can any one suggest me to write a recursive mathematica code, so that it usues initial conditions
and generate all numerical answer.