Compute cos(X*pi) accurately
Calculate Cosine of Multiples of π
Compare the accuracy of
Create a vector of values.
X = [0 1/2 1 3/2 2];
Calculate the cosine of
X*pi using the normal
Y = cos(X*pi)
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 .
ans = 6.1232e-17
cospi to calculate the same values. In this case, the results are exact.
Z = cospi(X)
Z = 1×5 1 0 -1 0 1
ans = 0
X — Input array
scalar | vector | matrix | multidimensional array | table | timetable
Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.
Complex Number Support: Yes
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version HistoryIntroduced in R2018b
R2023a: Perform calculations directly on tables and timetables
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.