Question

In: Computer Science

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:

  1. 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) Write a main program to invoke the function and calculate and print the values of

2! 4! 6! 8!

?2?
cos (− ), cos(−?), and sin ⁡( ).

Solutions

Expert Solution

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

import numpy as np
def fact(n):
if(n<=1):
return 1;
return n*fact(n-1);
def cosCalc(x):
s=0;
for i in range(20):
t=(-1)**i*(x**(2*i))/fact(2*i);
s=s+t;
if(abs(t)<1e-8):
break;
return s;
def main():
print("cos(pi/4)=",cosCalc(np.pi/4.0));
print("cos(-pi)=",cosCalc(-np.pi));
main();

Kindly revert for any queries

Thanks.


Related Solutions

Write a python program to calculate the value of sin(?) using its Taylor series expansion: sin(?)...
Write a python program to calculate the value of sin(?) using its Taylor series expansion: sin(?) = ? − ? 3 3! + ? 5 5! − ? 7 7! + ? 9 9! … The program should consist of the following functions: a) Develop and test a calcSin() 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 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...
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).
Find Taylor series expansion for sin^2(z)
Find Taylor series expansion for sin^2(z)
Using steps.py in this repository for a starter file, write a Python program to calculate average...
Using steps.py in this repository for a starter file, write a Python program to calculate average number of steps per month for a year. The input file (which you will read from the command line, see the sample program on how to read command line arguments in this repository) contains the number of steps (integer) a person took each day for 1 year, starting January 1. Each line of input contains a single number. Assume this is NOT a leap...
Write a Python program that has the user enter their name.   Using the user's name calculate...
Write a Python program that has the user enter their name.   Using the user's name calculate the following: 1. How many letters are in the user's name? 2. Print the user's name in REVERSE both in capital letters and lowercase letters 3. Print the ASCII value for each letter of the user's name. 4. Print the SUM of all of the letters of the user's name (add each letter from #3)
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.
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 Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
Find Taylor series expansion of log(1+z) and show radius of convergence
Find Taylor series expansion of log(1+z) and show radius of convergence
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT