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:
In this Python program, we need to find the sum of the sine series. In Fourier analysis sine is a Trigonometric function of an angle. Sine series is a number series that is for finding the value of sine(x). Where ’x’ is the angle in between. The formula to find the sine(x) is denoted by
sine(x) = x * pi / 180
We further expand this sine formula and get the result as
sine(x) = x −x^3/3!+x^5/5!−x^7/7!+x^9/9!· · ·
Consider an example to understand the idea,
take the x as 30, so we have to find the value of sine(30) that is 30 * pi / 180. Where the value of pi is 3.1415
so the equation will be
Sine(30) = 30 * 3.1415 / 180 = 0.5, so the radian value of sine 30 will be 0.5, and we have to apply this value to the equation we mentioned above to get the sine series
Sine(30) = (0.5^3 / 3!) + (0.5^5 / 5!) + ….
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 of terms to calculate the sum of the series. Inside the loop, we have to convert the angle from degree to radian using the formula
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.
STEP 1: Accept the number of terms of series and the angle in degree from the user, using the input method in python language and convert it to the integer using
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 mathematical functions.
STEP 2: Initialize a variable sine as 0
STEP 3: Open a
for loop from 1 to the number using the
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.
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))
Enter the value of x in degrees:30 Enter the number of terms:10 0.5