Compute cos(X*pi) accurately
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
Input array, specified as a scalar, vector, matrix, or multidimensional array.
Complex Number Support: Yes
This function fully supports tall arrays. For more information, see Tall Arrays.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).