Document 451

User-defined MuPAD functions in Scientific Notebook

Version: 4.x & 5.x - Scientific WorkPlace & Scientific Notebook

Teachers sometimes ask "Can I write my own procedures in Scientific Notebook?" Although the answer to this question is "No," there are ways to accomplish much of the same intent. And, even if you aren't inclined to write your own procedures, you can easily make use of that type of work done by others. The purpose of this brief article is to show you how to do that.

First, though, here are a few examples of the usage and output of the user-defined procedures discussed here:

  • create a Math: 4×4 base-3 addition table:
    Math: base-3 addition table
  • create a Math: 5×5 base-5 multiplication table:
    Math: base-5 multiplication table
  • determine the base-7 integer representation of the base-10 integer Math: 3579:
    Math: base-7 integer representation

Of course, those are just very few examples of what can be done by calling user-defined MuPAD procedures from within a Scientific Notebook document. At the end of this article, there is a link that you can use to download the source file that contains those three MuPAD procedures. If you are a Scientific Notebook (or Scientific WorkPlace) user, you might want to read through the article once and then download the source file and follow the instructions to implement the functions in one of your own documents.

Now, let's use the last example in the list above (determine the base-7 integer representation of the base-10 integer Math: 3579) and go through the steps necessary to accomplish that in a Scientific Notebook document.

Notes:

  • For the purposes of this article, it is assumed that you are using Scientific Notebook version 4.0 or 4.1, but statements in this article that refer to Scientific Notebook also apply to Scientific WorkPlace version 4.0 or 4.1.
  • At this time, the MuPAD procedures referred to in this article are best developed in MuPAD Pro 2.0, because that is the version of the MuPAD computational engine that is integrated into Scientific Notebook versions 4.0 and 4.1. Procedures that are developed in MuPAD Pro 2.5 can be used as long as any built-in functions utilized are also available in the MuPAD Pro 2.0 kernel and standard libraries


Before we start, make sure you're using the MuPAD engine:

  1. From the Tools menu, choose Computation Setup and select the Engine Selection tab.
  2. In the Current Engine box, select MuPAD and choose OK.

The specific tasks in this type of process are to

  1. Develop a specific MuPAD procedure, or obtain one from someone else.
  2. Create a new Math Name for use in calling that procedure.
  3. Map the MuPAD procedure to the newly-created Math Name.
  4. Use the newly-mapped procedure in a Scientific Notebook document

    .

Develop a specific MuPAD procedure or obtain one from someone else

While we won't go into very many details here about creating your own MuPAD procedure, a few comments might be helpful. The MuPAD procedure must be in a MuPAD source file, which is just a text file with a .mu extension. Because that file is a text file, you can create such a procedure outside of the MuPAD Pro 2.0 environment. In that case, however, you would not easily be able to test and debug the procedure during development.

For the purposes of this article, we will describe the use of a procedure called changeFromBase10 that is stored in a source file called userFcns.mu. As you'll see below, that procedure accepts two input arguments, a base-10 integer and a new base designation (between 2 and 10). It then returns the equivalent integer representation in the new base.

Notes:

Create a new Math name for use in calling that procedure

In many similar instances, as here, we would like to create the new function name in Scientific Notebook to be the same as the one used to develop the procedure in MuPAD Pro 2.0. For that purpose in Scientific Notebook, you will need to use what is referred to as a Math Name.

The steps here are as follows:

  1. Choose Insert/Math Name and type changeFromBase10 in the Name field of the dialog box that appears.
  2. Make sure that Function or Variable and Add Automatic Substitution are selected, and click OK.

The new Math Name

Math: changeFromBase10

is inserted into your document wherever the cursor is. In addition, whenever you are in math mode and type the Math Name that was just created, it will be automatically recognized by Scientific Notebook. That is, it will change from Math: changeFromBase1 to Math: changeFromBase10 when you type the last character of the function name.

A brief word of caution here: when creating a new Math Name, don't try to use a sequence of characters that contains an already defined Math Name. For example, you couldn't properly create and use a Math Name called mysinfunc because it contains the already defined special Math Name Math: sin . Before you could finish typing that name in math mode, the sin would be automatically recognized and the result would look like Math: my sin func .

Map the MuPAD procedure to the new Math Name

Recall from above that this step, as described here, assumes that there is a procedure called changeFromBase10 that is stored in a file called userFcns.mu in a MuPAD\UserFcns subfolder of the local Scientific Notebook installation. Don't be too worried if that comment sounds confusing at this point. It should become a little bit clearer as you read through the specific steps below.

The steps here are as follows:

  1. Choose Compute/Definitions/Define MuPAD Name....
  2. Type changeFromBase10(n,b) in the MuPAD Name field of the dialog box that appears.
  3. Type changeFromBase10(n,b) in the Scientific Notebook Name field.
    You'll see that math mode is the default mode of that field.
  4. Select in MuPAD format file (.mu file) and click the Browse button.
  5. Browse to the folder that contains the MuPAD source file.
    In this example, we've stored the file called userFcns.mu in a subfolder that we created called UserFcns. So, the full path to the appropriate file in that case would be:
    Path name
  6. Select the userFcns.mu file, click the Open button, then click the OK button.

Use the newly mapped procedure in a Scientific Notebook document

In order to use the newly mapped procedure, you merely need to type the argument call in math mode and then select Compute/Evaluate. An example call is shown below:

Sample exercise

Students could, for example, be asked to demonstrate their understanding of the base-n representation of a number by using the above procedure, independently producing the base-n expansion, and using Scientific Notebook to compare the two. For instance:

Sample exercise

Sample exercise (using Compute/Evaluate)

or

Sample exercise (using Compute/Evaluate)

or

Sample exercise is true (using Compute/Check Equality)

Tip: Create a "shell" from a document

After you have made one or more definitions (mappings) as described above, you can create a shell document that will preserve those definitions for use in the development of other documents. For more information on shell documents, select Search... from the Help menu, type shells into field 1, select Shell documents in field 3, then click the OK button.

Some comments about the MuPAD procedures

There are three procedures defined in the userFcns.mu file referred to throughout this article:

  1. changeFromBase10 - accepts a base-10 integer input argument and a new-base designation (between 2 and 10) and displays the new-base integer representation as output; for example:
    Sample output
  2. makeBaseAddTable - accepts a dimension argument Math: n and a base designation Math: b (between 2 and 10) and displays the Math: n×n base- Math: b addition table; for example:
    Math: base-b addition table
  3. makeBaseMultTable - accepts a dimension argument Math: n and a base designation Math: b (between 2 and 10) and displays the Math: n×n base- Math: b multiplication table; for example:
    Math: base-b multiplication table


Click here to download a copy of the userFcns.mu file.

If you have an idea for a procedure and you'd like some assistance in implementing it, or if you have a working MuPAD procedure that you'd like to offer for use by others, please email us.

If you have SWP, SW, or SNB, you can open the .rap version of this document that is available on our website.

Back to top

Last revised 03/13/06

This document was created with Scientific WorkPlace.