Python Forum
A program to define sine function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A program to define sine function
#1
I am a technical person but new to python. I like math, science and coding. So here I wrote a small python code to define sine function as a series. (For mathematical definition go here)

#To find sine of any angle in radians from the series expansion of sine function
# n indicates the number of terms used from the series expansion for calculation

import math

def sine(x,n):

    pmax=2*n+2 #max exponent
    ans=0 # variable to hold the final answer

    for i in range(1,pmax,2):

        if(i%4==1):
            sign=1
        else:
            sign=-1

        ans+=sign*x**i/math.factorial(i)

    return ans
#demo
print(sine(6,25))
How do you find this code? Can I improve it in any way or is it fine?
Also it gives errors for very high value of x, I'm not sure it's mathematical or a coding issue.

Many Thanks!
Reply
#2
You can improve the code by using the relation that exists between two consecutive terms of the series, thus avoiding the computation of high powers of x and factorials
from itertools import count, islice
import math

def sine_terms(x):
    t = x
    f = -x ** 2
    for n in count(3, 2):
        yield t
        t *= f /((n-1) * n)
        
def sine(x, n):
    return sum(islice(sine_terms(x), n))

if __name__ == '__main__':
    print(sine(6, 25), math.sin(6))
Output:
-0.2794154981989254 -0.27941549819892586
There is necessarily an error for large values of x because the sine function is not a polynomial. You could find online estimates for the remainder of the series, for example here is an estimate for the remainder of the exponential series, which should work also for the sine series (I haven't checked the estimate). In fact, for the sine series you can get an easy bound for the error by using the Lagrange remainder of the Taylor formula.
Reply
#3
I am a newbie to python and cannot understand most of your code, so whatever I didn't understand I have marked in bold. Kindly elaborate.
Thanks!

from itertools import count, islice
import math

def sine_terms(x):
t = x
f = -x ** 2
for n in count(3, 2):
yield t
t *= f /((n-1) * n)

def sine(x, n):
return sum(islice(sine_terms(x), n))

if __name__ == '__main__':
print(sine(6, 25), math.sin(6))
Reply
#4
mohanp06 Wrote:so whatever I didn't understand I have marked in bold.
from itertools import count, islice
This imports two functions from the itertools module of the standard library. This module contains various functions to help handle "iterables", which are the python name for general sequences. These sequences can have finitely many terms or not. For example count(3, 2) is the infinite sequence 3, 5, 7, 9, ....
for n in count(3, 2):
    yield t
    ...
This loops runs every integer n in the iterable count(3, 2) described above, and every time it generates a value by using the yield keyword. This keyword is used to define "generator functions" that create finite or infinite sequences. Every function that contains the yield keyword is such a generator instead of a normal function. For example here is
a function that generates the square of all numbers up to a limit
def squares(m):
    for n in range(m):
        yield n ** 2

for s in squares(4):
    print(s)  # prints 0, 1, 4, 9
The islice functions takes at most n terms of an iterable (a sequence)
islice(sine_terms(x), n)  # <-- the n first terms of the iterable sine_terms(x)
Finally the colloquial
if __name__ == '__main__':
    ...
When python code is executed, the variable __name__ contains the name of the current module. When this name is "__main__", it means that the current module is the main program. This test is used to declare code that we only want to run when this file is used as our main program and not as a library module.
Reply
#5
Thank you so much for the elaborate explanation!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020