generate
::Simscape
Generate Simscape equation
MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
generate::Simscape(e
, <NoWarning>)
generate::Simscape(e)
generates Simscape™ code
for the MuPAD^{®} expression e
.
Simscape software extends the Simulink^{®} product line with tools for modeling and simulating multidomain physical systems, such as those with mechanical, hydraulic, pneumatic, thermal, and electrical components. Unlike other Simulink blocks, which represent mathematical operations or operate on signals, Simscape blocks represent physical components or relationships directly. With Simscape blocks, you build a model of a system just as you would assemble a physical system. For more information about Simscape software, see Simscape.
You can extend the Simscape modeling environment by creating
custom components. When you define a component, use the equation section
of the component file to establish the mathematical relationships
among a component's variables, parameters, inputs, outputs, time,
and the time derivatives of each of these entities. MuPAD and Simscape software
let you perform symbolic computations and use the results of these
computations in the equation section. The generate::Simscape
function
translates the results of symbolic computations to Simscape language
equations.
generate::Simscape
returns a Simscape formatted
string representing an expression, equation, list of expressions or
equations, or a matrix.
generate::Simscape
converts the identifier t
to
the variable time
in the resulting Simscape code.
However, the name t
of a function call does not
change during conversion. See Example 1 and Example 2.
generate::Simscape
converts any derivative
with respect to the variable t
to the Simscape notation x.der
,
where x
is the timedependent variable. See Example 3.
generate::Simscape
assumes that the type
of converted data is double
. See Example 4.
When generating Simscape code for a matrix, the generator produces a dense matrix. See Example 5.
Use the generate::optimize
function
to optimize the MuPAD code before converting it to the Simscape syntax.
See Example 6.
generate::Simscape
converts piecewise expressions
to Simscape code by using the if
statements.
See Example 7.
The equation section of a Simscape component file supports
a limited number of functions. For details and the list of supported
functions, see Simscape equations
. If a symbolic equation contains the functions
that are not available in the equation section of a Simscape component
file, generate::Simscape
cannot correctly convert
these equations to Simscape equations. Such expressions do not
trigger an error. The following types of expressions are prone to
invalid conversion:
Expressions with infinities
Expressions that contain programming structures, such
as loops, coditional statements (except for the if
statement),
and map
function
calls
Expressions that contain intervals, sets, and lists
To display generated Simscape code on screen, use the print
function. To remove
quotation marks and to expand special characters like line breaks
and tabs, use the printing option Unquoted
. If
a generated code line is longer than the TEXTWIDTH
setting, the print
function breaks
that line into several shorter lines. The inserted line continuation
character (\
) is not valid in Simscape. To
avoid inserting line continuation characters, increase the TEXTWIDTH
setting
or use the fprint
function
to write generated code to a file.
To write generated Simscape code to a file, use the fprint
function with
the Unquoted
option. See Example 8.
The NoWarning
option lets you suppress warnings.
See Example 2.
The generate::Simscape
function replaces
all instances of the MuPAD identifier t
with
the variable time
. For example, convert the following
equation to the Simscape equation:
e := A*sin(w*t) + B*cos(w*t) = 0: print(Unquoted, generate::Simscape(e))
B*cos(time*w)+A*sin(time*w) == 0.0;
The generate::Simscape
function does not
change the function name t
in function calls:
print(Unquoted, generate::Simscape([t(), t(0), t(x)]))
Warning: Function 't' not verified to be a valid Simscape function.
t(); t(0.0); t(x);
This example produces a few identical warnings. If you started
using generate::Simscape
recently, warnings can
help you identify potential issues in the converted code. If you want
to suppress warnings, use the NoWarning
option:
print(Unquoted, generate::Simscape([t(), t(0), t(x)], NoWarning))
t(); t(0.0); t(x);
When generating Simscape code, the generate::Simscape
function
converts the derivatives with respect to the variable t
to
the Simscape notation x.der
. Here x
is
the timedependent variable. For example, generate the Simscape code
for the equation e
that has two timedependent
variables:
e := x'(t) + diff(y(t), t) + 2*x + 5 = 0: print(Unquoted, generate::Simscape(e))
x*2.0+x.der+y.der+5.0 == 0.0;
By default, Simscape stores all numeric values as doubleprecision
floatingpoint values. In accordance with this default data type, generate::Simscape
converts
the elements of expressions, equations, and matrices to the double
format:
print(Unquoted, generate::Simscape(x^2 + y/3 + 1/6))
y*(1.0/3.0)+x^2+1.0/6.0;
generate::Simscape
can generate Simscape code
for a MuPAD matrix. In contrast to generate::MATLAB
(which produces sparse
matrices), the Simscape code generator produces dense matrices:
A:= matrix([[1, 0, 0],[0, 0, 1]]): print(Unquoted, generate::Simscape(A))
[ 1.0 0.0 0.0 0.0 0.0 1.0 ];
The generate::Simscape
function does not
optimize your code:
print(Unquoted, generate::Simscape([x = a + b, y = (a + b)^2])):
x == a+b; y == (a+b)^2;
You can use the generate::optimize
function
before converting your MuPAD code to Simscape syntax. For
example, this function can reduce the number of operations by finding
common subexpressions:
print(Unquoted, generate::Simscape( generate::optimize([x = a + b, y = (a + b)^2]) )):
x == a+b; y == x^2;
The generate::Simscape
function also accepts
piecewise expressions. The function uses if
statements
when generating Simscape code for piecewise expressions. For
example, the Fourier transform of the following expression is a piecewise
function:
FT := fourier(exp(abs(x)*abs(t))*sin(t)/t, t, s)
generate::Simscape
converts this result to
a valid Simscape expression:
print(Unquoted, generate::Simscape(FT))
if (x ~= 0.0) atan((s1.0)/abs(x))+atan((s+1.0)/abs(x)); else NaN; end
To create a text file with a Simscape formatted string
representing a symbolic expression, use the fprint
command:
e := x'(t) + 2*x + 5 = 0: fprint(Unquoted, Text, "eqn.txt", generate::Simscape(e))
If the file eqn.txt
already exists, fprint
replaces the
existing Simscape code with the converted symbolic expression.
You can open and edit the resulting text file.

An expression, equation, list of equations, or a matrix 

Suppress warnings. 
generate::Simscape
returns a string
containing Simscape code.
In case of invalid conversion, the returned value is an arbitrary
string.