cospi - Compute cos(X*pi) accurately - MATLAB (original) (raw)

Main Content

Compute cos(X*pi) accurately

Syntax

Description

Y = cospi([X](#mw%5Fa59f0387-4086-4f3f-bd0a-a735f030e8ce)) computescos(X*pi) without explicitly computing X*pi. This calculation is more accurate than cos(X*pi) because the floating-point value of pi is an approximation of π. In particular:

example

Examples

collapse all

Compare the accuracy of cospi(X) vs. cos(X*pi).

Create a vector of values.

Calculate the cosine of X*pi using the normal cos function.

Y = 1×5

1.0000    0.0000   -1.0000   -0.0000    1.0000

The results contain small numerical errors due to the fact that pi is a floating-point approximation of the true value of π. For instance, Y(2) is not exactly zero even though cos(π2)=0.

Use cospi to calculate the same values. In this case, the results are exact.

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

Data Types: single | double | table | timetable
Complex Number Support: Yes

Extended Capabilities

expand all

Thecospi function fully supports tall arrays. For more information, see Tall Arrays.

The cospi function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced in R2018b

expand all

The cospi function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.