# Approximation of sine and cosine by a polynomial of 2 degrees

on the site habr.com/ru there were already similar publications in the fall of 2021:

How to calculate the sine the fastest https://habr.com/ru/company/ruvds/blog/584460/

How to calculate sine quickly https://habr.com/ru/post/577256/

not on habr How to make a fast function to calculate the sine?https://forum.ixbt.com/topic.cgi?id=26:20413 Topic started in 2003 Last reply in 2020

The purpose of this publication is an attempt to offer a way to calculate the sine and cosine quickly enough for those platforms where these functions are not available (microcontrollers) or where the speed of calculation is more important than accuracy.

It seems to me that the convex functions sine and cosine can be approximated not only by a Taylor series (by a polynomial of the form a*x + b*x^3 +c*x^5 … ), but by an ordinary parabola through the points 1,2,3 as on the image.

Points 1 and 2 are basic, placed with a certain specified step. In a Scilab script, this is a variable step. We will calculate the coordinates of point 3 in such a way that it has the maximum deviation of the sine (or cosine) from the segment 1-2.

Below are the Scilab scripts for calculating the coefficients of a polynomial of the second degree on the interval [ 0 … pi /2] and plots of deviations for the sine and cosine functions.

< spoiler title="Calculation of coefficients for sine on Scilab ">

``````/// синус
function [a,b,c]=  koef_parabola(x1,y1,x2,y2,x3,y3)
/// расчет коэффициентов параболы по 3 точкам
a1= y3 -  ( x3*(y2-y1)+ x2*y1 -x1*y2 )/( x2-x1)
a2=   x3*( x3-x1-x2 ) + x1*x2
a= a1 /a2
b =( y2-y1)/( x2-x1) - a*( x1+x2)
c=( x2*y1 - x1*y2) / (x2-x1)   + a * x1*x2
endfunction

kol_int =12   /// количество интервалов на отрезке 0 ... %pi/ 2
kol_funct =kol_int+1
step =%pi/ ( 2*kol_int)  /// шаг
point0_x =0:step:%pi/ 2
point0_sin = cos(point0_x)
aa_sin =1:1:(kol_int)
bb_sin =1:1:(kol_int)
cc_sin =1:1:(kol_int)
xx1=0
yy1=1
for ii=1:1:kol_int
xx2=xx1+step
yy2=cos(xx2)
wsp1 =(yy2-yy1)/(xx2-xx1)  /// наклон
xx3=asin(-wsp1)   /// координаты центральной точки
yy3= sqrt( 1- wsp1^2)  ///  cos(xx3)
[aa,bb,cc]=koef_parabola( xx1,yy1,xx2,yy2,xx3,yy3)
aa_sin(ii)=aa
bb_sin(ii)=bb
cc_sin(ii)=cc
xx1=xx2
yy1=yy2
end
///вывод шага и коэффициентов
disp( step,aa_sin,bb_sin,cc_sin)
xx1=0
xx2=step
for ii=1:1:kol_int
int01=[xx1:0.005:xx2]
a1=aa_sin(ii)
b1=bb_sin(ii)
c1=cc_sin(ii)
plot(int01, (cos(int01) -  a1*int01^2 -b1*int01-c1 ) )
xx1=xx1+step
xx2=xx2+step
end``````

graph of deviations of the polynomial of the second degree from the sine in the interval 0 … Pi / 2 when divided into 8 intervals (interval size = Pi / 16 approximately = 0.196 )

``````function [a, b, c]=koef_parabola(x1, y1, x2, y2, x3, y3)
/// расчет коэффициентов параболы по 3 точкам
a1= y3 -  ( x3*(y2-y1)+ x2*y1 -x1*y2 )/( x2-x1)
a2=   x3*( x3-x1-x2 ) + x1*x2
a= a1 /a2
b =( y2-y1)/( x2-x1) - a*( x1+x2)
c=( x2*y1 - x1*y2) / (x2-x1)   + a * x1*x2
endfunction
kol_int =12   /// количество интервалов на отрезке 0 ... %pi/ 2
kol_funct =kol_int+1
step =%pi/ ( 2kol_int)  /// шаг
point0_x =0:step:%pi/ 2
point0_sin = cos(point0_x)
aa_sin =1:1:(kol_int)
bb_sin =1:1:(kol_int)
cc_sin =1:1:(kol_int)
xx1=0
yy1=1
for ii=1:1:kol_int
xx2=xx1+step
yy2=cos(xx2)
wsp1 =(yy2-yy1)/(xx2-xx1)  /// наклон
xx3=asin(-wsp1)   /// координаты центральной точки
yy3= sqrt( 1- wsp1^2)  ///  cos(xx3)
[aa,bb,cc]=koef_parabola( xx1,yy1,xx2,yy2,xx3,yy3)
aa_sin(ii)=aa
bb_sin(ii)=bb
cc_sin(ii)=cc
xx1=xx2
yy1=yy2
end
///вывод шага и коэффициентов
disp( step,aa_sin,bb_sin,cc_sin)
xx1=0
xx2=step
for ii=1:1:kol_int
int01=[xx1:0.005:xx2]
a1=aa_sin(ii)
b1=bb_sin(ii)
c1=cc_sin(ii)
plot(int01, (cos(int01) -  a1*int01^2 -b1*int01-c1 ) )
xx1=xx1+step
xx2=xx2+step
end``````

graph of deviations of the polynomial of the second degree from the sine in the interval 0 … Pi / 2 when divided into 12 intervals

(pi step / 16)

I do not provide code in any language or pseudocode, so as not to litter the publication, everyone can choose the coefficients of a polynomial of degree 2 for approximation and write their own function. By argument value x calculate the interval number, remember the coefficients and substitute them into the polynomial formula