Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Missing Assignments

Views: 711
Kernel: Python 3 (Anaconda 5)
###### Ex 5: Calculating a series expansion #import numpy #as np #imports the numpy maths function as a more easily called name import math x = input("Please input a value for x (in degrees): ") x2 = int(x) #converts user input to an integer xrad = math.radians(x2) #converts the inputted value to radians ucos = 1 - ((xrad**2)/math.factorial(2)) + ((xrad**4)/math.factorial(4)) + ((xrad**6)/math.factorial(6)) #series expansion of cos(x) to 4 terms mcos = math.cos(xrad) #calculated value cosdiff = ucos - mcos #absolute difference between both cos values cosper = cosdiff/((ucos + mcos)/2)*100 #percentage difference between both values print("Value calculated using a series expansion of cos(x) with 4 terms:",ucos, '\n', "Value calculated using a cosine function:",mcos, '\n', "Absolute difference between both results:",cosdiff, '\n', "Percentage difference between both results:",cosper) #The 4 term expansion of cos(x) has a negligible difference compared to cos(x) as an infinite expansion
Please input a value for x (in degrees):
Value calculated using a series expansion of cos(x) with 4 terms: 0.5036278376714493 Value calculated using a cosine function: 0.5000000000000001 Absolute difference between both results: 0.003627837671449141 Percentage difference between both results: 0.7229448078814174