Wednesday, 25 December 2024

Day 57: Python Program to Find Sum of Cosine Series


 import math

def cosine_series_sum(x, n):

    """

    Calculate the sum of the cosine series up to n terms for a given x.

    :param x: The angle in radians

    :param n: The number of terms in the series

    :return: The sum of the cosine series

    """

    cosine_sum = 0

    sign = 1  

    for i in range(n):

        term = sign * (x**(2 * i)) / math.factorial(2 * i)

        cosine_sum += term

        sign *= -1  

     return cosine_sum

x = float(input("Enter the value of x (in radians): "))

n = int(input("Enter the number of terms in the series: "))

result = cosine_series_sum(x, n)

print(f"The sum of the cosine series up to {n} terms for x = {x} is: {result}")

#source code --> clcoding.com 

Code Explanation:

Import Statement
import math
Imports the math module, which provides mathematical functions like math.factorial().
Function Definition: cosine_series_sum()

def cosine_series_sum(x, n):
    """
    Calculate the sum of the cosine series up to n terms for a given x.
    
    :param x: The angle in radians
    :param n: The number of terms in the series
    :return: The sum of the cosine series
    """
Purpose: Defines a function to compute the cosine of an angle using its Taylor series expansion.
Parameters:
x: The angle in radians.
n: The number of terms in the series.
Returns: The computed cosine value as the sum of the series.

Initialize Variables
    cosine_sum = 0
    sign = 1
cosine_sum: Initializes the sum to 0.
sign: Tracks the alternating signs (+1,−1,+1,−1,…) in the series.

For Loop to Compute Series
    for i in range(n):
Iterates through ๐‘›
n terms of the cosine series.

Calculate Each Term
        term = sign * (x**(2 * i)) / math.factorial(2 * i)
Computes each term in the Taylor series:๐‘ฅ2 ๐‘–x 2i Raises x to the power 
2i (only even powers for cosine).
(2i)!: Computes the factorial of 
2i using math.factorial().
sign: Alternates between +1 and −1 for successive terms.
 
Add Term to Sum
        cosine_sum += term
Adds the computed term to the cumulative sum.

Alternate the Sign
        sign *= -1
Multiplies sign by −1to alternate between 
+1 and −1 for the next term.

Return the Result
    return cosine_sum
Returns the final sum of the cosine series.

Input the Angle and Number of Terms
x = float(input("Enter the value of x (in radians): "))
n = int(input("Enter the number of terms in the series: "))
Prompts the user to:
Enter the angle (๐‘ฅ )in radians.
Enter the number of terms (๐‘›)for the series.

Call the Function
result = cosine_series_sum(x, n)
Calls the cosine_series_sum() function with the provided inputs.
Stores the result in the variable result.

Output the Result
print(f"The sum of the cosine series up to {n} terms for x = {x} is: {result}")
Formats and displays the result using an f-string.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (59) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (939) Python Coding Challenge (373) Python Quiz (31) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses