Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: Recursive expression

  1. Jun 13, 2013 #1
    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

    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?
     
  2. jcsd
  3. Jun 14, 2013 #2
  4. Jun 18, 2013 #3

    Hepth

    User Avatar
    Gold Member

    Code (Text):

    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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook