Sins of Numerical Analysis:

Andy Long, Spring, 2024

We use symmetries, periodicities, etc. so that we only need construct sine on the smallest interval necessary:

First let’s just make sure that we’ve got the logic structure correct: so, in the end, we’ll just use the true Sine function, and see if we can handle any value of x:

sinner[x_]:=
If[x<0,-sinner[-x], 
If[x>2Pi,sinner[x-Floor[x/(2Pi)]*2Pi],
If[x>Pi,-sinner[2Pi-x],
If[x>Pi/2,sinner[Pi-x],
Sin[x]
]
]
]
]
Plot[sinner[x],{x,-Pi,3Pi}]

Maclaurin sins:

We wanted an error less than 10^{-6}; if we take the error term to 11, it works (while 10 is not quite enough):

errTerm[n_]:=(Pi/2)^(n+1)/Factorial[n+1]
N[errTerm[11]]
N[errTerm[10]]

So we’ll use the 11 degree Taylor polynomial to fake sine:

T11[x_]=Normal[Series[Sin[x],{x,0,11}]]

And then redefine sinner, incorporating this fake, plot it, and plot its error -- is it less than 10^{-6} everywhere?

sinner[x_]:=
If[x<0,-sinner[-x], 
If[x>2Pi,sinner[x-Floor[x/(2Pi)]*2Pi],
If[x>Pi,-sinner[2Pi-x],
If[x>Pi/2,sinner[Pi-x],
T11[x]
]
]
]
]
Plot[sinner[x],{x,-Pi,3Pi}]
Plot[(sinner[x]-Sin[x]),{x,-Pi,3Pi},PlotRange->All]

What if we centered the Taylor polynomial on Pi/4, as Danny suggested?

errTerm2[n_]:=(Pi/4)^(n+1)/Factorial[n+1]
N[errTerm2[8]]

T8[x_]=Normal[Series[Sin[x],{x,Pi/4,8}]]

sinner2[x_]:=
If[x<0,-sinner2[-x], 
If[x>2Pi,sinner2[x-Floor[x/(2Pi)]*2Pi],
If[x>Pi,-sinner2[2Pi-x],
If[x>Pi/2,sinner2[Pi-x],
T8[x]
]
]
]
]
Plot[sinner2[x],{x,-Pi,3Pi}]
Plot[(sinner2[x]-Sin[x]),{x,-Pi,Pi},PlotRange->All]

Which sins do you prefer? Choose your sin!