# Simulation eBook - Functions

# Functions

The sections on expressions and parameters shows how component and source values can be defined using arithmetic equations. The examples used to illustrate this used only simple linear expressions. This section introduces the concept of functions.

Functions hugely expand the power of parameters and expressions by allowing the creation of expressions including non-linear functions of other parameters.

## Predefined functions

EasyEDA has a number of pre-defined functions. With the exception of the softlim(ip, lo, hi, sharp) function which must be copied and pasted by hand, all are immediately available to be used in expressions because they are built-in to LTspice.

For a complete list of functions and information on their usage, please see Help in a locally installed copy of LTspiceXVII or:

and:

Some of the most frequently used functions are listed, together with illustrative examples, in the table below.

Note that all the functions in this list can be used in any context in EasyEDA: in the value fields and in expressions for component values (E), Independent Sources (I) and for B Sources (B).

| **Function** | **Description** | **LTspice native or EasyEDA special** | **Where useable** | | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- | ----------------- | --- | --------------------------------------------------------------------------------- | ------------ | ------- | ------- | | abs(x) | Absolute value of x | LTspice | E, I, B | | acos(x) arccos(x) | Arc cosine of x. | LTspice | E, I, B | | acosh(x) | Real part of the arc hyperbolic cosine of x, e.g., acosh(.5) returns 0, not 1.0472i | LTspice | E, I, B | | asin(x) arcsin(x) | Arc sine of x. | LTspice | E, I, B | | asinh(x) | Arc hyperbolic sine | LTspice | E, I, B | | atan(x) arctan(x) | Arc tangent of x | LTspice | E, I, B | | atan2(y,x) | 4 quadrant arc tangent of x/y (tan^-1(x/y)) | LTspice | E, I, B | | atanh(x) | Arc hyperbolic tangent. | LTspice | E, I, B | | buf(x) | Returns 1 if x > 0.5, else 0 | LTspice | E, I, B | | ceil(x) | Integer equal or greater than x | LTspice | E, I, B | | cos(x) | Cosine of x | LTspice | E, I, B | | cosh(x) | Hyperbolic cosine of x | LTspice | E, I, B | | exp(x) | e to the power of x | LTspice | E, I, B | | floor(x) | Integer equal to or less than x | LTspice | E, I, B | | if(x,y,z) | IF x > 0.5, THEN y ELSE z | LTspice | E, I, B | | placeholder | Placeholder for other function | LTspice | E, I, B | | int(x) | Convert x to integer | LTspice | E, I, B | | inv(x) | Returns 0 if x > 0.5, else 1 | LTspice | E, I, B | | limit(x, L, U) | Value of x, bounded by L and U | LTspice | E, I, B | | ln(x) log(x) | Natural logarithm of x. | LTspice | E, I, B | | placeholder | Placeholder for other function | LTspice | E, I, B | | log10(x) | Base 10 logarithm. Generates a real valued output for all x. | LTspice | E, I, B | | max(x,y) | The greater of x or y | LTspice | E, I, B | | min(x,y) | The smaller of x or y | LTspice | E, I, B | | pow(x,a) | Real part of x raised to the power of a. Zero for negative x and fractional a | LTspice | E, I, B | | pwr(x,a) | The absolute value of x raised to the power of a | LTspice | E, I, B | | pwrs(x,a) | pwr(x) multiplied by the sign of x | LTspice | E, I, B | | sgn(x) | Sign of x. Returns -1 for x < 0, 0 for x == 0 (where == means 'exactly equal to') and 1 for x > 0 | LTspice | E, I, B | | sin(x) | Sine of x | LTspice | E, I, B | | sinh(x) | Hyperbolic sine of x | LTspice | E, I, B | | softlim(ip, lo, hi, sharp) | Value of ip, bounded by lo and hi with the sharpness of the transition between linear and limited regions defined by 'sharp'. | EasyEDA | E, I, B | | sqrt(x) | Real part of the square root of x. Zero for negative x | LTspice | E, I, B | | tan(x) | Tangent of x | LTspice | E, I, B | | tanh(x) | Hyperbolic tangent of x | LTspice | E, I, B | | u(x) | Unit step, i.e., 1 if x > 0., else 0 | LTspice | E, I, B | | placeholder | Placeholder for other function | LTspice | E, I, B | | uramp(x) | x if x > 0, else 0 | LTspice | E, I, B |

## User defined functions

There may be occasions where a function is required that maybe has to be used in several places in a schematic or it is useful in several different schematics. To save having to copy and paste a complicated expression as a block of text each time it is needed, the .func statement makes it is possible to create a user defined function.

The syntax of the .func statement is very simple:

`.func myfunctionname(a,b,c, ...n) {expression of functions of a, b, c ... n}`

For example:

`.func hypotenuse(x,y) {sqrt(x^2+y^2)}`

defines a function that calculates the length of the hypotenuse of a right angle triangle with sides length x and y.

Once a function has been defined in a schematic in a project in this way it can be used anywhere in that schematic in that project. It does, however, have to be defined in every sheet in every project in which it is to be used but, if it's a really useful function, let us know and maybe we'll add it to the growing list of pre-defined functions!

To use the function all that is then required is to paste `hypotenuse(x,y)`

into wherever it is needed and to substitute the 'x' and 'y' with the required variables. So, for example to use the function in a parametric expression:

`.param a=3 b=4 hypot=hypotenuse(a,b)`

or in a current output B Source driven by two voltages, V(oneside) and V(otherside):

`I=hypotenuse(V(oneside), V(otherside))`

There are many examples of functions defined by the .func statement in the simulations linked to in the table above and in all EasyEDA spice netlists for the automatically appended predefined functions.

**Note that when used in .func statements, expressions can wrap over more than one line by using the '+' continuation character.**

The softlim(ip, lo, hi, sharp) function is an example of this in the table above:

```
.func softlim(ip, lo, hi, sharp)
+ {uramp(((u(ip/2+0.125/(max(abs(sharp),1)*0.5/(hi-lo))-hi/2))*(max(abs(sharp),1)*0.5/(hi-lo))*
+ -1*(uramp(0.25/(max(abs(sharp),1)*0.5/(hi-lo))+hi-ip)**2) +
+ (1-u(ip/2+0.125/(max(abs(sharp),1)*0.5/(hi-lo))-hi/2))*(ip-hi)+hi)-(hi+lo)/2) -
+ uramp(-1*((1-u(ip/2-0.125/(max(abs(sharp),1)*0.5/(hi-lo))-lo/2))*(max(abs(sharp),1)*0.5/(hi-lo))*
+ uramp(ip+0.25/(max(abs(sharp),1)*0.5/(hi-lo))-lo)**2 +
+ u(ip/2-0.125/(max(abs(sharp),1)*0.5/(hi-lo))-lo/2)*(ip-lo)+lo)+(hi+lo)/2) + (hi+lo)/2}
```

2

3

4

5

6

7