MATLAB Lesson 4 - Functions

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.
Try the following MATLAB command:

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.

>>  help sqrt



 

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 functionDescriptionExample
sqrtsquare rootsqrt(2)
expexponentialexp(1)
log (natural) logarithm (base e)log( exp(2) )
log2 logarithm base 2log2(16)
log10logarithm base10log10(1e4)

 

Trigonometric functions

MATLAB functionDescriptionExample
sinsinesin(pi/6)
coscosinecos(pi/3)
tantangenttan(pi/4)
asininverse sine (arcsine)asin(1/2)
acosinverse cosine (arccos)acos(1/2)
ataninverse 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 functionDescriptionExample
sinhhyperbolic sine sinh(pi/6)
coshhyperbolic cosine cosh(1)
tanhhyperbolic tangent tanh(pi/4)
asinhinverse hyperbolic sine asinh(0.5)
acoshinverse hyperbolic cosine acosh(0.5)
atanhinverse hyperbolic tangent atanh(0.5)

 

Rounding functions

MATLAB functionDescriptionExample
roundroundround(2.5)
floorround towards negative infinity floor(-2.6)
ceilround towards positive infinity ceil(-2.6)
fixround towards zerofix(-1.9)
modremainder after divisionmod(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 functionDescriptionExample
factorialfactorial n or n! factorial(4)
nchoosekbinomial coefficient Cnk nchoosek(4,2)
factorprime factorsfactor(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.
Find the smaller of pi and 22/7.

The mathematical constant pi is approximately 3.1416, while 22/7 is approximately 3.1429, which is larger than pi.

>>  min(pi, 22/7)
>>  max(pi, 22/7)

 

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.

Find the square roots of the integers 1,...,10.

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.

>>  sqrt(1:10)


Find the sine of -pi/2, -pi/4, 0, pi/4, pi.

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.

>>  sin( (pi/2)*[-1:1/2:1] )

 

Sometimes the result of the function has fewer elements than the input.

Find the maximum and the minimum of 132, 129, 66, 120.

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.

>>  x = [132 129 66 120]
>>  max(x)
>>  min(x)
Find the sum of k3 for k = 1,...,20.

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.

>>  w = [1:20].^3
>>  sum(w)

 

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.

What do you expect the following functions to produce?

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.

>>  log(-1)
>>  asin(2)




 

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.