Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find the sum of sine series
Let us consider that we have a value x and we need to calculate the sum of the sine(x) series. The sine series is represented mathematically as:
sine(x) = x - x³/3! + x?/5! - x?/7! + x?/9! - ...
To solve this series-based problem, we will take the degree as input, convert it to radians, and then iterate through the terms to calculate the sum using the mathematical formula.
Approach
Take input of the number of terms and degree value.
Convert degree to radians using the formula: radians = degrees × ?/180
Iterate over the terms and calculate the sum using power and factorial operations.
Print the calculated sum.
Example
Here's a complete program to calculate the sine series sum ?
import math
# Input: number of terms and degree value
n = 10
degree = 30
# Convert degree to radians
x = degree * math.pi / 180
print(f"Converting {degree}° to radians: {x:.6f}")
# Initialize variables
sine_sum = 0
sign = 1
# Calculate sine series sum
for i in range(n):
term = (x ** (2*i + 1)) / math.factorial(2*i + 1)
sine_sum += sign * term
sign *= -1 # Alternate signs
print(f"Term {i+1}: {sign * -1} * {x:.4f}^{2*i+1} / {2*i+1}! = {sign * -1 * term:.8f}")
print(f"\nSum of sine series with {n} terms: {sine_sum:.10f}")
print(f"Built-in math.sin({degree}°): {math.sin(x):.10f}")
print(f"Difference: {abs(sine_sum - math.sin(x)):.2e}")
Converting 30° to radians: 0.523599 Term 1: 1 * 0.5236^1 / 1! = 0.52359878 Term 2: -1 * 0.5236^3 / 3! = -0.02393758 Term 3: 1 * 0.5236^5 / 5! = 0.00033046 Term 4: -1 * 0.5236^7 / 7! = -0.00000206 Term 5: 1 * 0.5236^9 / 9! = 0.00000001 Term 6: -1 * 0.5236^11 / 11! = -0.00000000 Term 7: 1 * 0.5236^13 / 13! = 0.00000000 Term 8: -1 * 0.5236^15 / 15! = -0.00000000 Term 9: 1 * 0.5236^17 / 17! = 0.00000000 Term 10: -1 * 0.5236^19 / 19! = -0.00000000 Sum of sine series with 10 terms: 0.4999999963 Built-in math.sin(30°): 0.5000000000 Difference: 3.70e-09
Simplified Version
Here's a cleaner implementation without detailed output ?
import math
def sine_series(degree, terms=10):
# Convert degree to radians
x = math.radians(degree)
sine_sum = 0
sign = 1
# Calculate sine series
for i in range(terms):
term = (x ** (2*i + 1)) / math.factorial(2*i + 1)
sine_sum += sign * term
sign *= -1
return sine_sum
# Test the function
degree = 45
calculated_sine = sine_series(degree, 15)
actual_sine = math.sin(math.radians(degree))
print(f"Sine of {degree}° using series: {calculated_sine:.10f}")
print(f"Sine of {degree}° using math.sin(): {actual_sine:.10f}")
print(f"Error: {abs(calculated_sine - actual_sine):.2e}")
Sine of 45° using series: 0.7071067812 Sine of 45° using math.sin(): 0.7071067812 Error: 1.11e-16
How It Works
The sine series converges rapidly for small angles. Each term in the series is calculated using:
Power calculation: x^(2i+1) for odd powers (1, 3, 5, 7, ...)
Factorial calculation: (2i+1)! for denominators
Alternating signs: +, -, +, -, ... pattern
Conclusion
The sine series provides an accurate approximation of the sine function, especially for angles close to zero. More terms in the series yield higher precision, with the error decreasing exponentially for small input values.
