function r=taylor_sin (n);

%taylor_sin (n)-This function takes an input n that corresponds to the
%number of terms used in approximating Sin(x) with its Taylor series. The
%input must be a positive number

x_values=0:.1:2*pi;   %Use 63 values for x on the interval from 0 to 2pi

for i=1:length(x_values)        %loop through x values
    for j=1:n                   %loop through terms in expansion
     x(j)=[(-1)^(j-1)*(x_values(i))^(2*j-1)]/factorial(2*j-1);    
end;                            %vector x contains terms in expansion x(0),x(1)..x(n)
taylor_sin(i)=sum(x);           %sum all elements together to get Sn(x_value(i))
end;

%length(taylor_sin);            %check to see if dimensions match
%length(sin(x_values));

A=abs(sin(x_values)-taylor_sin);    %Create vector whose components are difference 

figure; cla;                                %clear plotting area, label axes, etc. for Sin(x) and Sn(x)
subplot(2,1,1);
plot(x_values,taylor_sin,'r');hold on
plot(x_values,sin(x_values),'b')
axis([0 2*pi -2 2]);
xlabel('x');ylabel('Sin(x) and S_n n(x)');
text(0.25,-1.6,'Sin(x)','color','b');text(0.25,-1,'S_n(x)','color','r');
title('Sin(x) and its Approximation S_n(x)');
text(1,-1, 'for n=','color','black'); text(1.5,-1, num2str(n),'color','black');

subplot(2,1,2);                     %Plot difference Sin(x)-Sn(x)
cla; plot(x_values,A)
xlabel('x');ylabel('abs(Sin(x)-S_n(x))');
title('Error in Approximation');

