How to create reasonable error graphs in math?

Not sure if what I'm trying to do makes sense. I am prototyping some code in math, one day I hope to write in C ++. An environment in which I can only calculate with double precision (since I use the MS VC ++ compiler).

I have a polynomial that approximates the function f, and I want to build an error curve. The polynomial coefficients were calculated on the accuracy of the machine (as is the case with C ++). But when I draw a relative error, I just get a graph of numerical noise. I would expect the error to be a smooth curve, since the polynomial is a Taylor polynomial. How can I get a more reasonable plot? The code below demonstrates what I'm trying to do, with the Cos function as a test case. The result of the graph is just numerical noise.

f[x_] = Cos[x] a[k_] := N[(-1)^k/(2*k)!] approx[x_] := Sum[a[k]*x^(2*k), {k, 0, 12}] Plot[approx[x]/f[x] - 1, {x, -Pi, Pi}, WorkingPrecision -> 30] 

I could increase the accuracy of calculating the coefficients a[k] .

 a[k_] := N[(-1)^k/(2*k)!,30] 

to get a more reasonable plot, but that defeats the purpose of my prototype code. Because in the end, I want to graphically display the error graphs that I create in C ++. And in this environment, the coefficients will be calculated only in double precision.

I think this test makes sense? I expect my approximation to be accurate regarding the accuracy of the machine, but how can I plot the error?

Thanks for reading.

+4
source share
1 answer

First of all, building approx[x]/Cos[x] - 1 over a range that includes either Pi/2 or -Pi/2 will cause problems only if Cos[x] goes to zero at these points. At these points, approx very close to zero, but not quite so. In addition, Cos[x] is estimated to be algebraically equal to zero before being converted to numeric zero during division, so you see spikes at these points.

Secondly, when building a more reasonable area

 Plot[approx[x]/f[x] - 1, {x, 0, 0.5}, WorkingPrecision -> 30, PlotRange -> All] 

I get

enter image description here

which is exactly what I expect when working near precision machines. Each burst is roughly equivalent to one bit.

Finally, if you want to get a good approximation of the entire domain of interest, I would not use the Taylor extension, which is only good in the vicinity of the extension point. Instead, I would look at the min-max approximation using Chebyshev polynomials . For example, taking the Chebyshev series and the Taylor series of 14 members, we get

enter image description here

where I draw the absolute difference between each series and Cos . As you can see from the plot on the left, the Chebyshev series works much better than the Taylor series on the right.

+6
source

Source: https://habr.com/ru/post/1398937/


All Articles