Recursive Function in Mathematica

Click For Summary

Discussion Overview

The discussion revolves around writing recursive functions in Mathematica, with a focus on syntax and structure. Participants explore examples, particularly the factorial function, and attempt to translate MATLAB code into Mathematica while addressing issues related to function performance and errors.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks guidance on writing a recursive function in Mathematica, specifically asking for syntax.
  • Another participant provides a link to examples of recursive functions but notes that they are complex and not what the original poster is looking for.
  • Several participants discuss the factorial function as a simple example of recursion, with one emphasizing the need for a more complex structure involving IF statements and loops.
  • A participant shares their MATLAB code for a recursive function and attempts to translate it into Mathematica, expressing concerns about its performance compared to the original MATLAB version.
  • Another participant suggests a more idiomatic Mathematica approach to writing the recursive function, proposing a different structure using the Sum function.
  • Errors encountered in the proposed Mathematica code are discussed, with participants questioning the syntax and logic used in the translations.
  • One participant expresses confusion over the output of their code, indicating discrepancies between expected and actual results.
  • A later post requests verification of the correctness of a mathematical expression written in Mathematica, highlighting uncertainty about the implementation.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best way to implement recursive functions in Mathematica. There are multiple competing views on syntax and structure, and several unresolved issues regarding errors and performance of the code.

Contextual Notes

Participants express uncertainty about specific syntax and logic in Mathematica, particularly regarding the use of the Sum function and handling of variable scopes. There are indications of missing assumptions and potential misunderstandings of Mathematica's function definitions.

EngWiPy
Messages
1,361
Reaction score
61
Hello,

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

Regards
 
Physics news on Phys.org
You would write it, for ex. 40!

See the Wolfram Mathematica Help for other recursive functions.
 
Дьявол said:
You would write it, for ex. 40!

See the Wolfram Mathematica Help for other recursive functions.

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.
 
saeddawoud said:
I want to write a recursive function in Mathematica, how can I do that?

Very simple example:
Code:
f[0] := 1
f[n_] := n f[n-1]
 
CRGreathouse said:
Very simple example:
Code:
f[0] := 1
f[n_] := n f[n-1]

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:
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:
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?
 
saeddawoud said:
Did I miss something?

Well, you're certainly not doing it in the Mathematica style like my factorial example. I would suggest something like
Code:
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.
 
CRGreathouse said:
Well, you're certainly not doing it in the Mathematica style like my factorial example. I would suggest something like
Code:
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.

This is an elegant code. But I faced with the following error message:
Code:
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]


During evaluation of In[1]:= Sum::write: Tag Max in Max[3-3+1,0] is \
Protected. >>
Out[5]= (3 Min[2, l])/((3 - l)! l!)

What is the problem?
 
  • #10
I wrote it in this way:

Code:
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?
 
  • #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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K