image
Physics Forums Logo
image
image
* Register * Upgrade Blogs Library Staff Rules Mark Forums Read
image
image   image
image

image Recursive Function in Mathematica Share It Thread Tools Search this Thread image
Old Jun17-09, 07:36 AM                  #1
S_David

S_David is Offline:
Posts: 244
Recursive Function in Mathematica

Hello,

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

Regards
  Reply With Quote
Old Jun17-09, 10:34 AM                  #2
Дьявол

Дьявол is Offline:
Posts: 366
Blog Entries: 1
Re: Recursive Function in Mathematica

Try this link.
  Reply With Quote
Old Jun17-09, 10:40 AM                  #3
S_David

S_David is Offline:
Posts: 244
Re: Recursive Function in Mathematica

Originally Posted by Дьявол View Post
Try this link.
These are complex examples, I just need the syntax of writting a function. For example, how can we write a factorial using recursive function in Mathematica?
  Reply With Quote
Old Jun17-09, 10:53 AM                  #4
Дьявол

Дьявол is Offline:
Posts: 366
Blog Entries: 1
Re: Recursive Function in Mathematica

You would write it, for ex. 40!

See the Wolfram Mathematica Help for other recursive functions.
  Reply With Quote
Old Jun17-09, 01:09 PM                  #5
S_David

S_David is Offline:
Posts: 244
Re: Recursive Function in Mathematica

Originally Posted by Дьявол View Post
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.
  Reply With Quote
Old Jun17-09, 04:43 PM                  #6
CRGreathouse

CRGreathouse is Offline:
Posts: 2,939
Recognitions:
Homework Helper Homework Helper
Science Advisor Science Advisor
Re: Recursive Function in Mathematica

Originally Posted by saeddawoud View Post
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]
  Reply With Quote
Old Jun17-09, 07:46 PM                  #7
S_David

S_David is Offline:
Posts: 244
Re: Recursive Function in Mathematica

Originally Posted by CRGreathouse View Post
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?
  Reply With Quote
Old Jun17-09, 11:40 PM                  #8
CRGreathouse

CRGreathouse is Offline:
Posts: 2,939
Recognitions:
Homework Helper Homework Helper
Science Advisor Science Advisor
Re: Recursive Function in Mathematica

Originally Posted by saeddawoud View Post
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.
  Reply With Quote
Old Jun18-09, 06:35 AM                  #9
S_David

S_David is Offline:
Posts: 244
Re: Recursive Function in Mathematica

Originally Posted by CRGreathouse View Post
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?
  Reply With Quote
Old Jun18-09, 02:09 PM                  #10
S_David

S_David is Offline:
Posts: 244
Re: Recursive Function in Mathematica

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?
  Reply With Quote
Old Jun20-09, 11:14 AM                  #11
S_David

S_David is Offline:
Posts: 244
Re: Recursive Function in Mathematica

The equation mathematically that was written using Mathematica 6 is:


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
  Reply With Quote
image image
Reply
Thread Tools


Similar Threads for: Recursive Function in Mathematica
Thread Thread Starter Forum Replies Last Post
even summation with recursive function in c++ NewDesigner Programming & Comp Sci 1 May7-08 11:25 AM
Recursive probability function CRGreathouse Set Theory, Logic, Probability, Statistics 1 May24-07 10:48 AM
Simplifying a recursive function randommacuser Calculus & Beyond 10 Apr10-07 09:21 AM
what the hell is going on with this recursive function gravenewworld Set Theory, Logic, Probability, Statistics 17 Feb15-05 12:50 AM
Recursive Function discoverer02 Computing & Technology 4 May4-04 08:45 PM

Powered by vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd. © 2009 Physics Forums
Sciam | physorgPhysorg.com Science News Partner
image
image   image