In mathematics, Function is a relation from a set of inputs to a set of possible outputs where each input is related to exactly one output. e.g. y = f(x) i.e. y is a function of x. This concept is borrowed by programming languages to simplify the code by avoiding unnecessary repetition. If you are following the series you might have been introduced to several commands/functions in self-help sections. In this post, we are emphasizing in this area only to understand it better and utilize it for effective coding. Like the earlier articles, we encourage readers planning to learn octave/Matlab to try the exercises marked as “Ex“.
Octave has many Built-in functions to simplify the life of a programmer. It doesn’t only reduce the task, at the same time it also enhances the reliability (due to reduced chances of manual error) and reproducibility (due to multiple people using the same function, not their own implementation) of code. Standard functions are distributed as function files, organized by topic, in subdirectories of octave-home/lib/octave/version/m. Names of all the directories are also listed under the Octave documentation Tab under header “Organization of Functions Distributed with Octave“.
e.g. Subdirectory elfun contains definitions of elementary trigonometric functions. Each function is defined in a separate file that is named after the function themself. A summary of different files(functions) under elfun along with their use is listed and categorized below.
Operations in radian
sin (X) Compute the sine for each element of X in radians.
cos (X) Compute the cosine for each element of X in radians.
tan (X) Compute the tangent for each element of X in radians.
csc (X) Compute the cosecant for each element of X in radians.
sec (X) Compute the secant for each element of X in radians.
cot (X) Compute the cotangent for each element of X in radians.
Operations in degree
sind (X) Compute the sine for each element of X in degrees.
cosd (X) Compute the cosine for each element of X in degrees.
tand (X) Compute the tangent for each element of X in degrees.
cscd (X) Compute the cosecant for each element of X in degrees.
secd (X) Compute the secant for each element of X in degrees.
cotd (X) Compute the cotangent for each element of X in degrees.
Inverse operation in radian
asin (X) Compute the inverse sine in radians for each element of X.
acos (X) Compute the inverse cosine in radians for each element of X.
atan (X) Compute the inverse tangent in radians for each element of X.
acsc (X) Compute the inverse cosecant in radians for each element of X.
asec (X) Compute the inverse secant in radians for each element of X.
acot (X) Compute the inverse cotangent in radians for each element of X.
Inverse operation in degree
asind (X) Compute the inverse sine in degrees for each element of X.
acosd (X) Compute the inverse cosine in degrees for each element of X.
atand (X) Compute the inverse tangent in degrees for each element of X.
acscd (X) Compute the inverse cosecant in degrees for each element of X.
asecd (X) Compute the inverse secant in degrees for each element of X.
acotd (X) Compute the inverse cotangent in degrees for each element of X.
sinh (X) Compute the hyperbolic sine for each element of X.
cosh (X) Compute the hyperbolic cosine for each element of X.
tanh (X) Compute the hyperbolic tangent for each element of X.
csch (X) Compute the hyperbolic cosecant for each element of X.
sech (X) Compute the hyperbolic secant for each element of X.
coth (X) Compute the hyperbolic cotangent for each element of X.
Ex: Try few of the above mentioned functions in your octave environment to get familiarized with them. Ex: Navigate to octave-home/lib/octave/version/m and discover different functions arranged in different sub-directories.
Knowing the correct function simplifies the use of Octave and helps the user to focus on the current problems rather than the specific implementation of algorithms. For avoiding repeatability and simplifying the code, a custom function can also be written and used. As the custom function is the topic of an upcoming post, so we will not dig deeper into it here.
Ex: Find the standard deviation of elements of vector [34.02, 34.5, 33.91, 35.16, 33.45, 34.59]
To solve the above problem statement one can use two different methods:
- Write a code to find the standard deviation by implementing the formulae.
- Find the function to calculate mean, or calculate it using operators.
- Find the deviation of each element.
- Square the deviations.
- Add all the squares of deviations and divide it by the number of elements.
- Find the function to get square-root for getting the standard deviation.
- Try to find a function that calculates the standard deviation directly.
In the first method, one needs to search for at least 2 functions, and the chances of error are also high. It is better to look for the exact function needed for the task. Finding the correct function can be key to faster execution along with reduced chances of error. Hence we will explore a few methods to find the exact function we need for the problem in hand.
- Use the help function for finding a list of all the functions.
>> help --list
- If some definition or string is known one can use “lookfor” command. The entire help text of each function can be searched by using the “-all” argument. All searches are case insensitive.
>> lookfor "standard deviation"
>> lookfor -all "standard deviation"
- Visit Octave-Forge for a complete list of functions with description.
Ex: search for a function which can check if a NAME exists as a variable, function, file, directory, or class.
Commands are functions that only accept string input arguments. Any regular function can be used as a command if it accepts string input arguments.
>>toupper("lower_case") % function to convert lower case argument to upper case ans = LOWER_CASE >>toupper lower_case % function used as command ans = LOWER_CASE
The general form of a command call is
cmdname arg1 arg2 …
Octave can’t tell the difference between a variable name and an ordinary string, hence commands can result in the unwanted results when one of the string input arguments is stored in a variable.
>>strvar = "hello world"; % semicolon after expression suppresses the output display. >>toupper strvar ans = STRVAR >>toupper (strvar) ans = HELLO WORLD
The package is the bundle of functions providing extra functionality for GNU Octave. Sometimes when in-built functions fall short to do an intended task, one can rely on 3rd party packages to get an enhanced set of features. Octave Forge is a central location for the development of packages for GNU Octave. These are similar to Matlab’s toolboxes. To install a package, use the “pkg” command from the Octave prompt by typing:
>> pkg install -forge package_name
Users can browse through Octave Forge repository to correctly identify the package and pre-requisites first.
Ex: Try to install "signal" package in octave environment. Ex: Use help function to list functions of newly installed package.
This basic introductory article may serve as a handy tool when searching for an appropriate function. May you find the exact functionality required for the task in hand, else we will help you develop custom functions in the upcoming article.
- GNU Octave – Scientific Programming Language
- Octave:1> Getting Started
- Octave:1> Variables and Constants
- Octave:1> Expressions and operators
- Octave:1> Built-In Functions, Commands, and Package
- Octave:2> Vectors and Matrices Basics
- Octave:2> Scripts and Algorithm
- Octave:2> Conditional Execution
- Octave:2> Functions
- Octave:3> File Operations
- Octave:3> Data Visualization