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....
Write a program in python to calculate the value of cos(?) using its Taylor series expansion:...
Write a program in python to calculate the value of cos(?) using its Taylor series expansion: ?2 ?4 ?6 ?8 cos(?)=1− + − + ... The program should consist of the following functions: a) Develop and test a cosCalc() function that receives as an argument, of type float, the value of the variable and returns the result as a float also. Include 20 terms maximum from the series, or until the value of a term is less than 1e-8. b)...
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.
Write C code to approximate Sin(x) and Cos(x) function using Taylor’s series. The angle x is...
Write C code to approximate Sin(x) and Cos(x) function using Taylor’s series. The angle x is expressed in radians. Use the first FIVE terms of Taylor’s series to approximate Sine/Cosine function. a. Compare your Taylor’s series approximation of Sine/Cosine function with the math.h implementation of sin and cos function. b. Determine tan(x) from Taylor’s series approximation of Sine/Cosine functions, for x = 0, PI/4, PI/2, PI. Use PI = 3.14159.
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 C program to create a series of processes, as shown below in the diagram....
Write a C program to create a series of processes, as shown below in the diagram. P |------ ---- > c1 |------------>c2                              |------- ------>c3 ---------->c4                             In other words, the parent process p creates process c1, c1 creates c2 and c3, and c3 creates c4. A sample run of the program shows Your program should output something similar to what is shown above. You could optionally use wait/waitpid/sleep/exit in your program. Comment your code to show where you created...
Write a C program to create a series of processes, as shown below in the diagram....
Write a C program to create a series of processes, as shown below in the diagram. P - ------------------ > c1 ------------>c3    |                                          |    |------------------> c2 |----- >c4 In other words, the parent process p creates processes c1 and c2, c1 creates c3 and c4.
(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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT