Find the Sum of Sine Series in Python
In this article, we will learn how to find the sum of the sine series using Python. To implement this problem we will use the math module.
The math module in Python provides many inbuilt mathematical functions. To learn more about the math module click here.
Sine series is an infinity series. So we will calculate the sum of the sine series for the first n terms.
Sine Formula:
sinx = x – (x³/3!) + (x^5/5!) – (x^7/7!)+ …..
x is the value of the angle in radian.
Example
Input: n = 10 x = 30 Output: 0.5001825021996699
Python program for Calculating the Sum of the Sine Series
1. Import the necessary modules (i.e. import math).
2. Get user input.
3. Declare sign variable to keep track of the alternative signs in the sine series.
4. Convert user input x (i.e. degree) to radian.
5. Use the factorial() method in the math module to calculate the factorial.
6. Finally, apply the sine formula.
import math def sine_sum(n, x): result = 0 for i in range(n): sign = pow(-1, i) pi = 22/7 angle = x*(pi/180) result += ((pow(angle, (2.0*i+1)))/math.factorial(2*i+1))*sign return result n = int(input("Enter the number of items(n): ")) x = int(input("Enter the angle in degree(x): ")) print("The sum of the series is ", sine_sum(n, x))
Output
Enter the number of items(n): 20 Enter the angle in degree(x): 60 The sum of the series is 0.8662360750607194 Enter the number of items(n): 10 Enter the angle in degree(x): 30 The sum of the series is 0.5001825021996699 Enter the number of items(n): 10 Enter the angle in degree(x): 90 The sum of the series is 0.999999800133368
Also, refer:
Leave a Reply