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

Recursive Function in Mathematica

  1. Jun 17, 2009 #1
    Hello,

    I want to write a recursive function in Mathematica, how can I do that?

    Regards
     
  2. jcsd
  3. Jun 17, 2009 #2
  4. Jun 17, 2009 #3
    Last edited by a moderator: Apr 24, 2017
  5. Jun 17, 2009 #4
    You would write it, for ex. 40!

    See the Wolfram Mathematica Help for other recursive functions.
     
  6. Jun 17, 2009 #5
    I want to build a recursive function, and the foctorial is just an example, not that I want the factorial itself, but I want to know how to write a recursive function through a simple example, like the factorial function.
     
  7. Jun 17, 2009 #6

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    Very simple example:
    Code (Text):
    f[0] := 1
    f[n_] := n f[n-1]
     
  8. Jun 17, 2009 #7
    Thank you, but I don't want it to be as this simple, I need it with some IF statements and For loops ...etc. Anyway, I have the following MATLAB code
    Code (Text):
    function Output = ExpanFact(k,j,N_A)
    Output=0;
    if k==0
        Output=1;
    elseif j==1
        Output = 1/factorial(k);
    elseif k==1
        Output=j;
    elseif j > 0
        for l=(k-N_A+1):k
            if l >= 0 & l <= (j-1)*(N_A-1)
                Output=Output+(ExpanFact(l,j-1,N_A)/factorial(k-l));
            end
        end
    end
    and after a little effort I came with this equivalent Mathematica code:
    Code (Text):
    output = 0
    B[k_, j_, N_] := If[k == 0, output = 1,
      If[j == 1, output = 1/Factorial[k],
       If[k == 1, output = j,
        If[j > 0,
          For[l = (k - N + 1), l <= k, l++,
          If[l >= 0 && l <= ((j - 1)*(N - 1)),
           output = output + B[l, (j - 1), N]/Factorial[k - l],
           output]]]]]]
    but the later doesn't perform as the former. For example ExpanFact(3,3,3) = 4, where the later doesn't give any thing. Did I miss something?
     
  9. Jun 17, 2009 #8

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    Well, you're certainly not doing it in the Mathematica style like my factorial example. I would suggest something like
    Code (Text):
    B[0, j_, N_] := 1
    B[1, j_, N_] := j
    B[k_, 1, N_] := 1/k!
    B[k_, j_, N_] := Sum[B[l, j - 1, N]/(k - l)!, {Max[(k - N + 1), 0],  Min[k, (j - 1)*(N - 1)]}]
    Tell me how that works.
     
  10. Jun 18, 2009 #9
    This is an elegant code. But I faced with the following error message:
    Code (Text):
    In[1]:= B[0, j_, N_] := 1
    B[1, j_, N_] := j
    B[k_, 1, N_] := 1/k!
    B[k_, j_, N_] :=
     Sum[B[l, j - 1, N]/(k - l)!, {Max[(k - N + 1), 0],
       Min[k, (j - 1)*(N - 1)]}]
    B[3, 3, 3]


    [COLOR="Red"]During evaluation of In[1]:= Sum::write: Tag Max in Max[3-3+1,0] is \
    Protected. >>[/COLOR]
    Out[5]= (3 Min[2, l])/((3 - l)! l!)
    What is the problem?
     
  11. Jun 18, 2009 #10
    I wrote it in this way:

    Code (Text):
    fun[m_, n_, r_] := If [m <= r <= n, 1, 0]
    B[0, j_, N_] := 1;
    B[1, j_, N_] := j;
    B[k_, 1, N_] := 1/k!;
    B[k_, j_, N_] :=
     Sum[B[l, j - 1, N]/Factorial[k - l]*fun[0, ((j - 1)*(N - 1)), l], {l,
        l = k - N + 1, k}]
     
    but it gives wrong answers. For example, B[2,3,3]=4.5, but this code gives 3.5!! why?
     
  12. Jun 20, 2009 #11
    The equation mathematically that was written using Mathematica 6 is:

    attachment.php?attachmentid=19240&stc=1&d=1244335353.jpg

    Can anyone verify if I wrote it correctly, please? Because from my point of view, and little experience in Mathematica, I see nothing is wrong, yet the code gives wrong answers.

    Thanks in advance
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Recursive Function in Mathematica
  1. Mathematica functions (Replies: 1)

Loading...