Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a function (called sin_approx) that takes two inputs, namely the value x for the series above and an accuracy acc. This function must compute and return sin(𝑥) using the series above, within the given accuracy.
For example, if 𝑥 = 𝜋/5 and acc=1e-3 this function should return 0.5869768284775588

What I have tried:

Python
import math

def sin_approx(x, acc):
    approx = 0
    term1 = x
    n = 1
    while abs(term1)>= acc:
        
        approx += term1
        n += 2
        term1 = ((-1)*(n//2))*(x**n)/math.factorial(n)
                 
    return approx
Posted
Updated 16-Apr-23 21:11pm
v2

You should go on a step further, with the approximation. Try
Python
import math
  
def sin_approx(x, acc):
    approx = 0
    term1 = x
    n = 1
    while True:
        approx += term1
        if term1 < acc:
            break
        n += 2
        term1 = ((-1)*(n//2))*(x**n)/math.factorial(n)

    return approx
 
Share this answer
 
Comments
OriginalGriff 17-Apr-23 2:53am    
Fixing his homework for him ... it's far more fun if he works that out for himself ... :laugh:
Without seeing the series that your homework refers to, we can't be specific - but the best guess is that the formula you are using is slightly wrong.

Manually work out what each term in the series formula should be for the first 5 or 6 terms, and compare them against the values your code calculates. When they differ, you can start looking more closely at why.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900