The Maclaurin series expansion for the cosine function up to some order n is giv
ID: 3758966 • Letter: T
Question
The Maclaurin series expansion for the cosine function up to some order n is given by cos(x) = 1 (x ^2 /2!) + (x ^4 /4!) (x ^6/ 6!) + (x ^8 /8!) · · · ,(x ^n /n!) , where x is the angle in radians and n! denotes the factorial of n. So, the cosine approximations for n = 4, n = 6, and n = 10 are as follows: cos(x) = 1 (x ^2/ 2)! + (x^4/4)! cos(x) = 1 (x^2/2!) + (x^4/4!) (x^6/6!) cos(x) = 1 (x^2/2!) + (x^4/4!) (x^6/6!) + (x^8/8!) (x^10/10!). Write a function cosineApprox that takes scalar input arguments x and n, and returns the approximate value of cos(x). So, the function will be invoked as follows: approx = cosineApprox(x, n); Use the built-in MATLAB function factorial when computing the series expansion. Assume that x is in radians.
Explanation / Answer
function result = cosineApprox(xrad, n)
%cos(x) = 1 (x ^2 /2!) + (x ^4 /4!) (x ^6/ 6!) + (x ^8 /8!) · · · ,(x ^n /n!)
cossum = 1;
for i = 1 : n
cossum = cossum + (power(-1, i) * (power(xrad, (2* i)) / factorial(2 * i)))
end
result = cossum
end
disp("Cos (pi/6):")
disp(cosineApprox(pi/6, 4))
disp(cosineApprox(pi/6, 6))
disp(cosineApprox(pi/6, 10))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.