Using Conditional Expressions in Equations

Statement Syntax

You can specify conditional equations by using `if` statements.

```equations [...] if Expression [...] elseif Expression [...] else [...] end [...] end ```

Each `[...]` section may contain one or more equation expressions.

You can nest `if` statements, for example:

```equations [...] if Expression [...] if Expression [...] else [...] end else [...] end [...] end ```

Restrictions

• Every `if` requires an `else`.

• The total number of equation expressions, their dimensionality, and their order must be the same for every branch of the `if-elseif-else` statement. However, this rule does not apply to the `assert` expressions, because they are not included in the expression count for the branch.

Example

For a component where x and y are declared as 1x1 variables, specify the following piecewise equation:

This equation, written in the Simscape™ language, would look like:

```equations if x >= -1 && x <= 1 y == x; else y == x^2; end end ```

Another way to write this equation in the Simscape language is:

```equations y == if x>=-1 && x<=1, x else x^2 end end ```