% Program 3.4 Building a sin calculator key, attempt #2
% Approximates sin curve with degree 9 polynomial
% Input:  x
% Output: approximation for sin(x), correct to 10 decimal places
function y=sin2(x)
% First calculate the interpolating polynomial and 
%   store coefficients
n=10;
b=pi/4+(pi/4)*cos((1:2:2*n-1)*pi/(2*n));
yb=sin(b);                      % b holds Chebyshev base points
c=newtdd(b,yb,n);
%For each input x, move x to the fundamental domain and evaluate
%   the interpolating polynomial
s=1;                            % Correct the sign of sin
x1=mod(x,2*pi);
if x1>pi
  x1 = 2*pi-x1;
  s = -1;
end
if x1 > pi/2
  x1 = pi-x1;
end
y = s*nest(n-1,c,x1,b);
