Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. (10 points) The Maclaurin series expansion for the cosine function up to some

ID: 3183810 • Letter: 3

Question

3. (10 points) The Maclaurin series expansion for the cosine function up to some number of terms is given by where r is the angle in radians and n! n x (n-1) x (n-2)… x 1 denotes the factorial of n. Note that 0! = 1. For example, the cosine approximations for one, two, and five terms can be obtained as follows: cos(z)-1 cos(z) = 1-21 Write a function approxCosineFunction that takes scalar input arguments x and the number of terms t. and returns the approximate value of cos(z)·The function is invoked as follows: approxcos ineFunction(x, t); . approx Use MATLAB's factorial function to compute the factorial values. Assume that x is in radians. Your code should use MATLABs built-in vector operations to solve this problem. You must not use for and while loops, or MATLAB's cos function. The values returned by MATLAB's cos function, which we will consider the gold standard, for some example values of x are: >2 pi/3; > actual-cos (x) actual 0.5000 >> actualcos (x) actual 0.5000 Since the approxCosineFunction function is only approximating the cosine value, the ap- proximation will get closer to the actual value as we increase the number of terms. Examples of correct function behavior for various values of x and t follow: >> actual cos (x) actua1 - 0.5000 > approxapproxCosineFunction (x, 2) approx -1.1932 >> approx approxcos ineFunction(x, 5) appro

Explanation / Answer

code

close all
clear
clc

x = 2*pi/3;
fprintf('Approx value (t = %d) = %f ', 1, approxCosineFunction(x,1));
fprintf('Approx value (t = %d) = %f ', 2, approxCosineFunction(x,2));
fprintf('Approx value (t = %d) = %f ', 3, approxCosineFunction(x,3));
fprintf('Approx value (t = %d) = %f ', 5, approxCosineFunction(x,5));
fprintf('Actual value = %f ', cos(x));

function y = approxCosineFunction(x, t)
tt = 0:2:2*t-1;
xx = x.^tt;
fac = factorial(tt);
minus = (-1).^(0:1:t-1);
y = xx./fac;
y = sum(y.*minus);
end

output

Approx value (t = 1) = 1.000000
Approx value (t = 2) = -1.193245
Approx value (t = 3) = -0.391525
Approx value (t = 5) = -0.499567
Actual value = -0.500000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote