Task 04: Formulating and Expressing Functions Using Polynomials
1. Confirm correctness of functions in Table 2.9
All listed series are correct Taylor Series expansions:
e^x: (_{i=0}^)
cos(x): (_{i=0}^)
sin(x): (_{i=0}^)
sinh(x), cosh(x), ln(1/(1 - x)), etc. are also correctly represented.
2. Computational Feasibility vs. Exact Solutions
Computability:
Finite-term approximations yield practical numerical results
Achievable through iterative summation of series terms
Limitations for Exact Solutions:
Infinite terms required for perfect accuracy
No general algebraic solution exists across all x-values
Numerical precision constrained by floating-point representation3. Making them
solvable
3. Making them solvable
Use finite truncation (Taylor approximation).
Select an acceptable number of terms (n) for required precision.
Apply error analysis using first omitted term.
4. Algorithm for Taylor Series (General)
Function taylor_series(fx, x, n):
Initialize sum = 0
For i from 0 to n:
Compute term = function-specific formula (e.g., x^i / i!)
Add term to sum
Return sum
5. Implementation in Python (Example: e^x)
from math import factorial, exp
import numpy as np
def taylor_exp(x, n):
return sum([(x**i)/factorial(i) for i in range(n)])
x_vals = np.arange(0, np.pi/2 + 0.1, 0.5)
n_terms = [5, 20, 30, 100, 500]
for n in n_terms:
print(f"n = {n}")
for x in x_vals:
approx = taylor_exp(x, n)
exact = exp(x)
print(f"x = {x:.2f}, Approx = {approx:.6f}, Exact =
{exact:.6f}, Error = {abs(approx - exact):.2e}")
6. Octave Implementation for cos(x) with Plot
function y = taylor_cos(x, n)
y = 0;
for i = 0:n
y += ((-1)^i * x^(2*i)) / factorial(2*i);
end
endfunction
x_vals = 0:0.5:pi/2;
orders = [5, 20, 30, 100, 500];
for n = orders
approx = arrayfun(@(x) taylor_cos(x, n), x_vals);
actual = cos(x_vals);
plot(x_vals, approx, 'DisplayName', ['n = ', num2str(n)]);
hold on;
end
plot(x_vals, cos(x_vals), 'k--', 'DisplayName', 'Built-in cos(x)');
legend();
title('Comparison of Taylor cos(x) approximations');
xlabel('x'); ylabel('cos(x)');
grid on;
7. Observations
Higher n leads to better approximation.
At n = 20 or above, results are almost indistinguishable from built-in values.
Errors decrease significantly with more terms.
8. Comparison with Built-in Functions
Built-in functions are optimized and usually faster and more precise.
Taylor series is effective for education and manual approximation.
Ideal for understanding the basis of function evaluation in computation.