Main Content

Call Built-In MuPAD Functions from MATLAB

Note

MuPAD® notebook has been removed. Use MATLAB® Live Editor instead.

To convert a MuPAD notebook file to a MATLAB live script file, see convertMuPADNotebook. MATLAB live scripts support most MuPAD functionality, although there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

To access built-in MuPAD functions at the MATLAB command line, use evalin(symengine,...) or feval(symengine,...). These functions are designed to work like the existing MATLAB evalin and feval functions.

evalin and feval do not open a MuPAD notebook, and therefore, you cannot use these functions to access MuPAD graphics capabilities.

evalin

For evalin, the syntax is

y = evalin(symengine,'MuPAD_Expression');

Use evalin when you want to perform computations in the MuPAD language, while working in the MATLAB workspace. For example, to make a three-element symbolic vector of the sin(kx) function, k = 1 to 3, enter:

y = evalin(symengine,'[sin(k*x) $ k = 1..3]')
y =
[ sin(x), sin(2*x), sin(3*x)]

feval

For evaluating a MuPAD function, you can also use the feval function. feval has a different syntax than evalin, so it can be simpler to use. The syntax is:

y = feval(symengine,'MuPAD_Function',x1,...,xn);

MuPAD_Function represents the name of a MuPAD function. The arguments x1,...,xn must be symbolic variables, numbers, or character vectors. For example, to find the tenth element in the Fibonacci sequence, enter:

z = feval(symengine,'numlib::fibonacci',10)
z =
55

The next example compares the use of a symbolic solution of an equation to the solution returned by the MuPAD numeric fsolve function near the point x = 3. The symbolic solver returns these results:

syms x
f = sin(x^2);
solve(f)
ans =
0

The numeric solver fsolve returns this result:

feval(symengine, 'numeric::fsolve',f,'x=3')
ans =
x == 3.0699801238394654654386548746678

As you might expect, the answer is the numerical value of 3π. The setting of MATLAB format does not affect the display; it is the full returned value from the MuPAD 'numeric::fsolve' function.

evalin vs. feval

The evalin(symengine,...) function causes the MuPAD engine to evaluate a character vector. Since the MuPAD engine workspace is generally empty, expressions returned by evalin(symengine,...) are not simplified or evaluated according to their definitions in the MATLAB workspace. For example:

syms x
y = x^2;
evalin(symengine, 'cos(y)')
ans = 
cos(y)

Variable y is not expressed in terms of x because y is unknown to the MuPAD engine.

In contrast, feval(symengine,...) can pass symbolic variables that exist in the MATLAB workspace, and these variables are evaluated before being processed in the MuPAD engine. For example:

syms x
y = x^2;
feval(symengine,'cos',y)
ans = 
cos(x^2)

Floating-Point Arguments of evalin and feval

By default, MuPAD performs all computations in an exact form. When you call the evalin or feval function with floating-point numbers as arguments, the toolbox converts these arguments to rational numbers before passing them to MuPAD. For example, when you calculate the incomplete gamma function, the result is the following symbolic expression:

y = feval(symengine,'igamma', 0.1, 2.5)
y =
igamma(1/10, 5/2)

To approximate the result numerically with double precision, use the double function:

format long
double(y)
ans =
    0.028005841168289

Alternatively, use quotes to prevent the conversion of floating-point arguments to rational numbers. (The toolbox treats arguments enclosed in quotes as character vectors.) When MuPAD performs arithmetic operations on numbers involving at least one floating-point number, it automatically switches to numeric computations and returns a floating-point result:

feval(symengine,'igamma', '0.1', 2.5)
ans =
0.028005841168289177028337498391181

For further computations, set the format for displaying outputs back to short:

format short