Question

In: Computer Science

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 calculate it. Also, calculate and print the cos(150) using matlab's built-in function cosd. (Your code and the cosd function should return the same values).

  • Create your script in matlab, name it Lab6.m and attach it in the space below. Your script should have a line of comment with your name and date and a line with a short description about the code.

Solutions

Expert Solution

Code Screenshot :

Executable Code:

result = 1;
angle = 150*(pi/180);
numerator = angle*angle;
denominator = 2;
sign = -1;
degree = 2;
count = 1;
while(numerator/denominator >= 0.0001)
   result = result + sign*(numerator/denominator);
   sign = -sign;
   numerator = numerator * angle * angle;
   denominator = denominator * (degree+1) * (degree + 2);
   degree = degree + 2;
   count = count + 1;
end

fprintf("%.2f is the value of cos(150) by Taylor series and total of %d terms is used\n",result,count);
fprintf("%.2f is the value of cos(150) by cosd in-build function\n",cosd(150));

Sample Output :

Please comment below if you have any queries.


Related Solutions

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).
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
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.
how to write in VBA a Taylor series program for f(x)= (x+2)^(1⁄3) expanded about x =...
how to write in VBA a Taylor series program for f(x)= (x+2)^(1⁄3) expanded about x = 0. Use approximate relative error to check for a stopping point for an error of 0.001 for x = 0.291.
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...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan⁡(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of...
(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.
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a...
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 = 3kHz and f2 = 5kHz and then sample it with fs = 10kHz. Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not? If there is aliasing specify which component is casing the aliasing
1. expand each function in a Taylor Series and determine radius of convergence. a) f(x) =...
1. expand each function in a Taylor Series and determine radius of convergence. a) f(x) = 1/(1-x) at x0 = 0 b) f(x) = e^(-x) at x0 = ln(2) c) f(x) = sqrt(1+x) at x0 = 0
The function f(x)= x^−5 has a Taylor series at a=1 . Find the first 4 nonzero...
The function f(x)= x^−5 has a Taylor series at a=1 . Find the first 4 nonzero terms in the series, that is write down the Taylor polynomial with 4 nonzero terms.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT