Finding the Sum of Cosine Series in Python

In this tutorial, we shall learn how to find the summation of the Cosine series. To achieve this, we require the math module.
This is a very simple and easiest program for finding the sum of the Cosine series.

By importing the math module we can easily find the sum of the cosine series for a given range of degrees.
As the math module

The math module comprises the definitions to perform all the required mathematical functions like finding the factorial, gcd, power and logarithmic functions, etc.

To know more about the functionalities of the math module, click on the below link.
Python-Math module

Now, let’s begin to code after importing the math module. We need to use the print function to print a text on the output screen.

print("Cosine series :\n")

Finding the values of cosine series for a given range of degrees to calculate its summation

Since we need to find the cosine values for a given range, let’s use a loop a for loop and to control the repetition of the block that we shall use the range() method.
This method takes 3 parameters, the 1st parameter will state the beginning value, 2nd is ending value, last is the incremental step value.

In our program, we will be printing a Cosine series from 0 to 90, therefore the 1st value in range method is 0 since we need to even find Cos(90) we should mention 2nd value as 91, as the range method takes 1 less than the ending value.

Now let us see the code.

for j in range(0,91,15):

In the above code, variable j is used for iteration that takes the values given in the range function.

To compute the cosine value, we need to invoke the Cosine function as math.cos() that uses only 1 parameter that is the degree value and to store its value let’s take another variable z.

Let’s see the complete code.

import math
print("Cosine series :\n")
for j in range(0,181,15):
   print("cos", j ,":", z)


The above code outputs the cosine values using the values given in the range method in terms of radians as noted below :


Cosine series :
cos 0: 1.0
cos 15: -0.759687.....
and so on till cos 90.

To know Cosine values in terms of degrees, we need to now invoke the math.radians() function as math.radians() that does the function of angle conversion.

To obtain the summation of the Cosine series

Our next step is to finally sum all the values which we got from the above code. To do this just include a single line of code i.e sum = sum+y within for loop as we need to include all the values to obtain the sum value. Also, we need to initialize the variable sum to 0, else it takes the garbage value.

Finally, after finding the sum of the Cosine series, let’s print the summation using the print statement.
So, the final code is as written below :

import math
print("Cosine series :\n")
for j in range(0,91,15):
     print("cos", j ,":", z)
print("Sum of Cosine series is", sum)
print("Finished printing the Cosine series with its summation!!!!")

OUTPUT for the above code would be as shown below :

Cosine series :
cos 0 : 1.0
cos 15 : 0.9659258262890683
cos 30 : 0.8660254037844387
cos 45 : 0.7071067811865476
cos 60 : 0.5000000000000001
cos 75 : 0.25881904510252074
cos 90 : 6.123233995736766e-17
Sum of Cosine series is 4.297877056362576
Finished printing the Cosine series with its Summation!!!!.


Leave a Reply

Your email address will not be published. Required fields are marked *