Functions
MATLAB provides an enormous number of functions as well as the ability to define your own functions. Fortunately the names of the functions are very similar to those commonly used in mathematics.
The general syntax is
output_value = function_name(input_value)
Getting more information
In MATLAB, you can use either the help command to provide basic information about a function or the doc command to open the help browser, which can also be accessed from the Help menu or the ? symbol on the MATLAB toolbar.This gives some basic information about the function sqrt
See also lists related functions (very useful). There is also information about overloaded functions(which you can ignore for now), and a link to the reference page in the help browser.
Built-in Functions
Some of the functions known when you initially start MATLAB are listed in the tables below. More information can be found by typing help elfun in the MATLAB command window or using the MATLAB help browser (search for Elementary Math).
Exponential functions
MATLAB function | Description | Example |
sqrt | square root | sqrt(2) |
exp | exponential | exp(1) |
log | (natural) logarithm (base e) | log( exp(2) ) |
log2 | logarithm base 2 | log2(16) |
log10 | logarithm base10 | log10(1e4) |
Trigonometric functions
MATLAB function | Description | Example |
sin | sine | sin(pi/6) |
cos | cosine | cos(pi/3) |
tan | tangent | tan(pi/4) |
asin | inverse sine (arcsine) | asin(1/2) |
acos | inverse cosine (arccos) | acos(1/2) |
atan | inverse tangent (arctan) | atan(1) |
Note that the arguments of sin, cos, tan are always in radians (not degrees) and that the results of the inverse trigonometric functions will also be in radians.
Hyperbolic functions
MATLAB function | Description | Example |
sinh | hyperbolic sine | sinh(pi/6) |
cosh | hyperbolic cosine | cosh(1) |
tanh | hyperbolic tangent | tanh(pi/4) |
asinh | inverse hyperbolic sine | asinh(0.5) |
acosh | inverse hyperbolic cosine | acosh(0.5) |
atanh | inverse hyperbolic tangent | atanh(0.5) |
Rounding functions
MATLAB function | Description | Example |
round | round | round(2.5) |
floor | round towards negative infinity | floor(-2.6) |
ceil | round towards positive infinity | ceil(-2.6) |
fix | round towards zero | fix(-1.9) |
mod | remainder after division | mod(7,3) |
The ceiling of x is the smallest integer which is greater than or equal to x, while the floor of x is the largest integer less than or equal to x.
Discrete mathematics
MATLAB function | Description | Example |
factorial | factorial n or n! | factorial(4) |
nchoosek | binomial coefficient Cnk | nchoosek(4,2) |
factor | prime factors | factor(12) |
More than one input argument
A few functions require more than one input argument, which are separated by commas ,. Examples that we have seen already are mod and nchoosek, which both require two input arguments. MATLAB will produce an error message is you try to use these functions with only one input argument.
Other examples that require more than one input argument are max, which calculates the larger of two inputs, and min which calculates the smaller of two inputs.The mathematical constant pi is approximately 3.1416, while 22/7 is approximately 3.1429, which is larger than pi.
Functions applied to a vector
Most MATLAB functions have been modified (overloaded) so they work with inputs which are vectors as well as scalars. When an argument is a vector the function is applied to each element of the vector, producing a vector of the same size as the input vector.
This will be extremely useful when we come to plotting functions and can also greatly improve the efficiency and readability of your programs.
The colon operator 1:10 can be used to get a vector of input values. The sqrt function then calculates the square root of each element of the input vector.
The colon operator can be used to get the vector of input values (pi/2)*[-1:1/2:1]. The sin function then calculates the sine of each element of the input vector.
Sometimes the result of the function has fewer elements than the input.
The variable x is a row vector with elements 132, 129, 66, 120.
The MATLAB function max finds the largest element of the input vector, while the function min find the smallest element.
The variable w is a row vector with elements k3 for k = 1,...,20, using the element by element power operator .^.
The MATLAB function sum finds the sum of all the elements of the input vector.
Warnings
MATLAB works automatically with complex numbers. Thus many functions which you think should not be defined, do not produce an error message, but do produce a complex answer.
The logarithm function is normally only defined for positive real arguments. For x < 0, the function log(x) produces a complex result.
For a real x, sin(x) is always in [-1, 1], so the inverse sine is normally only defined on the domain [-1, 1]. For |x| > 1, asin(x) produces a complex answer.
Self-test Exercise
Create a vector u with elements ek/10, k = 0,1,...,10.Answer: u = exp( [0:10]/10 )
Use the mouse to select the text between the word "Answer" and here to see the answer.
Summary
MATLAB contains all the standard mathematical functions and many more.
Many MATLAB functions also accept input arguments which are vectors, evaluating the function at each element of the input vector.