# Documentation

## Generate Simscape Equations

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 `simscapeEquation` function translates the results of symbolic computations to Simscape language equations.

If you work in the MuPAD® Notebook app, see Create Simscape Equations from MuPAD Expressions.

### 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 equation:

```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(s)`

`simscapeEquation` generates the following code:

```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 `ans` and the equal sign that follows it.

`simscapeEquation` converts any derivative with respect to the variable t to the Simscape notation, `X.der`, where `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 `simscapeEquation(LHS, RHS)`:

```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, `simscapeEquation` generates valid code for the equation section of a Simscape component file:

`simscapeEquation(s)`
```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:

`syms x clear`

If you perform symbolic computations in the MuPAD Notebook app and want to convert the results to Simscape equations, use the `generate::Simscape` function in MuPAD.

### Limitations

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 the equation section of a Simscape component file does not support. `simscapeEquation` cannot correctly convert these equations to Simscape equations. Such expressions do not trigger an error message. The following types of expressions are prone to invalid conversion: