Common way is using a \"power series\" approximation of a function. For cos(x),
ID: 3802675 • Letter: C
Question
Common way is using a "power series" approximation of a function. For cos(x), that approximation is given in simplified form as an infinite series: cos x = sigma^n _i=0(-l)^i x^2i/(2i)! If you expand the series a few terms, it looks like this: cos x = x^0/0! - x^2/2! + x^4/4! - x^6/6! + Write a MATLAB function, Cosine, to calculate the cosine of an angle given the angle in radians and the number of terms to expand the series. You may call a MATLAB function from your function to evaluate the factorial term in the formula. You should test your results with a combination of some "hand" calculations (using a single m-file) and comparison with the values returned by the intrinsic MATLAB COS() function. You should also check the input to make sure it is valid before your function performs calculations. Some limits on those inputs might be determined through experimentation with your function. You must explicitly declare variable types. How many terms are required to give an answer for cos(pi/4) valid to 6 significant figures? Submit your function file and a single m-file including sufficient examples indicating your function works. Give 2 examples of verification of results using "hand" calculations.Explanation / Answer
Matlab function Cosine.m
function y = Cosine(x,n)
% input: x = angle in radians
% n = number of terms to expand the series
% output y = the result of cos(x) approximation using power series
y = 0; % initially declear the variable y
for i =0:n-1 % 0 to n-1 is n terms
y = y + (-1).^i*(x.^(2*i))/factorial(2*i); % Evaluating the power series
end
end
matlab m-file to test the function
x = 0;n=5;
y = Cosine(x,n);
HandCal = 1;
fprintf('The function Cosine(x,n) give an output y = %f for x = %f and n = %f ',y,x,n);
fprintf('Using hand calculation obtained y = %f for x = %f ',HandCal,x);
x = pi/3;n = 5;
y = Cosine(x,n);
HandCal = 0.5;
fprintf('The function Cosine(x,n) give an output y = %f for x = %f and n = %f ',y,x,n);
fprintf('Using hand calculation obtained y = %f for x = %f ',HandCal,x);
x = -pi;n = 5;
y = Cosine(x,n);
HandCal = -1;
fprintf('The function Cosine(x,n) give an output y = %f for x = %f and n = %f ',y,x,n);
fprintf('Using hand calculation obtained y = %f for x = %f ',HandCal,x);
x = 0;n=5;
y = Cosine(x,n);
MatlabCos = cos(x);
fprintf('The function Cosine(x,n) give an output y = %f for x = %f and n = %f ',y,x,n);
fprintf('Using Matlab cos() function obtained y = %f for x = %f ',MatlabCos,x);
x = pi/3;n = 5;
y = Cosine(x,n);
MatlabCos = cos(x);
fprintf('The function Cosine(x,n) give an output y = %f for x = %f and n = %f ',y,x,n);
fprintf('Using Matlab cos() function obtained y = %f for x = %f ',MatlabCos,x);
x = -pi;n = 5;
y = Cosine(x,n);
MatlabCos = cos(x);
fprintf('The function Cosine(x,n) give an output y = %f for x = %f and n = %f ',y,x,n);
fprintf('Using Matlab cos() function obtained y = %f for x = %f ',MatlabCos,x);
x = pi/4;n = 1;
while abs(Cosine(x,n)-cos(x)) > 0.0000001
n = n+1;
end
fprintf('The function Cosine(x,n) give an output y = %0.8f for x = %f and n = %f is valid to 6 significant figures ',Cosine(x,n),x,n);
OUTPUT
>> cosineFunTest
The function Cosine(x,n) give an output y = 1.000000 for x = 0.000000 and n = 5.000000
Using hand calculation obtained y = 1.000000 for x = 0.000000
The function Cosine(x,n) give an output y = 0.500000 for x = 1.047198 and n = 5.000000
Using hand calculation obtained y = 0.500000 for x = 1.047198
The function Cosine(x,n) give an output y = -0.976022 for x = -3.141593 and n = 5.000000
Using hand calculation obtained y = -1.000000 for x = -3.141593
The function Cosine(x,n) give an output y = 1.000000 for x = 0.000000 and n = 5.000000
Using Matlab cos() function obtained y = 1.000000 for x = 0.000000
The function Cosine(x,n) give an output y = 0.500000 for x = 1.047198 and n = 5.000000
Using Matlab cos() function obtained y = 0.500000 for x = 1.047198
The function Cosine(x,n) give an output y = -0.976022 for x = -3.141593 and n = 5.000000
Using Matlab cos() function obtained y = -1.000000 for x = -3.141593
The function Cosine(x,n) give an output y = 0.70710681 for
x = 0.785398 and n = 5.000000 is valid to 6 significant figures
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.