Doing Math by SWP

atharkharal@gmail.com's picture

 How we can implement recursive formulae in SWP's computer algebra facility?

John MacKendrick's picture

Hi Try instead

Hi

Try instead writing

fibonacci := proc(n)
local x;
begin
   if n=1 then x := 0
   elif n=2 then x := 1
   else
      x := fibonacci(n-2)+fibonacci(n-1):
   end_if:
return(x);
end_proc:
  

as the procedure.

Thanks to John M. for the

Thanks to John M. for the revision. This works well.

Dr. Robert Stanton's picture

I have been able to use

I have been able to use piecewise function notation to do recursive functions. (See help files if you don't know how to do this.) Let f(x) =

1 if x = 1

1 if x = 2

f(x-1)+f(x-2) if x > 2

Define function, and evaluate at any positive integer.

Note: The subscript notation for a function argument won't work here.

 

  

John MacKendrick's picture

Re: Doing Math by SWP

The Scientific WorkPlace interface does not directly support recursive functions, but you can write recursive functions in MuPAD code and access the function using the Define MuPAD Name function.

An example of a simple function which finds the nth number in the fibonacci series can be written as follows in an ASCII editor (such as Notepad):

fibonacci := proc(n)
local x;
begin
   if n=1 then x := 0
   else
      if n=2 then x := 1:
   end_if:
   else
      x := fibonacci(n-2)+fibonnacci(n-1):
   end_if:
return(x);
end proc:
 

You can then save this function in a file named, say, fib.mu and access it as follows:

 

1. Choose Compute, Definitions, Define MuPAD name.

2. Enter

fibonacci(n)

as the MuPAD name.

3. Enter

f(n)

as the Scientific WorkPlace name. You can name this function anything you want. It is only important that the argument list of the MuPAD name and the Scientific WorkPlace name match.

4. Under "The MuPAD Name is a procedure", select "In MuPAD format file (.mu file)".

5. Enter the path and name of the fib.mu file created with the ASCII editor.

6. Click OK.

At this point, you should be able to evaluate f(5) and get 3.

A good book to use to learn the MuPAD programming language is "MuPAD Pro Computing Essentials, Second Edition". See http://www.mackichan.com/products/books/books.html to learn more about this book.

I tried to follow this

I tried to follow this example exactly as presented, and when I clicked the "show definitions" button, it returned f(n) <-> C:/swp55/fib.mu [f(n) in math mode] However, when I type [math] f(5) followed by Compute/Evaluate, it returns f(5). I'm running the MuPad engine in SWP b.2960 XP. Any suggestions?

tomprice's picture

The examples deal with the

The examples deal with the Fibonacci numbers. I use Binet's formula to define this sequence. Many sequences of this type can be written in a similar way. Use simplify instead of evaluate to calculate a member of the sequence. The calculations are very fast.