USE PYTHON FOR THE PROGRAMS For each question below write a

USE PYTHON FOR THE PROGRAMS!!!

For each question below, write a small Python (or C, or C++, ...) function. The arguments of the trigonometric functions are angles expressed in radians. How could we compute (cos(x) - 1)/x^2 for values of x that are very close to 0? Use the Taylor series of cos(x) near 0. Compare the Taylor series method with the direct evaluation of the formula (cos(x) - 1)/x^2 (in Python, or C, etc.), when |x| is small. Use the values 10^-6, 10^-10, 10^-16, 0 for x. Similarly, how could we compute sin(x)/x for values of x that are very close to 0? Again, use the Taylor formula. Suppose we have a function s(.) that computes s(x) = sin(x) for 0 lessthanorequalto x

Solution

from math import factorial
def cos(x):
   res = 0
   term = 1
   for i in range(1, 20, 2):
       res += term
       term *= -x * x/ i /(i + 1)
   return res

def sin(x):
sine = 00.0
for i in range(20):
sign = (-1)**i
sine = sine + ((x**(2.0*i+1))/factorial(2*i+1))*sign
return sine

def factorial(n):
if n > 1:
return n * factorial(n-1)
return 1


x=10**-6
print (cos(x)-1)/(x*x) #for Q1
print -(sin(x)/x)*0.5#for Q2
print sin(x)/x# for Q3

============================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ python cospy.py
-0.500044450291
-0.5
1.0

USE PYTHON FOR THE PROGRAMS!!! For each question below, write a small Python (or C, or C++, ...) function. The arguments of the trigonometric functions are angl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site