In: Computer Science
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 ).
`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.