# Computing $\sin(\pi/2)$ numerically

I'm trying to understand the types of numerical errors, to do this I want to calculate $\sin(\pi/2)=1$ numerically. To do this I use the Taylor series of $\sin(x)$ in 0: $$\sin(x)=x-\frac{x^3}{6}+\frac{x^5}{120}-\frac{x^7}{5040}+ \ldots$$

To make the calcultaions I did the following Fortran program:

program main

implicit none
real*8 pi, x, res, error

pi=3.14159265
x = pi/2
res = x - x**3/6 + x**5/120 - x**7/5040

error = 1-res
print*, "Result: ",res
print*, "Error: ", error

end


And I get the following results depending of the degreee of the Taylor series and in the decimals of pi.

If $\pi=3.14$

• Taylor 3rd Degree: 0.92501782114084341
• Taylor 5th Degree: 1.0045086615502121
• Taylor 7th Degree: 0.99984349522599403
• Taylor 9th Degree: 1.0000032059098956
• Taylor 11thDegree: 0.99999962708361323

If $\pi=3.14159265$

• Taylor 3rd Degree: 0.92483221907327295
• Taylor 5th Degree: 1.0045248564076874
• Taylor 7th Degree: 0.99984310136039689
• Taylor 9th Degree: 1.0000035425853664
• Taylor 11thDegree: 0.99999994374102952

And I observe that the error gets bigger if I put more decimals in my $\pi$ approximation. Why is this happening? Edit: Adding more terms to the Taylor expansion (11th degree) makes adding more $\pi$ digits better.

Also, How can I make a better approximation? What method will be better to a problem like this one?

How can I predict the size of these errors?

• What happens if you start with an approximation to $\pi$ that is larger than the actual value, such as $3.15$? – Brian Borchers Sep 1 '13 at 1:18
• @Brian If I start with an approximation of 3.15 and a 3rd degree polynomial, the answer is: 0.92383592604100528 which is worse than with the 3.14 approximation. – Msegade Sep 1 '13 at 10:45
• Taylor series converge very slowly. What happens if you go to higher orders? – Guido Kanschat Sep 1 '13 at 15:57
• Since this is most likely a homework exercise, I'll simply suggest that you consider two different sources of error in this computation: 1. you've truncated the Taylor series and 2. you've approximated $\pi$. For each of these errors you'll need to find a way to bound the error and then compare the sizes of the two errors (hint: use Taylor's theorem with a remainder term.) – Brian Borchers Sep 1 '13 at 16:10
• @Guido When I added the 11th degree I get better results with the longer $\pi$ approximation, so that is interesting. – Msegade Sep 1 '13 at 18:01

The first is that you are looking at finite approximating Taylor polynomials $p_3(x), p_5(x), p_7(x)$ evaluated at $x=\pi/2$. Taylor's theorem says that for analytic functions (which the sine is), $p_N(\pi/2)\rightarrow 1$ as $N\to \infty$. Your results appear to confirm this, at least for the small set of examples you consider.
On the other hand, you are not considering $p_N(\pi/2)$ but $p_N(x)$ for some $x\approx \pi/2$. Now, you expect that $|p_N(x)-1|$ becomes smaller as $x\to \pi/2$, but there is really no reason why that should be so. All you know is that $p_N(x)\to p_N(\pi/2)$ as $x\to \pi/2$. So what to you looks like the error getting larger is because you think that the error is $|p_N(x)-1|$, but that's not correct. The error is in fact $|p_N(x)-p_N(\pi/2)|$ for which you have not shown results.
• That clarifies a lot. Thank you! But how I estimate the error in $\|p_N(x)-p_N(\pi/2)\|$ depending on mi $\pi$ approximation? – Msegade Sep 1 '13 at 18:58
• @Msegade: You can estimate the error by using that $|p_N(x)-p_N(y)|\approx |p'_N(x)|\;|x-y|$ whenever $x$ and $y$ are close together. If you can evaluate $p_N(x)$, then you can also evaluate $p'_N(x)$. – Wolfgang Bangerth Sep 2 '13 at 3:40