Program to Find the Sum of Sine Series in Python

Python Program to Find the Sum of Sine Series

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Consider the case where we have a value x and we need to determine the sum of the sine(x) series. There are numerous terms in a sine(x) series such that,

sine(x) = x− x^3/fact(3) + x^5/fact(5) −x^7/fact(7).....

To begin solving the specific series-based problem, we will take the degree as input and convert it to radian. To obtain the sum of the entire number of terms in this series, we shall iterate through all of the given terms and obtain the sum by operations.

Given a number x in degrees and number of terms, the task is to print the sum of sine series in  python

Examples:

Example1:

Input:

enter the number of degrees = 55
enter number of terms = 15

Output:

The sum of sine series of 55 degrees of 15 terms = 0.82

Example2:

Input:

enter the number of degrees = 45
enter number of terms = 12

Output:

The sum of sine series of 45 degrees of 12 terms = 0.71

Program to Find the Sum of Sine Series in Python

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

1)Printing sum of sine series in Python (Static input)

Approach:

  • Give the degree value of x and the number of terms as static and store them in separate variables.
  • As parameters, pass these values to the sine function.
  • Create a sine function and then, using a for loop, convert degrees to radians.
  • Then apply the sine formula expansion to each term and add them to the sum variable.
  • Then print the expansion’s total sum.
  • Exit of program.

Below is the implementation:

# importing math module
import math
# function which returns sum of sine series


def sumsine(degrees, terms):
  # taking a variable which stores sum of sine series
    sumSeries = 0
    for i in range(terms):
      # getting sign
        signofNum = (-1)**i
        # pie value
        pievalue = 22/7
        # degree value of given number
        degval = degrees*(pievalue/180)
        sumSeries = sumSeries + ((degval**(2.0*i+1)) /
                                 math.factorial(2*i+1))*signofNum
     # returning the sum of sine series
    return sumSeries


degrees = 30
terms = 10
print(round(sumsine(degrees, terms), 2))

Output:

The sum of sine series of  30 degrees of 10 terms = 0.5

Explanation:

  • The user must provide the value of x in degrees and the number of terms, which will be saved in separate variables.
  • These values are supplied as arguments to the sine functions.
  • A sine function is built, and a for loop is used to convert degrees to radians and calculate the values of each term using the sine expansion formula.
  • The sum variable is added to each term.
  • This process is repeated until the number of phrases equals the number specified by the user.
  • The total sum of sine series is printed

2)Printing sum of sine series in Python (User input)

Approach:

  • Scan  the degree value of x and the number of terms using int(input())  and store them in separate variables.
  • As parameters, pass these values to the sine function.
  • Create a sine function and then, using a for loop, convert degrees to radians.
  • Then apply the sine formula expansion to each term and add them to the sum variable.
  • Then print the expansion’s total sum.
  • Exit of program.

Below is the implementation:

# importing math module
import math
# function which returns sum of sine series


def sumsine(degrees, terms):
  # taking a variable which stores sum of sine series
    sumSeries = 0
    for i in range(terms):
      # getting sign
        signofNum = (-1)**i
        # pie value
        pievalue = 22/7
        # degree value of given number
        degval = degrees*(pievalue/180)
        sumSeries = sumSeries + ((degval**(2.0*i+1)) /
                                 math.factorial(2*i+1))*signofNum
     # returning the sum of sine series
    return sumSeries


degrees = int(input("enter the number of degrees = "))
terms = int(input("enter number of terms = "))
print("The sum of sine series of ", degrees, "degrees", "of",
      terms, "terms =", round(sumsine(degrees, terms), 2))

Output:

enter the number of degrees = 55
enter number of terms = 15
The sum of sine series of 55 degrees of 15 terms = 0.82