Python Program to python program to find the sum of sine series

In this simple python program, we need to compute the sum of sine values. It is a number-based python program.

For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:

What is a sine series?

In this Python program, we need to find the sum of the sine series. In Fourier, analysis sin is a Trigonometric function of an angle. Sine series is a number series that is for finding the value of sin(x). Where ’x’ is the angle in between. The formula to find the sin(x) is denoted by

image

We further expand this sine formula and get the result as

image

Consider an example to understand the idea, take the x as 30, so we have to find the value of sin(30) that is

30 * pi / 180. Where the value of pi is 3.1415 so the equation will be

Sin(30) = 30 * 3.1415 / 180 = 0.5, so the radian value of sin 30 will be 0.5, and we have to apply this value to the equation we mentioned above to get the sine series

Sin(30) = (0.5**3/3!) + (0.5**5/5!) + ….

How to find the sum of the sine series in python?

We have to apply this logic in our python programming, as we accept the value of x in degree and the number of terms, we needed in the sine series. Then we are calling a user-defined function to calculate the value of the sine series in python language. We import a module called math to the program, and we initialize the sine variable to zero, and we open a for loop till the number. Inside the loop, we have to convert the angle from degree to radian using the formula

y=x*(pi/180)

and after getting the value in radian, we have to apply the formula for the sine series in python, which is

sine = sine + ((y**(2.0*i+1))/math.factorial(2*i+1))*sign

then we return the sine value to the calling function and print the value of the sine series using the round method in python programming.

ALGORITHM

STEP 1: Accept the number of series and the angle in degree from the user, using the input method in python language and convert it to the integer using int datatype.

STEP 2: Call the user-defined function to calculate the sine series and print the return value using the round method in python programming basics.

USER DEFINED FUNCTION

STEP 1:  Import a math module to the program to use the inbuilt functions.

STEP 2:  Initialize a sine variable as zero

STEP 3: Open a for loop from 1 to the number using the range method

STEP 4: Calculate the sign using the formula (-1)**i

STEP 5: Convert the angle from degree to radian using the formula y=x*(pi/180)

STEP 6: Calculate the sine series using the formula sine + ((y**(2.0*i+1))/math.factorial(2*i+1))*sign and return the sine value to the calling function using python programming methods.

Python Source Code

                                          import math
def sin(x,n):
    sine = 0
    for i in range(n):
        sign = (-1)**i
        pi=22/7
        y=x*(pi/180)
        sine = sine + ((y**(2.0*i+1))/math.factorial(2*i+1))*sign
    return sine
x=int(input("Enter the value of x in degrees:"))
n=int(input("Enter the number of terms:"))
print(round(sin(x,n),2))
                                      

OUTPUT

Enter the value of x in degrees:30
Enter the number of terms:10
0.5