Python Program to find sum of cosine series

March 31, 2022, Learn eTutorial
1143

In this simple python program, we need to compute the sum of cosine values. It is a number-based python program.

For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:

What is the cosine series?

In this python program, we need to find the value of the cosine series. The cosine series is a Fourier series in calculus. So what is mean by cosine series, it is used to calculate the value of cos(x) where the x is the angle that is given in degree and to is converted to radians. The formula used to solve the cosine series is given below.

cosx = 1 - (x2/2!) + (x4/4!) - (x6/6!) + ..........

Where X is the angle in between,

We need to convert the angle from degree to the radian, which is used in the cosine formula. We use the formula x= 30 * pi / 180 to convert the x from degree to radian. Take an example where x is 30, applying the above formula to convert the degree to radian. using x = 30 * 3.1415 / 180 = 0.5.

Then we apply the value of x in the cosine formula, which will be equal to cos(0.5) = 1 - (0.52/2!) + (0.54 / 4!) .... which we can understand cosine series is an even number series.

How to find the sum of the cosine series in python?

Now applying the logic in the python program, we are importing a `math` module to do the mathematical calculations. We accept the number of terms and angles in degree from used using python programming basics and we call the user-defined function to calculate the value of the cosine series.

Inside the user-defined function, we get the number of terms and angles from the program as parameters, and we open a `for loop` from 2 to the number and increment with two. Inside we convert the angle from degree to radian using the formula y=x*(pi/180), where the angle is in degree. After calculating the angle in radian, we calculate the cosine series using the formula.

cosx = cosx + (sign*(y**i))/math.factorial(i)

and we change the sign to negative in iteration to print the series with an interchanging sign. And we return the value to the calling function for printing the value using the `round` method.

ALGORITHM

STEP 1: Accept the number of terms and the user's angle and save that to the variables. Using the input method and float data type in the python programming language.

STEP 2: Using the print method in python programming, print the cosine series using the round method to get 2 decimal precision. And calling the user-defined function to calculate the value of the cosine series.

User-defined function for calculating cosine series

STEP 1: We have to accept the value angle in degree and number of terms as a parameter.

STEP 2: Initialize the variables for cosx as 1 and sign as negative ones to change the sign in every iteration.

STEP 3: Open a `for loop` in python programming from 2 to a number of terms incrementing by 2.

STEP 4: Convert the value of angle from degree to radian using the formula y=x*(pi/180).

STEP 5: Apply the cosine series formula to get the series printed using the value of y [angle in radian].

STEP 6: Change the sign to negative and repeat the `for loop` and return the value after all iterations of for loop in python programming.

Python Source Code

```                                          ```import math
def cosine(x,n):
cosx = 1
sign = -1
for i in range(2, n, 2):
pi=22/7
y=x*(pi/180)
cosx = cosx + (sign*(y**i))/math.factorial(i)
sign = -sign
return cosx

x=int(input("Enter the value of x in degrees:"))
n=int(input("Enter the number of terms:"))
print(round(cosine(x,n),2))```
```

OUTPUT

```Enter the value of x in degrees:0
Enter the number of terms:10
1.0```

VIEW ALL
VIEW ALL