Cos approximation and error threshold

8 views (last 30 days)
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.

Accepted Answer

John D'Errico
John D'Errico on 18 Apr 2015
Ok, you've made a quite credible effort on this. (Sad how too many students cannot bother in the least. So my congratulations are due.)
It looks like your computation of the value of n that yields a term under the threshold seems right. Nicely done there in a loop, going from term to term by multiplying by pi/n, since you are given that x does not exceed pi. The value of n here is appropriate for the exponential series, and since the cosine series essentially uses the even order terms from the exponential one, this could work, IF you were careful in the last step.
It is in the last step that you had a problem. Actually, several of them.
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))
You use the colon operator to compute x. See that this does NOT end up at pi for the last term, but a little short.
x = -pi:0.01:pi;
x(end)
ans =
3.13840734641021
Better is to use linspace. Since there were 629 steps in the colon call, I'll use the same number of elements.
x = linspace(-pi,pi,629);
x(end)
ans =
3.14159265358979
The zero'th order term in the cosine series is 1. Instead, you set cos_approx to x. (That would be correct for the linear term in the sine series.)
Then at the end, you subtract sin(x) from the cosine approximation. So I think you have some code for a sine approximation and the cosine approximation confused.
I also see that you did not use a loop that computes each term in the series as you did before.
Finally, you had one more problem. You used the value of n that would work for an exponential series. But the cosine series uses only every OTHER term in that series.
The simple solution is to aggregate the pair of loops into ONE loop. I'll set it such that it computes each term from the previous one, AND does the test to determine when to stop in this same loop.
x = linspace(-pi,pi,629);
cos_term = ones(size(x));
cos_approx = cos_term; %This equates to the summation formula when n = 0
xsquared = x.^2; % precompute x^2 outside the loop
n = 2;
while abs(cos_term(end)) > Threshold
% flipping the sign of each term gets the powers of -1 in there
cos_term = - cos_term.*xsquared./(n*(n-1));
cos_approx = cos_approx + cos_term;
n = n + 2;
end
trunc_error = abs(cos_approx - cos(x));
So the above loop is similar to your two loops.
The cos_term that I compared to the threshold at each step was JUST larger then that threshold for the next to last step.
ans =
1.37686472803774e-12
ans =
-2.09063233531477e-14
Better code might had allowed the loop to terminate sooner for each value of x independently, based on the specific term for that value.
  3 Comments
Luffy Tan
Luffy Tan on 18 Apr 2015
Edited: Luffy Tan on 18 Apr 2015
@John D'Errico ,So just to double confirm with you again, by fixing all the errors I did to what you explained , if the value I input is 0.5 , I will get a U shape graph , but the U is inverted to horizontal ? Is this suppose to be the right graph ?
John D'Errico
John D'Errico on 18 Apr 2015
Wait, are you asking if the THRESHHOLD is 0.5? Why would you possibly use a threshold that obscenely large? You know that cos(x) is limited to lie in the interval [-1,1]. So if you start dropping all terms that are smaller than 0.5, then you won't keep anything beyond the second term in the approximation. But you are also throwing away many terms that will be quite important in that approximation.
So think about it. You will be approximating cos(x) with the polynomial
1 - x^2/2
What is the shape of that function? Do I hear any votes for an inverted U shape (actually a parabola)? If you are not sure, try plotting it:
ezplot(@(x) 1-x.^2,[-pi,pi])
When you see something strange like that, think about what you are doing. Think about what is the meaning of that "Threshold" parameter.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!