Home > Python > Basic Python Programs

Ch. 3.1 Series Expansion

Chapters Index
Program 1

Series Expansion

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Algorithm for Summing Natural Numbers

Input:

Set \( n = 50 \) (the upper limit of the natural numbers to sum).

Initialize Variables:

  • Set total = 0 to store the sum of the natural numbers.
  • Set series = "" to store the string representation of the series.

Loop from 1 to \( n \):

For each \( i \) from 1 to \( n \) (i.e., \( i = 1, 2, 3, \ldots, n \)):

  • Add \( i \) to total (i.e., total += i).
  • Append the string representation of \( i \) followed by " + " to series (i.e., series += f"{i} + ").

Output the Series:

Print the string series which contains the natural numbers from 1 to \( n \) concatenated with " + " symbols.

Output the Total Sum:

Print the value of total, which represents the sum of natural numbers from 1 to \( n \).

Program 2

Series Expansion

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 3

Series Expansion

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Exponential Function Taylor Series Expansion

The exponential function \( e^x \) can be expressed as a Taylor series expansion around \( x = 0 \):

\[ e^x = \sum_{n=0}^{\infty} \frac{x^n}{n!} = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \ldots \]

This series converges for all real values of \( x \) and provides a way to approximate \( e^x \) by summing a finite number of terms.

Variable Initialization:

  • x = 5: Set the value for which \( e^x \) will be computed.
  • tol = 1e-16: Define the tolerance level for convergence, ensuring that the algorithm stops when the terms become sufficiently small.
  • sum, term = 0, 1: Initialize sum to store the cumulative result and term to represent the current term in the series (starting at 1, which corresponds to \( \frac{x^0}{0!} = 1 \)).
  • n = 1: Initialize the term index, which is used to compute factorials.

Loop for Series Calculation:

The loop continues until the absolute value of the current term is less than the tolerance level. This ensures the approximation is accurate.

Inside the loop:

  • sum += term: Adds the current term to the cumulative sum.
  • term = term * x / n: Calculates the next term in the series using the current term. This formula uses the property of the series:
  • \[ \text{Next term} = \frac{x^n}{n!} = \text{Current term} \times \frac{x}{n} \]

  • n += 1: Increments the index for the next term.
Theoretical Principles & Computational Physics Guide

Series Expansion: Taylor & Maclaurin Polynomials with Truncation Error

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Polynomial Approximation: Expands transcendental functions into computable polynomial series around a point of interest.
Convergence Radius: Computes successive terms iteratively until the incremental term falls below numerical precision.
Truncation Error: Balances order of expansion $N$ against floating-point round-off accumulation.

2. Mathematical Formulation & Algorithmic Pipeline

Taylor series expansion and error remainder:

$$ f(x) = \sum_{n=0}^\infty \frac{f^{(n)}(x_0)}{n!}(x - x_0)^n = \sum_{n=0}^N t_n(x) + R_N(x), \quad t_n = t_{n-1} \cdot \frac{x}{n} $$

3. Numerical Mechanics & Implementation Logic

  • Recurrence Relation: Compute each new term from the preceding term ($t_n = t_{n-1} \cdot \frac{x}{n}$) to avoid expensive factorials.
  • Convergence Loop: Terminate when $|t_n| < \epsilon_{\text{tol}}$ or when maximum terms $N_{\max}$ are reached.
  • Domain Reduction: Map arguments into primary fundamental intervals (e.g. $[0, 2\pi]$) for trigonometric stability.

4. Relevant Computational Practice Exercises

Exercise 1
Implement the Taylor series for $e^x$ using term-to-term recurrence and compare against <code>math.exp(x)</code> for $x = -15, 0.5, 10$.
Exercise 2
Compute $\sin(x)$ expansion and verify that truncation error scales as $\mathcal{O}(x^{2N+1}/(2N+1)!)$.
Exercise 3
Demonstrate numerical catastrophic cancellation when evaluating $e^{-x}$ for large positive $x$ via direct series versus $1 / e^x$.
Standard Literature & References:
  • William H. Press et al., "Numerical Recipes: The Art of Scientific Computing", Cambridge University Press (2007).