Program to Find the Sum of Cosine Series

Python Program to Find the Sum of Cosine Series

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

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 cosine(x) series such that

Given n and x, where n is the number of entries in the series and x is the angle in degrees.
Calculate the value of cosine of x using the series expansion formula and compare it to the output of the library function.

cos x = 1 – (x2 / 2 !) + (x4 / 4 !) – (x6 / 6 !) +…

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 cosine series in  python

Examples:

Example1:

Input:

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

Output:

The sum of cosine series of  45 degrees of 10 terms = 0.71

Example2:

Input:

enter the number of degrees = 65
enter number of terms = 20

Output:

The sum of cosine series of  65 degrees of 20 terms = 0.42

Python Program to Find the Sum of Cosine Series

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 cosine series (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 cosine function.
  • Create a cosine function and, using a for loop that iterates twice, convert degrees to radians.
  • Then apply the cosine formula expansion to each term and add them to the sum variable.
  • Then print the cosine expansion’s total sum.
  • Exit of program

Below is the implementation:

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


def sumCosine(degrees, terms):
  # taking a variable which stores sum of cosine series
    sumCosSeries = 1
    # taking sign variable and initialize it to -1
    signofSequence = -1
    for i in range(2, terms, 2):

        # pie value
        pievalue = 22/7
        # degree value of given number
        degval = degrees*(pievalue/180)
        sumCosSeries = sumCosSeries + \
            (signofSequence*(degval**i))/math.factorial(i)
        # changing the sign by multiplying it with -1
        signofSequence = -1*signofSequence

     # returning the sum of cosine series
    return sumCosSeries


degrees = 45
terms = 10
print("The sum of cosine series of ", degrees, "degrees", "of",
      terms, "terms =", round(sumCosine(degrees, terms), 2))

Output:

The sum of cosine series of  45 degrees of 10 terms = 0.71

Explanation:

  • The user must provide the value of x in degrees and the number of terms or give static input, which will be saved in separate variables.
  • These values are supplied as arguments to the cosine functions.
  • A cosine 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 is repeated until the number of terms equals the number specified by the user.
  • Print the total sum

2)Printing sum of cosine series (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 cosine function.
  • Create a cosine function and, using a for loop that iterates twice, convert degrees to radians.
  • Then apply the cosine formula expansion to each term and add them to the sum variable.
  • Then print the cosine expansion’s total sum.
  • Exit of program

Below is the implementation:

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


def sumCosine(degrees, terms):
  # taking a variable which stores sum of cosine series
    sumCosSeries = 1
    # taking sign variable and initialize it to -1
    signofSequence = -1
    for i in range(2, terms, 2):

        # pie value
        pievalue = 22/7
        # degree value of given number
        degval = degrees*(pievalue/180)
        sumCosSeries = sumCosSeries + \
            (signofSequence*(degval**i))/math.factorial(i)
        # changing the sign by multiplying it with -1
        signofSequence = -1*signofSequence

     # returning the sum of cosine series
    return sumCosSeries


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

Output:

enter the number of degrees = 65
enter number of terms = 20
The sum of cosine series of 65 degrees of 20 terms = 0.42

Related Programs: