function sinetrunc(x,n) % for n terms in the series, print out the truncation error for a given % value of x exact = sin(x); approx = 0; for j=1:n approx = approx + (-1)^(j+1)*(1/factorial(2*j-1))*(x.^(2*j-1)); end format long %disp(['Exact: ' num2str(exact) ' Approx: ' num2str(approx) ' Trunc: ' num2str(exact-approx)]); str = sprintf('Exact=%0.15G Approx=%0.15G Trunc=%0.15G',exact,approx,exact-approx); disp(str)