Wednesday, 25 December 2024

Day 56: Python Program to Find Sum of Sine Series

 


import math

def factorial(n):

    """Calculate the factorial of a number."""

    if n == 0 or n == 1:

        return 1

    return n * factorial(n - 1)

def sine_series(x, terms):

    """Calculate the sum of the sine series."""

    sine_sum = 0

    for i in range(terms):

        power = 2 * i + 1

        sign = (-1) ** i 

        term = sign * (x ** power) / factorial(power)

        sine_sum += term

    return sine_sum

angle_degrees = float(input("Enter the angle in degrees: "))

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

angle_radians = math.radians(angle_degrees)

sine_sum = sine_series(angle_radians, terms)

print(f"The sum of the sine series for {angle_degrees}° is: {sine_sum}")

#source code --> clcoding.com 

Code Explanation:

Import Statement
import math
Imports the math module to use its built-in functions, like math.radians for converting degrees to radians.
Factorial Function
def factorial(n):
    """Calculate the factorial of a number."""
    if n == 0 or n == 1:
        return 1
    return n * factorial(n - 1)
Purpose: Computes the factorial of n recursively.
Logic:
If ๐‘›=0 or ๐‘›=1, return 1 (base case).
Otherwise, return ๐‘›×factorial(๐‘›−1)(recursive step).
Example:
factorial(3)=3×2×1=6.

Sine Series Function
def sine_series(x, terms):
    """Calculate the sum of the sine series."""
    sine_sum = 0
    for i in range(terms):
        power = 2 * i + 1
        sign = (-1) ** i
        term = sign * (x ** power) / factorial(power)
        sine_sum += term
    return sine_sum
Logic:
power: Calculates the power 
2i+1 (odd powers only).sign: Alternates between 
1 and −1 for successive terms, determined by 
(−1)๐‘– .
sine_sum: Accumulates the sum of all terms up to the specified number of terms.

Input Angle in Degrees
angle_degrees = float(input("Enter the angle in degrees: "))
Prompts the user to enter the angle in degrees.
Converts the input to a float for precise calculations.

Input Number of Terms
terms = int(input("Enter the number of terms: "))
Prompts the user to specify the number of terms in the series.

Converts the input to an integer.
Convert Degrees to Radians
angle_radians = math.radians(angle_degrees)
Converts the angle from degrees to radians using the math.radians() function.
This step is necessary because the Taylor series for sine requires the angle in radians.

Calculate the Sine Series
sine_sum = sine_series(angle_radians, terms)
Calls the sine_series() function with:
angle_radians: The angle in radians.
terms: The number of terms in the series.
Stores the computed sum in sine_sum.

Output the Result
print(f"The sum of the sine series for {angle_degrees}° is: {sine_sum}")
Uses an f-string to display the result of the sine series for the input angle in degrees.

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