Generate Simscape equation
MuPAD® notebooks will be removed in a future release. 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) generates Simscape™ code
for the MuPAD® expression
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
translates the results of symbolic computations to Simscape language
generate::Simscape returns a Simscape formatted
string representing an expression, equation, list of expressions or
equations, or a matrix.
generate::Simscape converts the identifier
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 is the time-dependent 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.
generate::Simscape converts piecewise expressions
to Simscape code by using the
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
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
Expressions with infinities
Expressions that contain programming structures, such
as loops, coditional statements (except for the
Expressions that contain intervals, sets, and lists
To display generated Simscape code on screen, use the
a generated code line is longer than the
TEXTWIDTH setting, the
\) is not valid in Simscape. To
avoid inserting line continuation characters, increase the
or use the
to write generated code to a file.
NoWarning option lets you suppress warnings.
See Example 2.
generate::Simscape function replaces
all instances of the MuPAD identifier
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;
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
generate::Simscape recently, warnings can
help you identify potential issues in the converted code. If you want
to suppress warnings, use the
print(Unquoted, generate::Simscape([t(), t(0), t(x)], NoWarning))
t(); t(0.0); t(x);
When generating Simscape code, the
converts the derivatives with respect to the variable
the Simscape notation
the time-dependent variable. For example, generate the Simscape code
for the equation
e that has two time-dependent
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 double-precision
floating-point values. In accordance with this default data type,
the elements of expressions, equations, and matrices to the double
print(Unquoted, generate::Simscape(x^2 + y/3 + 1/6))
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 ];
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
before converting your MuPAD code to Simscape syntax. For
example, this function can reduce the number of operations by finding
print(Unquoted, generate::Simscape( generate::optimize([x = a + b, y = (a + b)^2]) )):
x == a+b; y == x^2;
generate::Simscape function also accepts
piecewise expressions. The function uses
when generating Simscape code for piecewise expressions. For
example, the Fourier transform of the following expression is a piecewise
FT := fourier(exp(-abs(x)*abs(t))*sin(t)/t, t, s)
generate::Simscape converts this result to
a valid Simscape expression:
if (x ~= 0.0) -atan((s-1.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
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
generate::Simscape returns a
string containing Simscape code.
In case of invalid conversion, the returned value is an arbitrary