Simscape™ functions model a class of pure first-order mathematical functions with explicit input-output relationship. These functions explicitly map the inputs of numerical values into outputs of numerical values by using declarative expressions. When a component calls a Simscape function, numerical input values are passed to the function, which then evaluates these declarative expressions to compute the output values.
Each function must be in a separate Simscape file. The file name must match the function name. For example, function
foo must be in a file called
The Simscape function file must start with the keyword
followed by the function header, which includes the function name, inputs, and outputs. For
function out = MyFunction(in1,in2)
If the function has multiple return values, the syntax is:
function [out1,out2] = MyFunction(in1,in2)
The body of the function must be enclosed inside the
section, for example:
function out = SumSquared(in1,in2) definitions out = in1^2 + 2*in1*in2 + in2^2; end end
The file name must match the function name. For example, function
foo must be in a file called
One or more output parameters are allowed.
If an output parameter is not used on the left-hand side of the
definitions section, you get an error.
Zero or more input parameters are allowed.
When the function is called, the number of input arguments must match the number of input parameters.
Input parameters are positional. This means that the first input argument during the function call is passed to the first input parameter, and so on. For example, if you write an equation:
o == SumSquared(5,2);
in1 is 5 and
in2 is 2.
If the function has multiple return values, they are also positional. That is, the first output parameter gets assigned to the first return value, and so on.
If the function has multiple return values, the rules and restrictions are the same as for declaration functions. For more information, see Multiple Return Values.
definitions section can contain intermediate terms and
if-elseif-else statements. The same syntax rules as in the
declaration section of a
let statement apply. For more information,
see Using Intermediate Terms in Equations.
definitions section cannot contain expressions with dynamic
semantics, such as
Simscape function files can reside directly on MATLAB® path or in package directories. For more information, see Organizing Your Simscape Files.
You can use source protection, as described in Using Source Protection for Simscape Files.
Importing a package imports all the Simscape functions in this package. For more information, see Importing Domain and Component Classes.
If a MATLAB function and a Simscape function have the same name, the MATLAB function has higher precedence.
The purpose of Simscape functions is to reuse expressions in equations of multiple components, as well as in member declarations of domain or component files.
For example, exponential diode equations often use an expression that is a modification
exp(, to provide protection for large
i. For details, see Diode and NPN Bipolar Transistor block reference
pages. The Simscape Functions example shows how you can
write a Simscape function to reuse this expression, instead of repeating it in every
function out = userFunction(x,y,z) definitions out = if x > y (x-z)*exp(y); elseif x < -z (x+y)*exp(-z); else exp(x) end end end
Then, the Diode block can call this function with
z values of
equations o == SimscapeFunction.Use.Functions.userFunction(i,80,79); end
and the NPN Bipolar Transistor block can call the same
function with values of
equations o == SimscapeFunction.Use.Functions.userFunction(i,40,39); end
Simscape language has a variety of tools that facilitate code reuse. Simscape functions and declaration functions let you reuse expressions. Subclassing and composite components let you reuse equations.
To reuse expressions across multiple components:
Use Simscape functions to reuse expressions in equations and member declarations.
Use declaration functions in member declarations to reuse expressions that are out of Simscape expression capability. For more information, see Declaration Functions.
|Functionality||Authoring Language||File extension||Usage||Supports Arguments with Units|
.ssc or .sscp
Member declaration and equations
.m or .p
Member declaration only
To reuse equations across multiple components:
Use subclassing to model the "is-a" relationship between the base component and the derived component. The equations in the base component are reused in the derived component. For more information, see Subclassing and Inheritance.
Use composite components to model the "has-a" relationship between the container component and the subcomponents. The equations in the member components are reused in the composite component. For more information, see About Composite Components.