Question

In: Computer Science

Recall that the following Taylor series is used to approximate Cosine: cos(x) = ∑ (−1) nx...

Recall that the following Taylor series is used to approximate Cosine: cos(x) = ∑ (−1) nx 2n (2n)! ∞ n=0 You have been tasked with developing an m-file that allows other engineers to quickly determine the minimum 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. You may want to Google the Taylor series to better your understanding of it. Your code should perform the following: 1. Ask the user to enter an error threshold value (scalar value) 2. Determine the number of terms (minimum n) needed so that the maximum truncation error for all values of x = −2π to 2π (increments of 0.01) is below the user error threshold. 3. Use fprintf to print the minimum n value, the mean truncation error and the maximum truncation error across all values of x. 4. Plot the truncation error against x. The following table can be used to check your answers.

Error threshold/ Minimum n value /Number of terms required

0.1 /8 /9

1e-5/ 12/ 13

1e-10 16 17

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Code:

clear;
threshold=input('Please give value of error threshold :');
totalValues=4*pi/0.01;
calculatedValues=zeros(1,round(totalValues));
x=-2*pi:0.01:2*pi;
originalValues=cos(x);
errors=zeros(1,round(totalValues));
maxi=1;
n=0;
while maxi>threshold
calculatedValues=zeros(1,round(totalValues));
for i = 0:n
calculatedValues=calculatedValues+((-1)^i/factorial(2*i))*(x.^(2*i));
end
n=n+1;
errors=abs(originalValues-calculatedValues);
maxi=max(errors);
end
fprintf("Threshold=%f, Value of n= %d, Terms required = %d\n",threshold,n-1,n);
plot(x,errors),xlabel('x'),ylabel('error'),grid;
clear;

Output:


Related Solutions

Compute the Taylor series at x = 0 for ln(1+x) and for x cos x by...
Compute the Taylor series at x = 0 for ln(1+x) and for x cos x by repeatedly differentiating the function. Find the radii of convergence of the associated series.
Write the Taylor series for f(x) = cos x about x = 0.
P1. Write the Taylor series for f(x) = cos x about x = 0. State the Taylor polynomials T2(x), T4(x), and T6(x) (note that T3(x) will be the same as T2(x), and T5(x) will be the same as T4(x)). Plot f(x), T2(x), T4(x), and T6(x), together on one graph, using demos or similar (cut-and-paste or reproduce below).
Prove that (cos x + isin x) n = cos nx + isin nx. (Hint: Use...
Prove that (cos x + isin x) n = cos nx + isin nx. (Hint: Use Euler’s formula).
The cos(x) function can be represented in a Taylor series shown below: Write a Matlab program,...
The cos(x) function can be represented in a Taylor series shown below: Write a Matlab program, and use a while loop, to calculate cos(150) (the input is in degrees) by adding terms of the series and stopping when the absolute value of the term that was added last is smaller than 0.0001. Make sure to make the required degree <-> radian conversions. Use fprintf to print the cos(150) (up to 2 decimal places) and the number of terms used to...
Expand in Fourier series: Expand in fourier sine and fourier cosine series of: f(x) = x(L-x),...
Expand in Fourier series: Expand in fourier sine and fourier cosine series of: f(x) = x(L-x),    0<x<L Expand in fourier cosine series: f(x) = sinx, 0<x<pi Expand in fourier series f(x) = 2pi*x-x^2, 0<x<2pi, assuming that f is periodic of period 2pi, that is, f(x+2pi)=f(x)
Find the Taylor series for f (x) = 2/(1+3x) at x = a where a =...
Find the Taylor series for f (x) = 2/(1+3x) at x = a where a = 0
Apply Newton’s method and the modified method to the equation f(x) = 1−cos(x−5) to approximate the...
Apply Newton’s method and the modified method to the equation f(x) = 1−cos(x−5) to approximate the a double root 5. Compare the results and demonstrate the superiority of the modified method. Numerically identify the rates of convergence of both the methods.
(a) Determine the Taylor Series centered at a = 1 for the function f(x) = ln...
(a) Determine the Taylor Series centered at a = 1 for the function f(x) = ln x. (b) Determine the interval of convergence for this Taylor Series. (c) Determine the number n of terms required to estimate the value of ln(2) to within Epsilon = 0.0001. Can you please help me solve it step by step.
Find the Taylor series or polynomial generated by the following functions a. )f(x) √ x centred...
Find the Taylor series or polynomial generated by the following functions a. )f(x) √ x centred at x=4 , of order 3 b.) f(x) cosh x= e^x+e^-x/(2), centred at x=0 c.) f(x) = x tan^-1x^2 , centred at x=0 d.) f(x) = 1/(√1+x^3) , centred at x=0 , of order 4 e.) f(x) = cos(2x+pie/2) centred at x= pie/4
1. Find Taylor series centered at 1 for f(x) = e^ (x^2). Then determine interval of...
1. Find Taylor series centered at 1 for f(x) = e^ (x^2). Then determine interval of convergence. 2. Find the coeffiecient on x^4 in the Maclaurin Series representation of the function g(x) = 1/ (1-2x)^2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT