Question

In: Computer Science

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 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 sin ( ? 4 ), sin (− ? 2 ), and cos2 ( ? 3 ).

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 calcSin(x):
s=0;
for i in range(20):
t=(-1)**i*(x**(2*i+1))/fact(2*i+1);
s=s+t;
if(abs(t)<1e-8):
break;
return s;
def main():
print("sin(pi/4)=",calcSin(np.pi/4.0));
print("sin(-pi/2)=",calcSin(-np.pi/2.0));
print("cos(pi/3)^2=",1-calcSin(np.pi/3.0)*calcSin(np.pi/3.0));
main();

Kindly revert for any queries

Thanks.


Related Solutions

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)...
Find Taylor series expansion for sin^2(z)
Find Taylor series expansion for sin^2(z)
Write the first three non-zero terms of the Taylor series of sin x near x0 =...
Write the first three non-zero terms of the Taylor series of sin x near x0 = 3π/2. Sketch a plot of the curves for sin x and your approximation.
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...
Find the zeroes and their order for the following. (Use Taylor series or derivatives) sin(z)
Find the zeroes and their order for the following. (Use Taylor series or derivatives) sin(z)
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)
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 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
Find the first four terms in the Taylor series expansion of the solution to y′′(x) −...
Find the first four terms in the Taylor series expansion of the solution to y′′(x) − 2xy′(x) + 2y(x) = 0, y(0) = 1, y′(0) = 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT