MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Introduction to MuPAD

Contents

What is MuPAD?

We can think of Matlab in a sense as being two programs in one, the first called Matlab and the second called MuPAD.

  1. Matlab is numerical.
  2. MuPAD is symbolic.

When we're in Matlab and execute a numerical command like fzero, Matlab simply executes it. However when we're in Matlab and execute a symbolic command like diff what happens is that Matlab calls upon MuPAD and MuPAD converts the command to its own language, does the job in the background and returns the result to Matlab. This process is transparent to us, however, so we don't usually realize this is occurring.

However at this point we'd like to discuss MuPAD as an independent program. As an independent program we can access MuPAD directly in two ways.

  1. Call a MuPAD command from the Matlab command line.
  2. Directly open a MuPAD notebook and execute MuPAD commands in it.

Before we begin, to really see what we mean by "two programs", try the following two commands in Matlab.

1/3+1/5
ans =

    0.5333

Here Matlab does the job and since Matlab is numerical we get an approximate answer.

evalin(symengine,'1/3+1/5')
 
ans =
 
8/15
 

Here Matlab calls upon its symbolic engine, MuPAD, and tells it to do the calculation instead. Since MuPAD is symbolic we get an exact answer because MuPAD works with the fractions symbolically rather than dealing with their approximations.

The Matlab command evalin makes the call to a symbolic engine. The bit that says symengine is a reserved variable (you can't change it) which indicates to Matlab that it ought to use MuPAD (think symengine=MuPAD). The command to execute is contained in the string.

Opening a MuPAD Notebook

To open a MuPAD notebook simply issue the command

nb = mupad

The nb is just a handle to the notebook - think of it as a "variable name" for the notebook. It's not a file name so really we can call it anything you like.

The MuPAD notebook will open in a new window like this: PUT WINDOW SNAPSHOT HERE

The cursor will be blinking in front of a vertical line or bracket.

 

 

 

Let's try our fraction example from before:

 

1/3 + 1/5

math

 

 

 

Now we get the exact (symbolic, non-approximate) answer without any special instructions because our command is going directly to MuPAD. Here's another command:

 

diff(sin(x)+x^2,x)

math

 

 

 

We see the exact output we expect. However if we try a standard Matlab command which is not symbolic MuPAD doesn't know what to do so it throws the command back at us:

 

fzero(inline(x^2+x-1),0)

math

 

 

 

You'll notice that when we press enter MuPAD evaluates the command and also creates a new input area. At any point we can use the mouse or cursor to move back through the input areas to change and then reevaluate a command by pressing enter again.

Matlab/MuPAD Differences

There are significant differences between Matlab and MuPAD that we should be aware of. We won't describe all the differences here but just the ones that can confuse n00bs.

Variable Assignment and Consideration

In MuPAD we assign variables using x:= rather than x= as in Matlab. Once a variable is assigned it is for all practical purposes equal to that number. This can cause some confusion because if you have a variable assigned as a number you cannot then use it as a variable in for example a solve command. For example consider the following MuPAD interaction:

 

x:=2

math

solve(x^2+7*x+3=0,x)

Error: Illegal variable(s) to solve for: 2 [solve]

 

 

This makes perfect sense if we appreciate that MuPAD now believes that x actually the same as 2 so we're asking MuPAD to solve for the number 2 and it doesn't like this much, obviously. If we really want to use x again we have to delete it first.

 

x:=2

math

delete x

solve(x^2+7*x+3=0,x)

math

 

 

 

Now we get what we want. Of course we could have used a different variable other than x if we had chosen to.

Function Definition

Functions can be defined only one way in MuPAD. For MuPAD a function is merely a symbolic mapping of the independent variable to the result. Here is s simple example.

 

f:=x->x^2+2*x-5

math

 

f(-1)

math

 

f(t+2)

math

 

 

 

Integration

Indefinite integration in MuPAD is just like in Matlab but definite integration uses slightly different notation. Here is an example of each. Note MuPAD's use of PI.

 

int(x^2+sin(x),x)

math

 

int(x^2+sin(x),x=0..PI/4)

math

 

 

 

Self-Test

  1. Without opening a MuPAD notebook use a MuPAD command to find the area under f(x)=1/x from x=1 to x=7.
  2. Without opening a MuPAD notebook use a MuPAD command to solve the equation x^4-2*x^2+5=0.
  3. Open a MuPAD notebook and do both of the previous exercises directly.
  4. Open a MuPAD notebook, define the function f(x)=x^2-tan(x) and plug some values in.

Answers to Self-Test

Next Topic:   Intermediate MuPAD