JMathLabTutorial:7 Functions

From jWork.ORG
Jump to: navigation, search
Limitted access. Reguest membership or login to this link first if you are already a member
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:


x=acos(0.4); printf('%f',x)

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


help("acos")

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.


a=log(sqrt(854)); printf('%f',a)         % natural logarithm

Try also these examples:


a=0.5*log(854); printf('%f\n',a) 
a=float(sin(pi/2));printf('%f\n',a)          % argument in radian
a=gammaln(1234);  printf('%f\n',a)           % log( gamma( x ) )
a=primes(1000000000000000001);  printf('%f\n',a)      
a=factorial(35); printf('%f\n',a)

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


a=factorial(rat(8));   printf('%f',a)    % to make it exact.

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.


function y=example(x) y=2*x; end
printf('%f',example(3.123) )

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:


function y=test1(x) 
         y=2*x;
         printf('%f',y)
end

test1(10)

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:


path

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.


help("acosh")

where "acosh" is the name of the function. This will generate a dialog which shows how to use this function.

Passing functions to other functions

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


function y=fit(a,x) % build function to fit data
y=a(1)*exp(-(x-a(2)).^2/a(3)^2); end;

d=fit([1 4 5],5) % testing function
printf('fit=%f\n',d)

% this is another function which can take any function as argument
function y=newtest(ff,a,x)
      y=ff(a,x)
end

a = newtest(@fit,[10,20,30],10)
a = newtest(@fit,[20,2,30],10)
printf('test=%f\n',a)

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.