Mathematica: Recursive expression

In summary, the conversation is about finding a smart way to write a given fraction and constructing a function to recursively find the value of the fraction for a given n. The suggested solutions involve using the value of n in the fraction and propagating it up to a_1.
  • #1
Niles
1,866
0
Hi

I am trying to find a smart way to write the following fraction,

$$
F = \frac{a_1}{1+\frac{a_2}{1+\frac{a_3}{1+a_4}}}
$$

Here we can just take [itex]a_n= n[/itex] for simplicity. My fraction is in principle infinite, but I am trying to construct a function which can find [itex]F[/itex] for a given [itex]n[/itex] recursively. I haven't had much success. So far I have

f[n_] := n;
f[n - 1]/(1 + f[n])

which is the last term for a given [itex]n[/itex]. For do I propagate all the way up to [itex]a_1[/itex] then?
 
Physics news on Phys.org
  • #3
Code:
f[1] = Subscript[a, 1];

f[n_] := (f[n - 1]) /.Subscript[a, n - 1] :> Subscript[a, n - 1]/(1 + Subscript[a, n])
Does that work?
 

Related to Mathematica: Recursive expression

1. What is a recursive expression in Mathematica?

A recursive expression in Mathematica is an expression that refers to itself in its own definition. It allows for the creation of more complex functions by building upon simpler versions of the same function.

2. How do you write a recursive expression in Mathematica?

A recursive expression in Mathematica is typically written using the 'Function' or 'Module' command. For example, a recursive function that calculates the factorial of a number can be written as Factorial[n_] := n * Factorial[n-1]. This function will call itself with a smaller value of n until it reaches the base case of n=1.

3. What is the difference between a recursive and iterative expression in Mathematica?

A recursive expression calls itself in its own definition, while an iterative expression uses a loop or other iterative construct to repeat a calculation. Recursive expressions are typically more concise and elegant, but they may also be less efficient for certain tasks compared to an iterative approach.

4. Can all problems be solved using recursive expressions in Mathematica?

No, not all problems can be solved using recursive expressions in Mathematica. Some problems may have more efficient solutions using other programming techniques, while others may not have a recursive solution at all. It is important to consider the complexity and efficiency of a problem before deciding on a programming approach.

5. How can I avoid infinite recursion in Mathematica?

To avoid infinite recursion in Mathematica, you can set a base case or stopping condition in your recursive function. This ensures that the function will eventually reach a point where it no longer calls itself and the recursion will stop. It is also important to carefully consider the parameters and input values used in the recursive function to avoid unintended infinite loops.

Similar threads

  • Calculus and Beyond Homework Help
Replies
11
Views
179
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
1
Views
821
  • General Math
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
951
Replies
8
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
682
Replies
4
Views
1K
Back
Top