Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [recursion]

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

0
votes
1answer
44 views

Recursive Function in Multiple Variables

I can define a recursive function in one variable, e.g. defining the factorial function $f(n)= n!$ using Mathematica. But how do you do this for a function which depends not only on $n$ but on $x$ and ...
0
votes
2answers
74 views

Problem with a simple recursive function

f[2 x_] := f[x] f[1] := 3 f[0] := 0 f[2 x_ + 1] := f[x] + f[x + 1] a[x_] := f[x]/f[x + 1] Will this work as an recursive function ? I think there's something ...
5
votes
1answer
2k views

Why is this recursive code so slow?

This code for the first five iterations the speed is okay, but after that the speed is very slow, I cannot understand what is wrong with this? Would you please help me fix it? ...
0
votes
1answer
69 views

How can I use Compile to implement recurrence?

I am trying to make the code for the following recurrence. Here wjoin[[i]] is used for calling values from a data set and a is a ...
1
vote
1answer
67 views

Solving for the recursion relation for the expansion coefficients of the asymptotic expansion of an ODE

I want to solve for the asymptotic solution of the following differential equation $$ \left(y^2+1\right) R''(y)+y\left(2-p \left(b_{0} \sqrt{y^2+1}\right)^{-p}\right) R'(y)-l (l+1) R(y)=0$$ as $y\...
2
votes
3answers
72 views

Indexed function recursion problem

I am trying to use indexed functions in a For loop; to illustrate, I give a trivial example. f[1, x_] := x^2 Now, I can define: ...
0
votes
1answer
38 views

Help with recurrence relation

I try to solve the following recursion for $n \in \mathbb{N}$. $r_i = r_{i-1} - \frac{1}{2} \cdot \sqrt{1 - \frac{4\pi^2\cdot r_{i-1}^2}{n^2} \cdot \cos^2 \left(\frac{\pi}{n}\right)}$ $r_0 = \...
3
votes
1answer
163 views

How to increase the accuracy of a plot

I have the following code below: ...
2
votes
1answer
69 views

My version of PowerMod breaks down around 10^308

I have been trying to write a function that duplicates PowerMod[a, b, n], computing a^b mod n...
0
votes
1answer
57 views

Update initial conditions recursively/sequentially within ParallelTable using NDSolve

I am modelling 3D motion of particles inside a force-field using second-order coupled PDEs, which I solve from time t=t0 to t=t1 with initial conditions x0,y0,z0. I have a total number of "particles" ...
2
votes
3answers
81 views

Defining a recursive function with additional parameters that can be used in a Manipulated ListPlot

I'm trying to code up some plots for Autocorrelation Functions in Time Series Analysis, which can often be defined recursively. The goal is then to have Manipulate sliders that allow you to ...
1
vote
1answer
82 views

A program for finding the summation of an analytical expression

Consider the Baker-Hausdorff formula for two operators $A$ and $B$: $$e^BA e^{-B} =A+[B,A]+\frac{1}{2!}[B,[B,A]]+\frac{1}{3!}[B,[B,[B,A]]]+....,$$ where $[A,B]=AB-BA$. In the case of my problem, $[B,...
1
vote
1answer
78 views

Why are there local variable conflicts in my recursive function definition?

Sorry to post a long piece of code. There are two places in here where I Print[] 'path2' when the count == 984, because for some reason, that list is getting ...
2
votes
1answer
67 views

a recursive function using memoization

I have looked at a lot of these cases, e.g. Can one identify the design patterns of Mathematica? and the included ones. My trimmed example, here establishing the 'initial conditions' (where the ...
5
votes
7answers
328 views

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]...} ...
0
votes
2answers
61 views

Implementing the factorial rule

I want to implement a command in Mathematica in order to simplify factorials in the following way $$(n+x)!=(n+x)(n+x-1)\cdots n!$$ I started to build this up slowly. I have managed to implement $...
1
vote
1answer
97 views

Speeding Up Recursive Evaluation

I have a recursive function for which I need the values for n=1 through about n=100,000,000 (or more). The function looks as follows, with output of the form ({x,y},a), and f, g, and h some functions. ...
1
vote
1answer
64 views

RSolve and a Piecewise Recurrence + Runtime

This should really be a comment to this answer but I don't have enough rep. I'd like to know how to do the same thing (efficiently - see below) but with $\mod 3$ conditions, i.e. define a[k] to be the ...
-1
votes
1answer
57 views

Is there a way to get values from a set of functions with circular dependencies? [closed]

I am using the following code to find iteratively the functions $\Sigma(r)$, $h(r)$ and $T(r)$ ...
1
vote
2answers
102 views

Creating an Excel-Like Recursive Function

I'm trying to create a certain "excel-like" recursive function, which runs through a pre-defined list and performs an operation dependent upon the current position (similar to the "drag down" type ...
1
vote
1answer
39 views

Mathematica code for computing the $p$-adic expansion of rational numbers

Does anyone know any Mathematica code for computing the $p$-adic expansion of rational numbers? I.e. given a rational number $a/b,~a,b\in \mathbb{Z}$ and a prime number $p$, then compute the $p$-adic ...
0
votes
1answer
72 views

Finding a sequence with RecurrenceTable

I would appreciate if somebody could help me with the following problem: Q: How can the following recursion equations be solved using RecurrenceTable? $\...
1
vote
2answers
62 views

How to define a recursive sequence

I would appreciate if somebody could help me with the following problem: Q: How to define this in Mathematica. $$a_{n+1}=\begin{cases} a_{n} & ,a_{n} \ge n\\ 2n-a_{n}& ,a_n<n \end{cases}...
1
vote
0answers
38 views

Recurrence that contains Sum of previous elements

I would like to create the recurrence that contains the summation of all previous elements, but it seems it does not work. That I'm doing wrong? ...
5
votes
1answer
119 views

Recycling solutions of multidimensional NDSolve

Dear wolfram community, I hope my problem is clear and easy to solve. I have already solved the following heat equation over a domain: ...
1
vote
1answer
37 views

RSolve for three arguments

I would like to find the general expression for a recursive equation: ...
0
votes
1answer
48 views

Plotting intermediate values of an argument in a recursive function

I've read through the documentation on tuning and debugging but feel like I am missing some important concepts. Many of the examples given presume the programmer is explicitly generating ranges of ...
0
votes
1answer
30 views

Exponent Recursion implementation

I want to implement this recursive formula: "j" is imaginary symbol (i.e. j = I = Sqrt[-1]); I have written my code as: ...
2
votes
2answers
53 views

Expand a recursion relation

Hermite Polynoms have the following recursion relation: $$ H_{n+1}(x) = 2x H_n(x) - 2n H_{n-1}(x), $$ which can be rearranged to be $$ x H_n(x) = \frac{1}{2} H_{n+1}(x) - n H_{n-1}(x).$$ Is it ...
0
votes
1answer
49 views

Recurrence Relation with two variables and boundary conditions

I try to express the following recurrence relation in Mathematica: I am very sorry but for any reason I am not allowed to embed math code here... \begin{align*} x_{1,t} &= \frac{1}{2} x_{1,t-...
4
votes
2answers
117 views

RSolve not reducing for a certain recurrence relation

I'm trying to use RSolve as follows to solve a recurrence relation: ...
0
votes
0answers
31 views

Recursive function call in Fold: $RecursionLimit issue

I'm trying to generate a list of all local complements of a graph $G$. A local graph complement $G\star v$ for $v\in V$ is defined as taking the neighbourhood of the vertex $v$, and flipping the ...
1
vote
1answer
32 views

How to perform a set of commands specific amout of time?

I have 3 lines of code that does the Collatz Conjecture in mathematica. the code is the following: ...
1
vote
0answers
51 views

ConditionalExpression still resulting from Assuming a Module's local variables

I have a memoizing, recursive function whose MWE looks like this (the two fake inputs/ICs are from 'K' and 'Bfun2', the latter just a quick way to check the results coming out of the Residue.: ...
1
vote
1answer
32 views

Getting error RSolve::dsfun [closed]

I an trying to solve a recursive formula and find the explicit representation of $a$. $a_0 = 1$ $a_{n+1} = 3 \sum\limits_{i=0}^{n} a_i$ When I evaluate ...
6
votes
2answers
490 views
0
votes
0answers
33 views

Recurrence Relation based on Vectors

I am new to Mathematica. I try to solve a recurrence relation based on vectors. So a[n] is a vector for all n (as well as ...
1
vote
0answers
86 views

Solving a recurrence relation with variable coefficients

I am trying to solve this recurrence inequality for $ a_n $, hopefully in terms of $ a_0,a_1,c $ and $ x $, $$(1-x)a_{n+1} \le xa_n + (1+x)a_{n-1} + c\quad\forall\ n\ge1$$ What I tried: RSolve. But ...
0
votes
0answers
33 views

Avoid Recursion in lag and difference operations

I have a panel data situation and I'm interested in taking lags and differences and using Mathematica to simplify the algebra of the result. Currently I'm working with a model like this: ...
7
votes
2answers
712 views

Simple Fractal square

I am working on a math question about infinite series, and one of the question images is below. Each new white square has an area that is 1/4 of the previous square. Always looking to learn elegant ...
1
vote
3answers
99 views

Unable to implement bisection method using recursion [duplicate]

I have tried to implement a bisection method for finding roots using SetAttributes[BisectionItt, HoldAll] and then ...
2
votes
1answer
90 views

Unwanted Hold from recursive function

I am literally copying an example from a tutorial in Mathematica documentation of a recursive function, but when I evaluate I get a Hold. I don't understand why I ...
1
vote
1answer
79 views

How can I implement a repeating operation?

I have the list: ...
1
vote
1answer
131 views

How do I define a function that uses a recurrence relation? [duplicate]

I know how to define functions other than the ones that use a recurrence relation to calculate the $n$-th term of a sequence. How would such a function be constructed?
1
vote
1answer
95 views

Definition of a recursive formula

For a given $n$, I would like to calculate a function $f_n$ recursively. Here is what I have: ...
0
votes
3answers
83 views

Understanding recursion and role of replacement rule for recursion in Mathematica

From the book "Essentials of Programming in Mathematica" (Fibonacci numbers): If we think of this sequence as a function, we would just change this to a functional definition. F(1) = 1 F(2) =...
4
votes
1answer
77 views

TracePrint does not work as in my book

I am using Mathematica 11.3 Here is an my execution of code from book "Essentials of Programming in Mathematica" (Fibonacci numbers): ...
9
votes
1answer
472 views

Tree-like data sets with efficient storage and retrieval

Most probably this is going to be a duplicate, sorry. I found too many questions/answers here about tree-like structures, and still could not locate what I want. It is a followup of my previous ...
3
votes
3answers
151 views

Generalisation of a generating function

I have asked a question here. I want to reproduce the coefficients of a generating function of the form: $$(1 + x)^2 (1 + x + x^2 + x^3+\cdots+x^n)^{n-1}$$ It is important that $x_0$ and $x_n$ to be ...