# JMathLabTutorial:7 Functions

## Contents

# Functions

About 250 predefined functions come with jMathLab. Users can define their own functions either in a separate file (*.m files) or inline in the same script.

# Built-in functions

There are more than 250 built-in functions included with jMathLab.
They are implemented as a external MatLab functions (with the extension .m) or
as a Java classes. You can look at them using the command **path**.
Look at the reference manual Reference manual.

Here is a simple example. Compute the inverse cosine of an angle:

which calls "acos" function. One can get help for this function as:

The most common implemented functions are the square root (sqrt(x)), the trigonometric functions (sin(x), cos(x), tan(x)) and inverses (atan(x), atan2(y,x)), and the hyperbolic functions (exp(x), log(x)). A large number of additional functions are available, see the list in chapter 4. Some functions are specific to integers, and also work with arbitrary large numbers: primes(Z) expands Z into prime factors, factorial(Z) calculates the factorial function. Modular division is provided by divide and treated later in the context of polynomials.

Try also these examples:

Let us calculate factorial. We will use "rat" and therefore we specify syms to use it here:

## Standard functions

Here is the table with the rebuild functions for scalar values:

Name(Arguments) | Function | |
---|---|---|

float($var$) | $var$ as floating point number | |

rat($var$) | $var$ as exact number | |

realpart($var$) | realpart of $var$ | |

imagpart($var$) | imaginary part of $var$ | |

abs($var$) | absolute value of $var$ | |

sign($var$) | sign of $var$ | |

conj($var$) | $var$ conjugate complex | |

angle($var$) | angle of $var$ | |

cfs($var$) [$var_T$]) | continued fraction expansion of $var$ with accuracy $var_T$ | |

primes(VAR) | VAR decomposed into primes |

## Built-in unctions

The functions can accept either a value (number), a vector of numbers, or symbolic value. In case of vectors, a function returns a vector.

Name(Arguments) | Function | |
---|---|---|

sqrt($var$) | squareroot | |

exp($var$) | exponential | |

log($var$) | natural logarithm | |

sinh($var$) | hyperbolic sine | |

cosh($var$) | hyperbolic cosine | |

asinh($var$) | hyperbolic areasine | |

acosh($var$) | hyperbolic areacosine | |

sech($var$) | hyperbolic secans | |

csch($var$) | hyperbolic cosecans | |

asech($var$) | hyperbolic areasecans | |

acsch($var$) | hyperbolic areacosecans | |

sin($var$) | sine (radian) | |

cos($var$) | cosine (radian) | |

tan($var$) | tangens (radian) | |

asin($var$) | arcsine (radian) | |

acos($var$) | arccosine (radian) | |

atan($var$) | arctangens (radian) | |

atan2($var_1$, $var_2$) | arctangens (radian) | |

sec($var$) | secans (radian) | |

csc($var$) | cosecans (radian) | |

asec($var$) | arcsecans (radian) | |

acsc($var$) | arccosecans (radian) | |

factorial(N) | factorial $N!$ | |

nchoosek(N,K) | binomial coefficient $N \choose K$ |

## Special functions

See the reference reference API

Name(Arguments) | Function | |
---|---|---|

erf($var$) | error function | |

inverf($var$) | inverse error function | |

gamma($var$) | gamma function | |

gammaln($var$) | logarithm of gamma function | |

beta($var$) | beta function | |

betaln($var$) | logarithm of beta function | |

j0($var$) | Bessel function of the first kind of order 0 |

as before, the functions can accept values and vectors.

# User functions

## Inline functions

Functions ca be defined inline in the same script from where they are called.

Programming a function is demonstrated in the following example of a function examples(x), which multiplies its argument by 2. After the definition it can be used like any other function.

Following the keyword function is the prototype with a return variable y. This replaces the construct return y of other programming languages.

Here is a more standard way to format a function:

which multiplies the value 10 by 2 and print it.## Functions in files

If functions are to be reused later, they should be written to a text file and saved somewhere in search path. The filename must be the function name extended by ".m", in the present example ttwo.m. In subsequent sessions the function ttwo can be used without separately loading the file. Several installed functions are provided using this mechanism.

You can list all functions which come with the program executing this statement:

It print all modules and functions. Functions typically ends with ".m" (MatLab scripts)
or ".java" (Java-implemented functions). You can get help and examples on a particular function as:
You can add a new directory with your ".m" files using **addpath** methods.

Try to see what "help" function is doing by replacing "acosd" with "help" |

## Passing functions to other functions

Functions can be created in the same macro, and can be passed to other functions as arguments

In this example we pass the function "fit" to the function "newtest". Note we use the symbol "@" to tell the program that the type is "user function". Then we evaluated the function using 3 parameters and one argument x=10.