Questions tagged [recursion]

For questions about defining recursive functions, recursive algorithms and solving recursive equations.

For questions about defining recursive functions (tail or central embedding), recursive algorithms and solving recursive equations. This can involve custom made recursive formulae (e.g. f[x_]:=f[x-1]) or built-in functions to solve e.g. recurrence equations (RSolve to solve equations of the form a[n + 1] - 2 a[n] == 1), recursive replacement (ReplaceRepeated), or recursive application (Nest, FixedPoint, etc.).

Useful links:

Example questions:

654 questions
9
votes
1 answer

What do RecursionLimit and IterationLimit actually set?

The following recursive functions respectively calculate the length of a Short-cut Collatz Conjecture series and perform a pointless demonstration recursion: colLength[m_] := Module[{}, Return[If[m == 1, 1, 1 + colLength[If[EvenQ[m], m/2, (3*m +…
Jerry Guern
  • 4,602
  • 18
  • 47
8
votes
1 answer

Defining a recursive sequence

I want to define a recursive sequence and then ask Mathematica to print a specific value: Am I doing something wrong?
the_fox
  • 307
  • 1
  • 2
  • 7
7
votes
4 answers

Iterating a rational function

I'm reading about iterating rational functions. I'm given $$R(z)=\frac{3z-2}{2z-1},$$ then the author makes the statement: "Then(by induction), $$R^n(z)=\frac{(2n+1)z-2n}{2nz-(2n-1)}.$$ Is there a simple way in Mathematica to produce $R^n(z)$?
David
  • 14,883
  • 4
  • 44
  • 117
6
votes
1 answer

How to deal with too much recursion

I have put together a very simple climate model based around four equations which define the state at time t based on time t-1, along with a initial state at time t = 0. The problem arises when I wish to evaluate, say, the temperature at year 2000…
Mr Alpha
  • 3,156
  • 2
  • 24
  • 33
6
votes
4 answers

Can the general term my recurrence equations be written with Floor or Mod?

I want to know the formula for the general term of the following recurrence system. I guess it can be written with Floor or Mod. How can I find it? RSolve[{a[n + 5] == a[n] + 6, a[1] == 1, a[2] == 3, a[3] == 2, a[4] == 1, a[5] == 1}, a[n],…
matrix42
  • 6,996
  • 2
  • 26
  • 62
6
votes
3 answers

Speed up iteration of pair of fully coupled recursion equations

I am trying to do several hundred iterations of these fully coupled recursion equations (but will use twenty iterations for the example). ClearAll["Global`*"] x1[t_] := x1[t] = (1 - m) x1[t - 1] + m x2[t - 1] x2[t_] := x2[t] = (1 - m) x2[t - 1] + m…
biologyUser
  • 135
  • 5
5
votes
2 answers

Building up a sequence dealing with a complex recursive scheme

Suppose I have multiple lists, the first one contains 2 elements, the next one 4 elements, the next one 8 elements, and so forth. Let these elements be labeled as Gij where i refers to the list it comes from and j refers to the position it…
Ibrahim
  • 169
  • 5
5
votes
1 answer

Specifying Range of RSolve

When I input RSolve[{0 == q[n + 1]^2 + 3 q[n + 1] + 2 q[n + 1] q[n] - 6 q[n] + q[n]^2, q[0] == 1}, q[n], n] I get two complicated-looking complex solutions: {{q[n] -> 1/4 I ((-2 + I) + 4 I^n - (2 + 5 I) I^(2 n) - (2 + 2 I) I^n Sqrt[5] + (2 + 2 I)…
Quinn Culver
  • 153
  • 5
5
votes
1 answer

How can I use a recursive function in a Module

I have a question about using a recursive function in a module. I made the following example to illustrate the question. The followwing code works: Clear[fib] fib[1] = 1; fib[2] = 1; fib[n_] := fib[n - 1] + fib[n - 2] f[a_, b_, n_] := Module[{x,…
nilo de roock
  • 9,657
  • 3
  • 35
  • 77
5
votes
1 answer

Recursion and safety of SetAttributes[Function, SequenceHold]

Compare the timings below N@Timing@Block[ {iii = 0}, Nest[ {#[[1]] + 1, Plus[#[[1]], 1, (#[[1]] + 1)*#[[2]]]} & , {0, 1}, 5000 ] ] -> {0.043021, {5000., 1.572304446472042*10^16326}} SetAttributes[Function,…
Jacob Akkerboom
  • 12,215
  • 45
  • 79
5
votes
7 answers

Computing only Prime Powers with NestList

Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute: {f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...} up to some specified prime power. Thanks so much!
user413587
  • 167
  • 5
5
votes
5 answers

How to solve the general formula by the recursive formula in MMA?

I want to solve this problem $T(n)=\sum_{i=1}^{n-1} 2T(i),T(1)=1$, I want to know how to solve this in MMA? My thinking: I have tried RSolve command, but the output is the same as the input. $$RSolve[a[n]==\sum_{i=1}^{n-1} 2*a[i],a,n]$$
maple
  • 193
  • 6
4
votes
3 answers

How to generate a recurrent sequence

How to generate this type of sequence? $$ f(n, x) = x f'(n-1, x) \hspace{2 mm}, f(0, x) = e^x$$ How do I evaluate it for numerical values for $x = 1$ or any number?
S L
  • 731
  • 7
  • 16
4
votes
1 answer

Why does Mathematica crash at a certain recursion depth?

If I enter Block[{$RecursionLimit = 70000}, x = x + 1] I get $RecursionLimit: Recursion depth of 70000 exceeded during evaluation of 1+x. But at $RecursionLimit = 80000, Mathematica crashes (i.e. goes unresponsive for a little while and then…
H.v.M.
  • 1,093
  • 5
  • 12
4
votes
2 answers

Recurrence Table with HoldForm

Consider: $$a_1=\sqrt{2}\qquad\text{and}\qquad a_{n+1}=\sqrt{2a_n}$$ I can do this: RecurrenceTable[{a[n + 1] == Sqrt[2 a[n]], a[1] == Sqrt[2]}, a, {n,5}] Now, is there a way I can HoldForm so that the RecurrenceTable command produces this…
David
  • 14,883
  • 4
  • 44
  • 117
1
2 3 4 5 6