Wednesday, April 12, 2017

Approximating y = sin x (0 to 90 degrees, 0 to pi/2 radians)



Approximating y = sin x (0 to 90 degrees, 0 to pi/2 radians)


Calculation

I used the HP Prime to fit polynomials to data by using the vandermonde and LSQ commands.

vandermonde(vector):  creates the matrix consisting of rows [ 1, (n_i), (n_i)^2, (n_i)^3, …, (n_i)^(n-1) ] where the vector has n elements for each i.  Available from the Math-Matrix-Create submenu.

LSQ(X, y):  returns the coefficients [ [a_0], [a_1], [a_2], … , [a_n] ], the minimum norm least squares vector from the system X*a=y.  In this case, the vector a can also be estimated by the operation (X^T X)^-1 X^T y.  Available from the Math-Matrix-Factorize submenu.

Case 1:  Four Points of Data

Fit cubic polynomial to the following data.  Remember we are working with the range of 0° ≤ x ≤ 90° or 0 ≤ x ≤ π/2 radians

X
sin(x)
0
30°
1/2 = 0.5
60°
√3/2 ≈ 0.866025403785
90°
1

Example 1:  Full precision approximation

y = 1.78098409219E-2 x – 1.99435471445E-5 x^2 – 6.05408712068E-7 x^3

Note:  E-n stands for 10^(-n).

Absolute maximum error, data measured in 5° tick marks:  0.002371558429
Absolute maximum error, data measured in 1° tick marks:  0.002392465593



Example 2:  If we round all coefficients to six digits, we get:

y = 0.017810 x – 0.000020 x^2 – 0.000001 x^3

Absolute maximum error, data measured in 5° tick marks:  0.2881 

In comparison between Example 1 and Example 2, keeping the decimal places makes a big difference. 



Example 3:  Let’s see if we do better with using radians instead of degrees.

X
sin(x)
0
0
π/6
1/2 = 0.5
π/3
√3/2 ≈ 0.866025403785
π/2
1

y = 1.02042871863 x – 0.065470803224 x^2 – 0.113871899065 x^3

Maximum absolute error, data measured in π/36 radian tick marks: 0.002371558428
Maximum absolute error, data measured in π/180 radian tick marks: 0.002392465661

The maximum error in Example 3 matched Example 1.

Example 4:  Round the coefficients to six digits:

y = 1.020429 x – 0.065471 x^2 – 0.113872 x^3

Maximum absolute error, data measured in π/36 radian tick marks:  0.002371292924
(2 decimal places)

The difference between Example 2 and Example 4 is that less information is “lost” when rounding the coefficients to a set amount of digits, in this case, 6.

Case 2:  Seven Points of Data

x (degrees)
x (radians)
sin(x)
0
0
15°
π/12
(√6 - √2)/4 ≈ 0.258819045102
30°
π/6
1/2 = 0.5
45°
π/4
√2/2 ≈ 0.707106781185
60°
π/3
√3/2 ≈ 0.866025403785
75°
5π/12
(√6 + √2)/4 ≈ 0.965925826288
90°
π/2
1

Example 5:  Full Precision – Degrees

y = 1.74539026131E-2 x – 1.00636910048E-7 x^2 – 8.79821584089E-7 x^3 – 1.93973162632E-10 x^4 + 1.66950110845E-11 x^5 – 2.72879457224E-14 x^6

Absolute maximum error, data measured in 5° tick marks:  1.2072093E-6
(pretty darn good)

Example 6:  Full Precision – Radians

y = -6.98792887569E-12 + 1.0000349642 x – 3.30435720904E-4 x^2 – 0.165486304503 x^3 – 2.09062241321E-3 x^4 + 1.03087220879E-2 x^5  - 9.65423458482E-4 x^6

Maximum absolute error, data measured in π/36 radian tick marks:  0.00049429473
(3 decimal places)

I think the degrees approximation wins.  But what if we round the radians version (Example 6) to 6 decimal places?

Example 7:  6 Decimal Places – Radians

y = 1.000035 x – 0.00033 x^2 – 0.165486 x^3 – 0.002091 x^4 + 0.010309 x^5 – 0.000965 x^6

Maximum absolute error, data measured in π/36 radian tick marks:  9.03153E-6

Interesting result here, and a nice one at that!



Eddie

This blog is property of Edward Shore, 2017