Cos approximation and error threshold
Show older comments
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 Comment
Image Analyst
on 18 Apr 2015
Try these useful short tutorials:
Accepted Answer
More Answers (0)
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!