# User defined functions

Posted by admin 12/11/2015 0 Comment(s) FAQs,

Is there a way that I can create functions of my own instead of having to type in long equations every time?

Micro-Cap provides the user the capability to create their own functions from the operators and functions currently available in the program. This can be accomplished through the use of the .DEFINE command statement in the following format:

.DEFINE name[(p1[,p2][...,pn])] f([p1][,p2][...,pn])

where f() is some expression involving the parameters p1, p2, ... pn. name can be any valid, unique name, and the optional parameters p1 to pn can be used to pass a variety of values through to the expression. Parameter values may consist of constants, PART attribute names, expressions, other .DEFINE parameters, or any other elements that are valid within the defined function. When the circuit is run, the parameter values will be substituted into the expression.

These user defined functions can be either local or global. A local function can be defined by typing the .DEFINE statement into the text area or into the schematic area and would only be available for that circuit. A global function is defined by typing the .DEFINE statement into the MCAP.INC file which is accessed by clicking on the User Definitions option under the Options menu. This procedure makes the function available for all circuit simulations. For either method, the function will be available for use in the top level schematic or in the analysis of the circuit.

A common use of the user defined function capability would be to create a function for any often used expressions. Two example functions are:

.define Power(R1) V(R1)*I(R1)
.define Energy(R1) SUM(V(R1)*I(R1),T)

The first expression calculates the power through any two node element such as a resistor, capacitor, inductor, diode, or source. The second expression calculates the energy through any two node element. While R1 as the parameter name seems to imply the use of a resistor, any PART attribute of a two node element may be passed through to the function. The Power(R1) function is available for schematic or analysis use. The Energy(R1) function is only available for use in transient analysis due to the presence of the SUM operator and the T variable.

The power and energy function are simple to use. The R1 parameter that needs to be passed is simply the PART attribute of a valid two node component. Plotting the power and energy through the battery, V1, would be as simple as specifying the plot expressions to be Power(V1) and Energy(V1).