About User-Defined Functions
There are three broad categories of user-defined functions — Analytic, Interpolation, and Piecewise — and a number of templates for common function types, such as step and ramp functions. You can also create external function interfaces to include functions written in C and MATLAB®. Functions can be global or local in a model component, although external functions and MATLAB functions can only be defined globally.
User-defined functions (including functions defined by materials, thermodynamic property packages, M-files in MATLAB, and external DLLs) can be used in definitions of parameters and in a wide range of settings (where you can also use parameters), including definitions of geometry objects. To access user-defined functions defined under a component from other components and from nodes under Global Definitions, add the component’s name as a prefix — for example, comp1.f(x). You can also use a user-defined function (with a single argument) as the function to apply in the Range dialog box.
Function Names and Calling Functions
Function names for built-in mathematical functions such as abs, cos, and test are reserved function names, and naming a user-defined function using one of the reserved function names is not recommended because it can cause unexpected results. If the name that you type in the Function name field is a reserved function name, the text color changes to orange, as a warning. If you move the cursor to a function name in orange, the tooltip is a reserved name is displayed.
If you want to use a user-defined function in the model settings or for postprocessing, then call it with its arguments, such as func1(x,y) for a function with two arguments describing a spatial variation in a 2D geometry.
Plotting Functions
Click the Plot button () in the upper-right corner of the Settings window to plot any user-defined function of 1–3 variables directly in a separate Function Plot window.
Click the Create Plot button () in the upper-right corner of the Settings window to create a persistent plot of the function under Results, including a Grid dataset with the function as the source data, a plot group and a plot feature. The added nodes can be used for plotting multiple functions on top of each other or for direct comparison to model results, for example.
For analytic functions, first define a range for the arguments in the Plot Parameters section. Note that the range is expressed in the current unit system’s base unit corresponding to the set function argument unit. For example, if the current unit system is SI and the function argument unit is cm, the plot range is expressed in m.