Mathematical operations in Octave
In this lesson, I'll explain what are the operators that allow you to perform the main mathematical operations on Octave.
Addition
The addition operator is +
>> 3+2
ans = 5
Subtraction
The subtraction operator is -
>> 3-2
ans = 1
Multiplication
Multiplication multiplication operator is
>> 4*2
ans = 8c
Division
The division operator is /
>> 8/2
ans = 4
Remainder of the Division
To calculate the remainder of the division use the mod() function
>> mod(7,2)
ans = 1
Warning. Unlike many programming languages, the Octave software does not use the percent symbol % to calculate the remainder of division, because the % symbol is used to write comments. So if you write 7%2 the software treats 2 as a comment and returns 7 as the result. Therefore, if you need to calculate the modulus of division you must use the mod() function.
Power
The power operator is ^
>> 2^3
ans = 8
Alternatively, you can also use the double asterisk to calculate the exponentiation of a number
>> 2**3
ans = 8
Square root
To calculate the square root you have to use the function sqrt()
>> sqrt(16)
ans = 4
Alternatively you can raise the number by the fraction 1/2
>> 16**(1/2)
ans = 4
In addition to these basic math operators, there are many other Octave functions that allow you to perform many other math operations
- abs(x)
returns the absolute value of x - cos(x)
calculate the cosine of x - sin(x)
calculate the sine of x - tan(x)
calculate the tangent of x - acos(x)
calculates the arc cosine of x - asin(x)
calculate the arcsine of x - atan(x)
compute the arc tangent x - exp(x)
calculate the exponential of x - log(x)
calculate the natural logarithm of - log10(x)
calculate the base 10 logarithm of x - sqrt(x)
calculate the square root of x - nthroot(x,n)
calculate the nth real root of x
For example to calculate the absolute value you can use the abs() function
>> abs(-1)
ans = 1
In general, Octave results are displayed in short format with 5 significant digits by default.
However, you can change the number format (long/short) or use scientific notation.