6

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?

Msegade
  • 173
  • 4
  • What happens if you start with an approximation to $\pi$ that is larger than the actual value, such as $3.15$? – Brian Borchers Sep 01 '13 at 01: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 01 '13 at 10:45
  • Taylor series converge very slowly. What happens if you go to higher orders? – Guido Kanschat Sep 01 '13 at 15:57
  • 1
    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 01 '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 01 '13 at 18:01
  • @Brian Not homework, I just want to understand and predict the errors. I will try with the Taylor remainder, but how I can predict the errors of $\pi$? as it appears more decimals is not always better. – Msegade Sep 01 '13 at 18:04
  • What happens when you evaluate the Taylor polynomial with Horner's method? (http://en.wikipedia.org/wiki/Horner%27s_method) – n00b Sep 01 '13 at 21:26

1 Answers1

5

It may look like the error is getting larger, but I think you are confusing multiple issues here.

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.

Wolfgang Bangerth
  • 55,373
  • 59
  • 119
  • 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 01 '13 at 18:58
  • I guess all Mr./Ms. Msegade needs to do here is change the definition of error to error=sin(x)-res; and then this error will be meaningful, it will represent the difference between the exact answer (for whatever x is used) and the series sum. Would be interesting to see these results. At some point we should be able to see that the machine accuracy is finite (maybe at 10-12 significant digits). – Maxim Umansky Sep 01 '13 at 22:19
  • 1
    @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 02 '13 at 03:40