function s = nTermSine1(x,n) % nTermSine1 Evaluate the n-term series approximation to sin(x) % Simplest approach: evaluate each term from scratch % % Synopsis: s = nTermSine1(x,n) % % Input: x = argument of sine(x) % n = number of terms in the series % % Output: s = approximation to sin(x) with n terms of the series term = x; s = term; % Initialize the sum and the sign of the term sgn = 1; fprintf('\n i sign k term s\n'); fprintf(' %4d %4d %4d %18.13f %8.5f\n',1,sgn,1,term,s); for i=2:n sgn = -sgn; % switch sign of term k = 2*i - 1; term = sgn*(x^k)/factorial(k); s = s + term; fprintf(' %4d %4d %4d %18.13f %8.5f\n',i,sgn,k,term,s) end