Python program to find the sum of sine series

Python program to find the sum of sine series

To find the sum of a sine series, you can use the following formula:

sin(x)=x−3!x3​+5!x5​−7!x7​+9!x9​−…

Where x is the angle in radians.

Given n terms, the Python program to find the sum of the sine series is:

import math def sine_series_sum(x, n): sine_sum = 0 sign = 1 for i in range(1, 2*n, 2): # 1, 3, 5, ... term = math.pow(x, i) / math.factorial(i) sine_sum += sign * term sign = -sign # Alternate the sign for each term return sine_sum # Example usage: x = float(input("Enter the value of x (in radians): ")) n = int(input("Enter the number of terms: ")) print(f"The sum of sine series up to {n} terms for x = {x} is:", sine_series_sum(x, n)) 

In the program above:

  • The sine_series_sum function calculates the sum of the sine series up to n terms for the given angle x in radians.
  • The sign variable is used to alternate the sign for each term in the series (positive and negative).
  • The for loop runs for odd numbers from 1 to 2*n - 1 because the sine series contains odd powers of x.

You can run the program, provide the value of x in radians and the number of terms n, and it will calculate the sum of the sine series up to n terms for the given x.


More Tags

spring-validator vgg-net signal-handling column-sum org.json uitextview sql-query-store class-design cifilter linegraph

More Programming Guides

Other Guides

More Programming Examples