Next: , Previous: , Up: Arithmetic   [Contents][Index]


17.3 Trigonometry

Octave provides the following trigonometric functions where angles are specified in radians. To convert from degrees to radians multiply by pi/180 or use the deg2rad function. For example, sin (30 * pi/180) returns the sine of 30 degrees. As an alternative, Octave provides a number of trigonometric functions which work directly on an argument specified in degrees. These functions are named after the base trigonometric function with a ‘d’ suffix. As an example, sin expects an angle in radians while sind expects an angle in degrees.

Octave uses the C library trigonometric functions. It is expected that these functions are defined by the ISO/IEC 9899 Standard. This Standard is available at: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf. Section F.9.1 deals with the trigonometric functions. The behavior of most of the functions is relatively straightforward. However, there are some exceptions to the standard behavior. Many of the exceptions involve the behavior for -0. The most complex case is atan2. Octave exactly implements the behavior given in the Standard. Including atan2(+- 0, 0) returns +- pi.

It should be noted that MATLAB uses different definitions which apparently do not distinguish -0.

: rad = deg2rad (deg)

Convert degrees to radians.

The input deg must be a scalar, vector, or N-dimensional array of double or single floating point values. deg may be complex in which case the real and imaginary components are converted separately.

The output rad is the same size and shape as deg with degrees converted to radians using the conversion constant pi/180.

Example:

deg2rad ([0, 90, 180, 270, 360])
  ⇒  0.00000   1.57080   3.14159   4.71239   6.28319

See also: rad2deg.

: deg = rad2deg (rad)

Convert radians to degrees.

The input rad must be a scalar, vector, or N-dimensional array of double or single floating point values. rad may be complex in which case the real and imaginary components are converted separately.

The output deg is the same size and shape as rad with radians converted to degrees using the conversion constant 180/pi.

Example:

rad2deg ([0, pi/2, pi, 3/2*pi, 2*pi])
  ⇒  0    90   180   270   360

See also: deg2rad.

: sin (x)

Compute the sine for each element of x in radians.

See also: asin, sind, sinh.

: cos (x)

Compute the cosine for each element of x in radians.

See also: acos, cosd, cosh.

: tan (z)

Compute the tangent for each element of x in radians.

See also: atan, tand, tanh.

: sec (x)

Compute the secant for each element of x in radians.

See also: asec, secd, sech.

: csc (x)

Compute the cosecant for each element of x in radians.

See also: acsc, cscd, csch.

: cot (x)

Compute the cotangent for each element of x in radians.

See also: acot, cotd, coth.

: asin (x)

Compute the inverse sine in radians for each element of x.

See also: sin, asind.

: acos (x)

Compute the inverse cosine in radians for each element of x.

See also: cos, acosd.

: atan (x)

Compute the inverse tangent in radians for each element of x.

See also: tan, atand.

: asec (x)

Compute the inverse secant in radians for each element of x.

See also: sec, asecd.

: acsc (x)

Compute the inverse cosecant in radians for each element of x.

See also: csc, acscd.

: acot (x)

Compute the inverse cotangent in radians for each element of x.

See also: cot, acotd.

: sinh (x)

Compute the hyperbolic sine for each element of x.

See also: asinh, cosh, tanh.

: cosh (x)

Compute the hyperbolic cosine for each element of x.

See also: acosh, sinh, tanh.

: tanh (x)

Compute hyperbolic tangent for each element of x.

See also: atanh, sinh, cosh.

: sech (x)

Compute the hyperbolic secant of each element of x.

See also: asech.

: csch (x)

Compute the hyperbolic cosecant of each element of x.

See also: acsch.

: coth (x)

Compute the hyperbolic cotangent of each element of x.

See also: acoth.

: asinh (x)

Compute the inverse hyperbolic sine for each element of x.

See also: sinh.

: acosh (x)

Compute the inverse hyperbolic cosine for each element of x.

See also: cosh.

: atanh (x)

Compute the inverse hyperbolic tangent for each element of x.

See also: tanh.

: asech (x)

Compute the inverse hyperbolic secant of each element of x.

See also: sech.

: acsch (x)

Compute the inverse hyperbolic cosecant of each element of x.

See also: csch.

: acoth (x)

Compute the inverse hyperbolic cotangent of each element of x.

See also: coth.

: atan2 (y, x)

Compute atan (y / x) for corresponding elements of y and x.

y and x must match in size and orientation. The signs of elements of y and x are used to determine the quadrants of each resulting value.

This function is equivalent to arg (complex (x, y)).

See also: tan, tand, tanh, atanh.

Octave provides the following trigonometric functions where angles are specified in degrees. These functions produce true zeros at the appropriate intervals rather than the small round-off error that occurs when using radians. For example:

cosd (90)
     ⇒ 0
cos (pi/2)
     ⇒ 6.1230e-17
: sind (x)

Compute the sine for each element of x in degrees.

The function is more accurate than sin for large values of x and for multiples of 180 degrees (x/180 is an integer) where sind returns 0 rather than a small value on the order of eps.

See also: asind, sin.

: cosd (x)

Compute the cosine for each element of x in degrees.

The function is more accurate than cos for large values of x and for multiples of 90 degrees (x = 90 + 180*n with n an integer) where cosd returns 0 rather than a small value on the order of eps.

See also: acosd, cos.

: tand (x)

Compute the tangent for each element of x in degrees.

Returns zero for elements where x/180 is an integer and Inf for elements where (x-90)/180 is an integer.

See also: atand, tan.

: secd (x)

Compute the secant for each element of x in degrees.

See also: asecd, sec.

: cscd (x)

Compute the cosecant for each element of x in degrees.

See also: acscd, csc.

: cotd (x)

Compute the cotangent for each element of x in degrees.

See also: acotd, cot.

: asind (x)

Compute the inverse sine in degrees for each element of x.

See also: sind, asin.

: acosd (x)

Compute the inverse cosine in degrees for each element of x.

See also: cosd, acos.

: atand (x)

Compute the inverse tangent in degrees for each element of x.

See also: tand, atan.

: atan2d (y, x)

Compute atan (y / x) in degrees for corresponding elements from y and x.

See also: tand, atan2.

: asecd (x)

Compute the inverse secant in degrees for each element of x.

See also: secd, asec.

: acscd (x)

Compute the inverse cosecant in degrees for each element of x.

See also: cscd, acsc.

: acotd (x)

Compute the inverse cotangent in degrees for each element of x.

See also: cotd, acot.

Finally, there are two trigonometric functions that calculate special arguments with increased accuracy.

: y = sinpi (x)

Compute sine (x * pi) for each element of x accurately.

The ordinary sin function uses IEEE floating point numbers and may produce results that are very close (within a few eps) of the correct value, but which are not exact. The sinpi function is more accurate and returns 0 exactly for integer values of x and +1/-1 for half-integer values (e.g., …, -3/2, -1/2, 1/2, 3/2, …).

Example
comparison of sin and sinpi for integer values of x

sin ([0, 1, 2, 3] * pi)
⇒
     0   1.2246e-16  -2.4493e-16   3.6739e-16

sinpi ([0, 1, 2, 3])
⇒
       0   0   0   0

See also: cospi, sin.

: y = cospi (x)

Compute cosine (x * pi) for each element of x accurately.

The ordinary cos function uses IEEE floating point numbers and may produce results that are very close (within a few eps) of the correct value, but which are not exact. The cospi function is more accurate and returns 0 exactly for half-integer values of x (e.g., …, -3/2, -1/2, 1/2, 3/2, …), and +1/-1 for integer values.

Example
comparison of cos and cospi for half-integer values of x

cos ([-3/2, -1/2, 1/2, 3/2] * pi)
⇒
     -1.8370e-16   6.1232e-17   6.1232e-17  -1.8370e-16

cospi ([-3/2, -1/2, 1/2, 3/2])
⇒
       0   0   0   0

See also: sinpi, cos.


Next: Sums and Products, Previous: Complex Arithmetic, Up: Arithmetic   [Contents][Index]