Products & Services Solutions Academia Support User Community Company

Learn more about Symbolic Math Toolbox   

Integration of MuPAD and MATLAB

Copying Variables and Expressions Between the MATLAB Workspace and MuPAD Notebooks

You can copy a variable in a MuPAD notebook to a variable in the MATLAB workspace using a MATLAB command. Similarly, you can copy a variable or symbolic expression in the MATLAB workspace to a variable in a MuPAD notebook using a MATLAB command. In order to do either assignment, you need to know the handle to the MuPAD notebook you want to address.

The only way to assign variables between a MuPAD notebook and the MATLAB workspace is to start the notebook using the syntax

nb = mupad;

(you can use any variable name for the handle nb) or open an existing notebook file with

nb = mupad(file_name);

This handle is used only for communication between the MATLAB workspace and the MuPAD notebook.

Copying and Pasting Using the System Clipboard

You can also copy and paste between notebooks and the MATLAB workspace using standard editing commands. If you copy a result in a MuPAD notebook to the system clipboard, you may get the text associated with the expression, or a picture, depending on your operating system and application support.

For example, consider the following MuPAD expression.

Select the output with the mouse and copy it to the clipboard.

Paste this into the MATLAB workspace. The result is text:

exp(x)/(x^2 + 1)

If you paste it into WordPad on a Windows® system, the result is a picture.

Calling MuPAD Functions at the MATLAB Command Line

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

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. You can use evalin for any MuPAD expression.

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 strings. 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. (For information on this function, enter doc(symengine,'numeric::fsolve') at the MATLAB command line.)

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

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

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

Usage of evalin vs. feval

The evalin(symengine,...) function causes the MuPAD engine to evaluate a string. 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)

The variable y is not expressed in terms of x because y is not known to the MuPAD engine workspace.

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)

Clearing Assumptions and Resetting the Symbolic Engine

The symbolic engine workspace associated with the MATLAB workspace is usually empty. The MATLAB workspace keeps track of the values of symbolic variables, and passes them to the symbolic engine for evaluation as necessary. However, the symbolic engine workspace contains all assumptions you make about symbolic variables, such as whether a variable is real or positive. These assumptions can affect solutions to equations, simplifications, and transformations, as explained in Examples of the Effect of Assumptions.

Here is an example of how the MATLAB workspace and the symbolic engine workspace respond to a sequence of commands:

StepCommandMATLAB workspaceMuPAD Engine Workspace
1syms x positivexx is positive
2clear xemptyx is positive
3syms xxx is positive
4syms x clearxempty

Checking a Variable's Assumptions

To check whether a variable, say x, has any assumption in the symbolic engine workspace associated with the MATLAB workspace, enter the command

evalin(symengine,'getprop(x)')

at the MATLAB command line.

For example:

syms x real
evalin(symengine,'getprop(x)')

ans =
R_

syms z
evalin(symengine,'assume(z <> 0)')
evalin(symengine,'getprop(z)')
 
ans = 
C_ minus {0}

syms z clear
evalin(symengine,'getprop(z)')
 
ans = 
C_

For more information about the basic sets that can be returned as assumptions, enter

doc(symengine,'solvelib::BasicSet')

Examples of the Effect of Assumptions

Assumptions can change the answers that the solve function returns, and can change the results of simplifications. The only assumptions you can make using MATLAB commands are real or positive.

For example, consider what transpires when solving the equation x^3 = 1:

syms x
solve('x^3=1')
 
ans =
                     1
 - (3^(1/2)*i)/2 - 1/2
   (3^(1/2)*i)/2 - 1/2

syms x real
solve('x^3=1')
 
ans =
1

However, clearing x does not change the underlying assumption that x is real:

clear x
syms x
solve('x^3=1')
 
ans =
1

Clearing x with syms x clear clears the assumption:

syms x clear
solve('x^3=1')
 
ans = 
                     1
 - (3^(1/2)*i)/2 - 1/2
   (3^(1/2)*i)/2 - 1/2

Using evalin or feval, you can make a variety of assumptions about an expression; see Calling MuPAD Functions at the MATLAB Command Line. All such assumptions can be cleared with the command syms x clear, as in this example:

evalin(symengine,'assume(a <> 0)');
evalin(symengine,'solve(a*x^2+b*x+c=0,x)')
 
ans =
{-(b - (b^2 - 4*a*c)^(1/2))/(2*a),...
 -(b + (b^2 - 4*a*c)^(1/2))/(2*a)}

syms a clear
evalin(symengine,'solve(a*x^2+b*x+c=0,x)')
 
ans =
piecewise([a <> 0, {-(b - (b^2 - 4*a*c)^(1/2))/(2*a),...
  -(b + (b^2 - 4*a*c)^(1/2))/(2*a)}],...
  [a = 0 and b <> 0, {-c/b}], [a = 0 and b = 0 and c = 0, C_],...
  [a = 0 and b = 0 and c <> 0, {}])
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS