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


Messages In This Thread
A program to define sine function - by mohanp06 - Aug-17-2020, 06:19 PM
RE: A program to define sine function - by mohanp06 - Aug-19-2020, 07:07 AM
RE: A program to define sine function - by mohanp06 - Aug-25-2020, 06:16 PM

Forum Jump:

User Panel Messages

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