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.
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:
Here is the table with the rebuild functions for scalar values:
|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|
The functions can accept either a value (number), a vector of numbers, or symbolic value. In case of vectors, a function returns a vector.
|atan2($var_1$, $var_2$)||arctangens (radian)|
|nchoosek(N,K)||binomial coefficient $N \choose K$|
See the reference reference API
|inverf($var$)||inverse error function|
|gammaln($var$)||logarithm of gamma 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.
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.where "acosh" is the name of the function. This will generate a dialog which shows how to use this function.
|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.