# Trigonometric Function

Specified trigonometric function on input

Math Operations

## Description

### Supported Functions

The Trigonometric Function block performs common trigonometric functions. You can select one of the following functions from the Function parameter list.

FunctionDescriptionMathematical ExpressionMATLAB® Equivalent
`sin`

Sine of the input

`sin(u)`

`sin`
`cos`

Cosine of the input

`cos(u)`

`cos`
`tan`

Tangent of the input

`tan(u)`

`tan`
`asin`

Inverse sine of the input

`asin(u)`

`asin`
`acos`

Inverse cosine of the input

`acos(u)`

`acos`
`atan`

Inverse tangent of the input

`atan(u)`

`atan`
`atan2`

Four-quadrant inverse tangent of the input

`atan2(u)`

`atan2`
`sinh`

Hyperbolic sine of the input

`sinh(u)`

`sinh`
`cosh`

Hyperbolic cosine of the input

`cosh(u)`

`cosh`
`tanh`

Hyperbolic tangent of the input

`tanh(u)`

`tanh`
`asinh`

Inverse hyperbolic sine of the input

`asinh(u)`

`asinh`
`acosh`

Inverse hyperbolic cosine of the input

`acosh(u)`

`acosh`
`atanh`

Inverse hyperbolic tangent of the input

`atanh(u)`

`atanh`
`sincos`

Sine of the input; cosine of the input

`cos + jsin`

Complex exponential of the input

The block output is the result of applying the function to one or more inputs in radians. Each function supports:

• Scalar operations

• Element-wise vector and matrix operations

 Note:   Not all compilers support the `asinh`, `acosh`, and `atanh` functions. If you use a compiler that does not support those functions, a warning appears and the generated code fails to link.

### Block Appearance for the atan2 Function

If you select the `atan2` function, the block shows two inputs. The first input is the y-axis or imaginary part of the function argument. The second input is the x-axis or real part of the function argument. (See Port Location After Rotating or Flipping in the Simulink® documentation for a description of the port order for various block orientations.)

### Block Appearance for the sincos Function

If you select the `sincos` function, the block shows two outputs. The first output is the sine of the function argument, and the second output is the cosine of the function argument.

## Definitions

### CORDIC

CORDIC is an acronym for COordinate Rotation DIgital Computer. The Givens rotation-based CORDIC algorithm is one of the most hardware-efficient algorithms available because it requires only iterative shift-add operations (see References). The CORDIC algorithm eliminates the need for explicit multipliers. Using CORDIC, you can calculate various functions, such as sine, cosine, arc sine, arc cosine, arc tangent, and vector magnitude. You can also use this algorithm for divide, square root, hyperbolic, and logarithmic functions.

Increasing the number of CORDIC iterations can produce more accurate results, but doing so also increases the expense of the computation and adds latency.

## Data Type Support

The block accepts input signals of the following data types:

FunctionsInput Data Types
• `sin`

• `cos`

• `sincos`

• `cos + jsin`

• `atan2`

• Floating point

• Fixed point (only when Approximation method is `CORDIC`)

• `tan`

• `asin`

• `acos`

• `atan`

• `sinh`

• `cosh`

• `tanh`

• `asinh`

• `acosh`

• `atanh`

• Floating point

Complex input signals are supported for all functions in this block, except `atan2`.

You can use floating-point input signals when you set Approximation method to `None` or `CORDIC`. However, the block output data type depends on which of these approximation method options you choose.

Input Data TypeApproximation MethodOutput Data Type

Floating point

`None`

Depends on your selection for Output signal type. Options are `auto` (same data type as input), `real`, or `complex`.

Floating point

`CORDIC`

Same as input. Output signal type is not available when you use the CORDIC approximation method to compute the block output.

You can use fixed-point input signals only when Approximation method is set to `CORDIC`. The CORDIC approximation is available for the `sin`, `cos`, `sincos`, ```cos + jsin```, and `atan2` functions. For the `atan2` function, the relationship between input and output data types depends also on whether the fixed-point input is signed or unsigned.

Input Data TypeFunctionOutput Data Type

Fixed point, signed or unsigned

`sin`, `cos`, `sincos`, and `cos + jsin`

`fixdt`(1, `WL`, `WL` – 2) where `WL` is the input word length

This fixed-point type provides the best precision for the CORDIC algorithm.

Fixed point, signed

`atan2`

`fixdt`(1, `WL`, `WL` – 3)

Fixed point, unsigned

`atan2`

`fixdt`(1, `WL`, `WL` – 2)

For CORDIC approximations:

• Input must be real for the `sin`, `cos`, `sincos`, ```cos + jsin```, and `atan2` functions.

• Output is real for the `sin`, `cos`, `sincos`, and `atan2` functions.

• Output is complex for the `cos + jsin` function.

## Invalid Inputs for CORDIC Approximations

If you use the CORDIC approximation method (see Definitions), the block input has some further requirements.

For the `sin`, `cos`, `sincos`, and `cos + jsin` functions:

• When you use signed fixed-point types, the input angle must fall within the range [–2π, 2π) radians.

• When you use unsigned fixed-point types, the input angle must fall within the range [0, 2π) radians.

For the `atan2` function:

• Inputs must be the same size, or at least one value must be a scalar value.

• Both inputs must have the same data type.

• When you use signed fixed-point types, the word length must be `126` or less.

• When you use unsigned fixed-point types, the word length must be `125` or less.

This table summarizes what happens for an invalid input.

Block UsageEffect of Invalid Input
SimulationAn error appears.
Generated codeUndefined behavior occurs. Avoid relying on undefined behavior for generated code or Accelerator modes.
Accelerator modes

## Parameters

Function

Specify the trigonometric function. The name of the function on the block icon changes to match your selection.

Approximation method

Specify the type of approximation for computing output. This parameter appears only when you set Function to `sin`, `cos`, `sincos`, ```cos + jsin```, or `atan2`.

Approximation MethodData Types SupportedWhen to Use This Method
`None` (default)

Floating point

You want to use the default Taylor series algorithm.

`CORDIC`

Floating point and fixed point

You want a fast, approximate calculation.

If you select `CORDIC` and enlarge the block from the default size, the block icon changes:

FunctionBlock Icon
`sin`

`cos`

`sincos`

`cos + jsin`

`atan2`

Number of iterations

Specify the number of iterations to perform the CORDIC algorithm. The default value is 11.

• When the block input uses a floating-point data type, the number of iterations can be a positive integer.

• When the block input is a fixed-point data type, the number of iterations cannot exceed the word length.

For example, if the block input is `fixdt(1,16,15)`, the word length is 16. In this case, the number of iterations cannot exceed 16.

This parameter appears when both of the following conditions hold:

• You set Function to `sin`, `cos`, `sincos`, ```cos + jsin```, or `atan2`.

• You set Approximation method to `CORDIC`.

Output signal type

Specify the output signal type of the Trigonometric Function block as `auto`, `real`, or `complex`.

FunctionInput Signal TypeOutput Signal Type
AutoRealComplex
Any selection for the Function parameterrealrealrealcomplex
complexcomplexerrorcomplex

 Note:   When Function is `atan2`, complex input signals are not supported for simulation or code generation.

Setting Approximation method to `CORDIC` disables this parameter.

Sample time
 Note:   This parameter is not visible in the block dialog box unless it is explicitly set to a value other than `-1`. To learn more, see Blocks for Which Sample Time Is Not Recommended.

## Examples

### sin Function with Floating-Point Input

Suppose that you have the following model:

The key block parameters for the Constant block are:

ParameterSetting
Constant value`1`
Output data type`Inherit: Inherit from 'Constant value'`

The block parameters for the Trigonometric Function block are:

ParameterSetting
Function`sin`
Approximation method`None`
Output signal type`auto`

The output type of the Trigonometric Function block is the same as the input because the input type is floating point and Approximation method is `None`.

### sincos Function with Fixed-Point Input

Suppose that you have the following model:

The key block parameters for the Constant block are:

ParameterSetting
Constant value

`1`

This value must fall within the range [–2π, 2π) because the Trigonometric Function block uses the CORDIC algorithm and the block input uses a signed fixed-point type.

Output data type`fixdt(1,13,5)`

The block parameters for the Trigonometric Function block are:

ParameterSetting
Function`sincos`
Approximation method`CORDIC`
Number of iterations`11`

The output type of the Trigonometric Function block is `fixdt(1,13,11)` because the input type is fixed point and Approximation method is `CORDIC`. The output fraction length equals the input word length – 2.

### Block Behavior for Complex Exponential Output

The following model compares the complex exponential output for the two different approximation methods:

The key block parameters for the Constant blocks are:

BlockParameterSetting
`Constant`Constant value`[-pi/2 0 pi]`
Output data type`fixdt(1,16,2)`
`Constant1`Constant value`[-pi/2 0 pi]`
Output data type`double`

The block parameters for the Trigonometric Function blocks are:

BlockParameterSetting
`Approximation = 'CORDIC'`Function`cos + jsin`
Approximation method`CORDIC`
Number of iterations`11`
`Approximation = 'None'`Function`cos + jsin`
Approximation method`None`

When the Approximation method is `CORDIC`, the input data type can be fixed point, in this case: `fixdt(1,16,2)`. The output data type is `fixdt(1,16,14)` because the output fraction length equals the input word length – 2.

When the Approximation method is `None`, the input data type must be floating point. The output data type is the same as the input.

### Digital Waveform Generation: Sine Wave Approximation

To learn how to generate a digital waveform using a CORDIC sine approximation, see the description for the `sldemo_tonegen_fixpt` model.

## Characteristics

 Data Types Double | Single Sample Time Inherited from driving block Direct Feedthrough Yes Multidimensional Signals Yes Variable-Size Signals Yes Zero-Crossing Detection No Code Generation Yes

## References

[1] Volder, JE. "The CORDIC Trigonometric Computing Technique." IRE Transactions on Electronic Computers. Vol. EC-8, September 1959, pp. 330–334.

[2] Andraka, R. "A survey of CORDIC algorithm for FPGA based computers." Proceedings of the 1998 ACM/SIGDA sixth international symposium on Field programmable gate arrays. Feb. 22–24, 1998, pp. 191–200.

[3] Walther, J.S. "A Unified Algorithm for Elementary Functions." Hewlett-Packard Company, Palo Alto. Spring Joint Computer Conference, 1971, pp. 379–386. (from the collection of the Computer History Museum). www.computer.org/csdl/proceedings/afips/1971/5077/00/50770379.pdf

[4] Schelin, Charles W. "Calculator Function Approximation." The American Mathematical Monthly. Vol. 90, No. 5, May 1983, pp. 317–325.

## See Also

#### Introduced before R2006a

Was this topic helpful?

Download now