Computing sin(π/2)sin⁡(π/2) numerically

计算科学 error-estimation approximation
2021-12-12 20:57:20

I'm trying to understand the types of numerical errors, to do this I want to calculate sin(π/2)=1 numerically. To do this I use the Taylor series of sin(x) in 0:

sin(x)=xx36+x5120x75040+

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 π=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 π=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 π approximation. Why is this happening? Edit: Adding more terms to the Taylor expansion (11th degree) makes adding more π 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?

1个回答

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 p3(x),p5(x),p7(x) evaluated at x=π/2. Taylor's theorem says that for analytic functions (which the sine is), pN(π/2)1 as N. Your results appear to confirm this, at least for the small set of examples you consider.

On the other hand, you are not considering pN(π/2) but pN(x) for some xπ/2. Now, you expect that |pN(x)1| becomes smaller as xπ/2, but there is really no reason why that should be so. All you know is that pN(x)pN(π/2) as xπ/2. So what to you looks like the error getting larger is because you think that the error is |pN(x)1|, but that's not correct. The error is in fact |pN(x)pN(π/2)| for which you have not shown results.