Loading content...
In numerical computing and mathematical analysis, polynomial series expansions provide a powerful method for approximating complex transcendental functions using simpler polynomial expressions. This technique, rooted in calculus and analysis, allows computers to evaluate functions like exponentials, trigonometric functions, and logarithms with arbitrary precision.
The fundamental idea is to represent a function as an infinite sum of polynomial terms, where each term is computed from the function's derivatives at a specific expansion point. When the expansion is centered at zero, this is known as a Maclaurin series. By truncating this infinite series to a finite number of terms, we obtain practical approximations that balance computational efficiency with accuracy.
Mathematical Formulation:
For a function f(x) that is infinitely differentiable at zero, the polynomial series expansion is:
$$f(x) \approx \sum_{k=0}^{n-1} \frac{f^{(k)}(0)}{k!} x^k$$
where f⁽ᵏ⁾(0) represents the k-th derivative of f evaluated at zero, and k! is the factorial function.
Supported Functions and Their Series:
Convergence and Accuracy: The accuracy of the approximation improves as more terms are added. For values of x close to zero, convergence is rapid. For larger values of |x|, more terms may be required to achieve the same level of accuracy. This trade-off between computational cost and precision is a fundamental consideration in numerical computing.
Your Task: Implement a function that computes the polynomial series expansion for the exponential, sine, or cosine function at a given evaluation point using a specified number of terms. Round the final result to 6 decimal places.
func_name = 'exp', x = 1.0, n_terms = 52.708333For the exponential function e^x, we compute the sum of x^k / k! for k = 0 to n-1.
With x = 1.0 and n_terms = 5, we calculate: • Term 0: 1.0^0 / 0! = 1 / 1 = 1.0 • Term 1: 1.0^1 / 1! = 1 / 1 = 1.0 • Term 2: 1.0^2 / 2! = 1 / 2 = 0.5 • Term 3: 1.0^3 / 3! = 1 / 6 ≈ 0.166667 • Term 4: 1.0^4 / 4! = 1 / 24 ≈ 0.041667
Sum = 1.0 + 1.0 + 0.5 + 0.166667 + 0.041667 = 2.708334, rounded to 2.708333.
The true value of e ≈ 2.71828, so 5 terms provides a reasonable approximation with less than 0.4% error.
func_name = 'sin', x = 0.5, n_terms = 30.479427For the sine function, we use the alternating series: x - x³/3! + x⁵/5! - x⁷/7! + ...
With x = 0.5 and n_terms = 3, we calculate: • Term 0: 0.5^1 / 1! = 0.5 • Term 1: -0.5^3 / 3! = -0.125 / 6 ≈ -0.020833 • Term 2: 0.5^5 / 5! = 0.03125 / 120 ≈ 0.000260
Sum = 0.5 - 0.020833 + 0.000260 ≈ 0.479427
The true value of sin(0.5) ≈ 0.479426, demonstrating excellent convergence for small x values.
func_name = 'cos', x = 1.0, n_terms = 40.540278For the cosine function, we use the alternating series: 1 - x²/2! + x⁴/4! - x⁶/6! + ...
With x = 1.0 and n_terms = 4, we calculate: • Term 0: 1.0^0 / 0! = 1.0 • Term 1: -1.0^2 / 2! = -1 / 2 = -0.5 • Term 2: 1.0^4 / 4! = 1 / 24 ≈ 0.041667 • Term 3: -1.0^6 / 6! = -1 / 720 ≈ -0.001389
Sum = 1.0 - 0.5 + 0.041667 - 0.001389 ≈ 0.540278
The true value of cos(1.0) ≈ 0.540302, showing the approximation is accurate to within 0.005%.
Constraints