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

I also need help with graphing the result of the function.. Please note: This as

ID: 2260835 • Letter: I

Question

I also need help with graphing the result of the function..

Please note: This assignment is to be completed using MATLAB, and your final results inculding the corresponding M-files should be submitted as an email attachment in pdf format. The cosine function can be evaluated by the following infinite series cos(z)-00 (-1)" Construct an M-file to implement this formula so that it computes and displays the values of cos(x) as each termin the series is added. in other words compute and display in sequence the values for cos(x) = 1 cos(x) = 1- cos(x) = 1-- 2! + up to the order term of your choosing. For each of the preceding, compute lay the percent relative error as % error = true-series approcination X 100% true As a test case, employ the program to compute cos(0.27) up to and including the term: 16 16! In addition, given that this is an alternating series, can you find an upper bound for the truncation error resulting from calculating up to and including the n-th term of the series? Explore your bound with the previous calculations, and include superposed graphs of the corresponding polynomials as well as the original function.

Explanation / Answer

clc
clear all

% input parameters %

no_of_term=20;
x=0.27;

% cosine serise

factorial=1;
cosine_value=1;

for n=1:no_of_term-1
%calculate factorial
factorial_value=factorial*(2*n)*((2*n)-1);
factorial=factorial_value

%calculate power of x
coefficient=(x^(2*n))/factorial;
if(mod(n,2)==1) %select sign of the coefficient
coefficient=coefficient*(-1);
end
cosine1=cosine_value+coefficient; % add all the term
cosine_value=cosine1;
  

end
cosine1 % show the cosine value

error=(1-(cosine1/cos(x)))*100 % error percentage

This code will calculate cosine value at particular x with specific number of terms. Also the relative error.

If you want to plot the polynomial with particular number of terms, then use a for loop to calculate cosine value at different x and store them in an array and finally plot them.

Here is the code:

clc
clear all

% input parameters %

no_of_term=5;
i=1; %counter
for x=0:0.1:5;

% cosine serise

factorial=1;
cosine_value=1;

for n=1:no_of_term-1
%calculate factorial
factorial_value=factorial*(2*n)*((2*n)-1);
factorial=factorial_value

%calculate power of x
coefficient=(x^(2*n))/factorial;
if(mod(n,2)==1) %select sign of the coefficient
coefficient=coefficient*(-1);
end
cosine1=cosine_value+coefficient; % add all the term
cosine_value=cosine1;
  

end
cosine_serise(i)=cosine1; % array to stor csine value for different x%
cosine_true(i)=cos(x);
x_value(i)=x;
error(i)=(1-(cosine_serise(i)/cosine_true(i)))*100 % error percentage

i=i+1;
end
plot(x_value,cosine_serise,'r')
hold on
plot(x_value,cosine_true,'b')
figure
plot(x_value,error,'g')

plot command is for plotting the graph.

See by changing the parameter 'no_of_term' how error is changing.