3

I am trying to evaluate $\cos(x)$ at the point $x=3$ with $7$ decimal places to be correct. There is no requirement to be the most efficient but only evaluate at this point.

Currently, I am thinking first write $x=\pi+x'$ where $x'=-0.14159265358979312$ and then use Taylor series $\cos(x)=\sum_{i=1}^n(-1)^n\frac{x^{2n}}{(2n)!}$ to decide the best $n$ and the fact the error bound $\frac{1}{(n+1)!}$ for $\cos(x)$ when $x\in[-1,1]$ to decide $n$. Using wolfram alpha I got $n=11$. Thus I need to use the first $11$ term of Taylor series of $\cos(x)$. Is this seems a reasonable approach?

If I am using some programming languages which don't contain $\pi$ as a constant, should I just define $\pi$ first and use the above method? Is there any other approach to this?

If I want to evaluate $\sin(\cos(x))$ at the point $x=3$, should I use above method to evaluate $\cos(x)$ first and then $\sin(\cos(x))$? Is there any other approach to this?

Asaf Karagila
  • 393,674
Qomo
  • 285

2 Answers2

1

Using the Taylor series around $\pi$ is fine. You could use the half angle formula to evaluate it for a smaller angle, which will converge faster. For this particular $x$ you don't need that many terms-the alternating series theorem says the error is less than the first neglected term and of the same sign. Since $\frac {0.14159^6}{6!} \lt 1.2\cdot 10^{-8}$ you only need to keep the first three terms (up through $x^4$) to get $7$ places.

Since $\cos 3$ is very close to $-1$, for $\sin (\cos (3))$ you will need more terms or the half angle formula.

Ross Millikan
  • 374,822
1

If your programming language has arctan or arccos, you can get $\pi$ as $4*\tan^{-1}(1)$ or $\cos^{-1}{(-1)}$. If not, just enter "pi = 3.14159265358979323". That is all I remember from when I memorized this in high school.

Since $|\cos(x)| \le 1$ for all $x$, so the same sort of estimation for $\sin(z)$ where $|z| \le 1$.

If you are trying to get sin or cos for large arguments, it is good to reduce first by multiples of $2\pi$ and then get the argument as small as possible to prevent using an alternating series with large arguments.

marty cohen
  • 107,799