Need help on python Part 1: Estimate sin(x) Write a Python program named lab3.py
ID: 3908977 • Letter: N
Question
Need help on python
Part 1: Estimate sin(x) Write a Python program named lab3.py that informs the user of its purpose to approximate the value of sin(x), then prompts for the number of terms to use in the calculation and for the value of x in degrees. Your team's code should then compute should convert x to radians, the approximate value, print the result and calculate the correct value of sin(r) using the math library and the difference between the two values TO DO #1: Setup program Import math library * Initialize any necessary variables and constants TO DO #2: Prompt for user input Prompt the user the value of x Prompt the user the number of elements to use in the calculation TO DO #3 : Calculate an approximation of sin(x) Convert to radians Create a for loop based on the number of elements in the calculation . Create an algorithm to approximate sin(x) TO DO #4: Display the results Display the calculated approximate value Display the actual exact value of sin(x) using the math library. Display the absolute difference between the exact value and your calculated approximate valueExplanation / Answer
import math
def fact(n):
if n == 1:
return 1
return n * fact(n-1)
print("This program approximtes the value of sin(x)")
print("More terms more the accuracy")
print("========================================================")
print("Enter the following values:")
x = float(input( " The value of x:"))
n = int(input( " The value of n:"))
print("========================================================")
y = x * (math.pi/180)
sum = x
for i in range(1,n):
a = math.pow(-1,i) * (math.pow(x,2*i + 1)/ fact(2*i + 1))
sum = sum + a
print("For x =", x, "in radians",y)
print(" Estimated value of sin(x):",sum)
print(" Exact value of sin(x):",math.sin(y))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.