Generate Simscape Equations from Symbolic Expressions
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. The Symbolic Math Toolbox™ 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
Convert Algebraic and Differential Equations
Suppose, you want to generate a Simscape equation from
the solution of the following ordinary differential equation. As a
first step, use the
dsolve function to solve the
syms a y(t) Dy = diff(y); s = dsolve(diff(y, 2) == -a^2*y, y(0) == 1, Dy(pi/a) == 0); s = simplify(s)
The solution is:
s = cos(a*t)
Then, use the
simscapeEquation function to
rewrite the solution in the Simscape language:
simscapeEquation generates the following
ans = 's == cos(a*time);'
The variable time replaces all instances
of the variable t except for derivatives with respect
to t. To use the generated equation, copy the equation
and paste it to the equation section of the Simscape component
file. Do not copy the automatically generated variable
the equal sign that follows it.
simscapeEquation converts any derivative
with respect to the variable t to the Simscape notation,
X is the time-dependent variable. For example,
convert the following differential equation to a Simscape equation.
Also, here you explicitly specify the left and the right sides of
the equation by using the syntax
syms a x(t) simscapeEquation(diff(x), -a^2*x)
ans = 'x.der == -a^2*x;'
simscapeEquation also translates piecewise
expressions to the Simscape language. For example, the result
of the following Fourier transform is a piecewise function:
syms v u x assume(x, 'real') f = exp(-x^2*abs(v))*sin(v)/v; s = fourier(f, v, u)
s = piecewise(x ~= 0, atan((u + 1)/x^2) - atan((u - 1)/x^2))
From this symbolic piecewise equation,
valid code for the equation section of a Simscape component file:
ans = 'if (x ~= 0.0) s == -atan(1.0/x^2*(u-1.0))+atan(1.0/x^2*(u+1.0)); else s == NaN; end'
Clear the assumption that x is real by recreating it using
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 (Simscape). If a symbolic expression
contains functions that are not supported by Simscape, then
simscapeEquation cannot represent the symbolic
expression as a Simscape equation and issues a warning instead. Always verify the conversion
result. Expressions with infinities are prone to invalid conversion.