This Python program reads a value x and a number n of terms, and computes an approximation of arcsin(x) using the Maclaurin series:
arcsin(x) ≈ ∑ [(2^k · x^(2k+1)) / (4^k · (k!)² · (2k+1))] for k from 0 to n
import math
def arcsin_maclaurin(x: float, n: int) -> float:
total = 0.0
for k in range(n + 1):
numerator = (2 ** k) * (x ** (2 * k + 1))
denominator = (4 ** k) * (math.factorial(k) ** 2) * (2 * k + 1)
total += numerator / denominator
return total
# Read inputs from user
x = float(input("Enter x (between -1 and 1): "))
n = int(input("Enter number of terms: "))
approx = arcsin_maclaurin(x, n)
actual = math.asin(x)
print(f"Approximation of arcsin({x}) with {n} terms: {approx:.6f}")
print(f"Actual value using math.asin: {actual:.6f}")
Approximation of arcsin(0.5) with 5 terms: 0.523599
Actual value using math.asin: 0.523599