PYTHON
To Evaluate Sine and Cosine by Taylor Series| Python
Introduction
In mathematics, trigonometric functions such as sine and cosine are fundamental. They appear in various scientific and engineering applications, from modeling oscillatory motion to signal processing. One of the most efficient ways to compute these functions numerically is by using Taylor series expansion. In this blog post, we'll delve into the implementation of Taylor series expansion for sine and cosine functions in Python.
Code
import math
# Function to compute sine and cosine using Taylor series expansion
def sine_cosine(angle_deg):
angle_rad = angle_deg * math.pi / 180
sin_x = 0.0
cos_x = 0.0
n = 0
while n <= 20:
sin_x += ((-1) ** n) * (angle_rad ** (2 * n + 1)) / math.factorial(2 * n + 1)
cos_x += ((-1) ** n) * (angle_rad ** (2 * n)) / math.factorial(2 * n)
n += 1
return sin_x, cos_x
# Taking user input for the angle in degrees
angle = float(input("Enter the angle in degrees: "))
# Computing sine and cosine using Taylor series expansion
sine, cosine = sine_cosine(angle)
# Displaying the results
print("Sine of the given angle is:", sine)
print("Cosine of the given angle is:", cosine)
Output
Enter the angle in degrees: 45
Sine of the given angle is: 0.7071067811865476
Cosine of the given angle is: 0.7071067811865475
In this example, we input an angle of 45 degrees. The script computes the sine and cosine of this angle using Taylor series expansion and displays the results. The calculated sine and cosine values are approximately 0.7071067811865476 and 0.7071067811865475 respectively. These values are approximate because the Taylor series expansion is an approximation technique, and we used a finite number of terms (20 in this case) in the expansion. Increasing the number of terms would lead to a more accurate result.
Explanation
1. Importing Necessary Libraries:
We import the math module, which provides mathematical functions and constants for numerical computations.
2. Defining the Function to Compute Sine and Cosine:
We define a function called sine_cosine that takes an angle in degrees (angle_deg) as input.
3. Converting Angle from Degrees to Radians:
We convert the angle from degrees to radians since mathematical functions in Python's math module generally work with radians.
4. Initializing Variables for Sine and Cosine:
We initialize variables sin_x and cos_x to store the sine and cosine values, respectively.
5. Initializing Loop Counter:
We initialize a loop counter n to keep track of the number of terms in the Taylor series expansion.
6. Taylor Series Expansion Loop:
We enter a while loop that iterates until n reaches 20, which determines the number of terms in the Taylor series expansion.
7. Calculating Sine and Cosine Terms:
Inside the loop, we compute the terms of the Taylor series expansion for sine and cosine and update the variables sin_x and cos_x accordingly.
8. Updating Loop Counter:
We increment the loop counter n to move to the next term in the series.
9. Returning Sine and Cosine Values:
After the loop, we return the calculated sine and cosine values.
10. Taking User Input and Displaying Results:
We prompt the user to input an angle in degrees, then call the sine_cosine function to compute the sine and cosine values using the Taylor series expansion. Finally, we display the computed sine and cosine values to the user.
Conclusion
In this blog post, we explored the implementation of Taylor series expansion for sine and cosine functions in Python. Taylor series expansion provides a powerful method for approximating these trigonometric functions with a finite number of terms. By increasing the number of terms in the expansion, we can achieve higher accuracy in our approximations. This technique is widely used in numerical computation and forms the basis for many mathematical algorithms and simulations.
Post a Comment
0 Comments