Cos approximation and error threshold
Afficher commentaires plus anciens
You have been tasked with developing an M-file that allows other engineers to quickly determine the maximum n needed to reduce the truncation error below an error threshold. The truncation error is the absolute error between the approximation and MATLAB’s cos() function. Your code should do the following: 1) Ask the user for an error threshold value
2) Find the number of terms needed so that the truncation error is always below the user error threshold from x = -π to π
3) Print the number of terms needed, the mean truncation error and the maximum truncation error using fprintf
4) Plot the truncation error against x
The shape of the graph I plotted using the codes I've type below is wrong compare to the right one, I need help since I have no idea how to get to the correct one. Can you point out the error in my codes and show me the complete solution for that particular part .
clear all, clc
Threshold = input('Please enter the error threshold:');
estimate_remainder = 1;
n = 1;
while estimate_remainder >= Threshold
estimate_remainder = estimate_remainder*(pi/n)
n = n+1;
end
clc
%Value of n has been calculated, now a approximation function must be
%formed using this value of n, and then given range for x.
x = -pi:0.01:pi;
cos_approx = x; %This equates to the summation formula when n = 0
for i = 1:n
cos_approx = cos_approx + (((-1)^i) / factorial(2*i)) *(x.^(2*i));
end
trunc_error = abs(cos_approx - sin(x))
plot(trunc_error,x)
Any help would be greatly appreciated, thanks.
1 commentaire
Image Analyst
le 18 Avr 2015
Try these useful short tutorials:
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Calculus dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!