LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

recursion in MathScript function

tl;dr

How do I make this go?

 

So here is the code in actual MatLab:

N = 10;

F  = myfib(N);

disp([N,F])

function [out] = myfib(n)
     if n< 2
          out = 1;
     else 
          out = myfib(n-2) + myfib(n-1);
     end
end

It yields this as output:

>> myfib_in_MatLab
    10    89

>> 

I want to do something similar in MathScript.

 

Here is my Snippet:

MathScript nodeMathScript node

You can notice that the text is grayed, it isn't compiling, and there is no output.  It can't detect "F" as an output variable.

 

 

 

 

0 Kudos
Message 1 of 4
(2,387 Views)

So don't use MathScript.  Do it in LabVIEW.  Note that Recursion, while really neat and lots of fun, does require that you know what you are doing and understand Reentrancy ...  Note that the Recursive Form you've given is the usual form for the Fibonacci series, but it is very inefficient, particularly for larger numbers (the number of calls grows much faster than N).

 

I just coded it (in LabVIEW) and got the following values for n = 1 .. 6:  1, 1, 2, 3, 5, 8.

 

Bob Schor

 

 

0 Kudos
Message 2 of 4
(2,346 Views)

Bob,

 

This is a toy that demonstrates recursion.  I don't have the choice of whether or not to use MathScript.  If I need to learn how reentrancy works, I should do that instead.

 

Can you provide references or links?

 

-EngrStudent

0 Kudos
Message 3 of 4
(2,322 Views)

Well, I know LabVIEW.  I also know Matlab (or used to -- it's been about a decade since I did anything serious in Matlab), but have never thought to bother with MathScript (the Worst of Both Worlds).  I have no idea why MathScript doesn't "do" recursion.  I do know that LabVIEW does, but since you "have no choice of whether or not to use MathScript", I can't be of any help to you.  Good luck.

 

BS

0 Kudos
Message 4 of 4
(2,313 Views)