MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Intermediate MuPAD

Contents

Solving Equations

MuPAD has one advantage over Matlab when it comes to solving equations. We can show it to you easily. Here is an equation solved in Matlab:

solve('sin(x)=1/2',x)
 
ans =
 
     pi/6
 (5*pi)/6
 

Versus, in a MuPAD notebook:

 

solve(sin(x)=1/2,x)

math

 

 

 

The clear advantage is that MuPAD gives you all of the infinitely many solutions. Of course a competent Calculus II student should know how to get all the answers from the basic two but it's nice to have them immediately available.

Substitution/Evaluation

If we have a symbolic expression which we wish to evaluate for a specific value of the variable we can do so easily in Matlab. For example here is the integral of x*exp(x) evaluated at x=-1:

 

int(x*exp(x),x)|x=-1

math

 

 

 

Passing Variables

Suppose we have a variable in either Matlab or MuPAD and wish to pass it to the other. How can we do this? For example suppose we've opened a MuPAD notebook by

nb = mupad

and done the calculation

 

z:=int(x^3-x+2,x=0..0.5)

math

 

 

 

Now we want this answer back in Matlab. This is easy! We can get it and assign it in Matlab as follows. Note here that the nb is the handle on the notebook. If we have multiple notebooks open at once this tells us which notebook to grab the variable z from.

x=getVar(nb,'z')

x =

0.890625

This will also work for symbolically defined expressions. Consider the following calculation performed in MuPAD

 

z:=int(x^2-exp(2*x),x)

math

 

 

 

We can get this back into Matlab with

getVar(nb,'z')

ans =

x^3/3 - exp(2*x)/2

The other way around is not so obvious. Since MuPAD deals with symbolic objects and a number is not a symbolic object we cannot simply pass a number back from Matlab to MuPAD. This generally is not a problem since MuPAD is usually used for small calculations on the side and the results passed to Matlab. We can however pass symbolically defined expressions from Matlab to MuPAD using setVar. For example suppose we do the following in Matlab.

syms x;
y=x^2+sin(x);
setVar(nb,y);

Now in our MuPAD notebook with handle nb we have the symbolic y available and equal to x^2+sin(x). If we go into the notebook and try the following we see.

y

math

 

 

 

We should note here that it is impossible in MuPAD to issue commands to obtain Matlab results. Matlab must do the work!

Plotting Functions

We'll close our brief journey to show that MuPAD can plot functions very easily. This may be useful if we'd like to play with a function in our MuPAD notebook before doing a final version in Matlab for publishing. MuPAD can plot as many functions as we'd like in one fell swoop. Not only that but it color codes them and identifies them. Here are two functions together.

plotfunc2d(x^2+2*x,2*x+4,x=-3..3)

MuPAD graphics

 

 

 

The notation is fairly obvious; MuPAD plots all the functions listed and separated by commas on the interval given.

Accessing Previous Results

If you do a calculation in MuPAD and then wish to access the answer in a following calculation you can do so with the % which is replaced automatically by the result of the previous calculation. For example

 

int(x^2-exp(2*x),x=0..2)

math

 

2*%

math

 

 

 

Even older results can be obtained using %2, %3 and so on.ry

Saving Your MuPAD Notebook

Suppose you do a MuPAD notebook and wish to save it. You can do this just using the file->save menu item. However next time you load Matlab you'll need to reassign this notebook to the filehandle if you need to pass variables back and forth. This is easily done. If a notebook has name mynotebook.mn then you can reload it in Matlab with:

nb=mupad('mynotebook.mn')

Exporting Your MuPAD Notebook

To produce a pretty output for our MuPAD file we can file->export it to html. This produces a very simple html file with images for all the code and calculations. Keep this in mind if you ever need to submit a MuPAD notebook as part of an assignment! All the MuPAD code you see in this document is essentially directly exported from MuPAD.

Self-Test

  1. Issue a call to MuPAD from within Matlab to solve sin(2*x)-cos(x)=0.
  2. Create a MuPAD notebook. In this notebook assign the function f(x) to be exp(x^2+x), assign k to be the value of the derivative at x=1 and then obtain the result in Matlab also assigned to the variable k.
  3. Create a MuPAD notebook. In this notebook find the indefinite integral of x*sin(x)*cos(x) assign this in MuPAD to y and return the result to Matlab, defining it as a function handle f.
  4. Create a MuPAD notebook. From Matlab pass the symbolic expression y=1/x+exp(1-x) to MuPAD and call it y in that notebook. In MuPAD find the definite integral from 1 to 3 and assign this to the value j. Lastly go back to Matlab and retrieve this value, calling it t.

Answers to Self-Test