Wednesday, February 8, 2012

2.4

The cosine function can be evaluated by the following infinite series : cos(x) = 1 - (x^2)/2!+(x^4)/4!-(x^6)/6!+ (x^8)/8! ... Write an algorithm to implement this formula.


N=50;
x=pi;  % Assumes x = pi for ease of verification

terms = [0:2:N];

facts = factorial(terms);
Power_Of_X = x.^terms;
signs = (-1).^(terms/2);
quotes = Power_Of_X ./facts;

series = signs .* quotes;
M_cos_N = cumsum(series);
plot (M_cos_N);




The problem then asks for the percent relative error or the algorithm. This can be done by updating the above code to include on the end:

%Percent Error
true = -1.0;
P_error = (true - M_cos_N)/true * 100;

figure(2);

plot(terms,(abs(P_error)));

This produces the graph:



Showing that the error inherent in the algorithm becomes largely negligible around 30 terms in

No comments:

Post a Comment