function [sol] = approxcos(x,i)
% MEC E 390 Chapter 2
% Example 2.3
% Approximate the function cos(x) with a specified number of terms
% Inputs:
%   x = the angle in radians
%   i = the number of terms to include in the series
%
% Outputs:
%   sol = the approximate value of cos(x)


% initialize the vector of elements
elements = zeros(i,1);
for n = 1:i
    elements(n) = (-1)^(n-1)/(factorial(2*(n-1)))*x^(2*(n-1)); % need n-1 because sum starts from n = 0
end
sol = sum(elements);
%fprintf('%10.6g\n',sol);
