Skip to content

Repository files navigation

Python Numerical Methods Toolkit

A comprehensive collection of 20 Python programs implementing fundamental numerical methods, designed as a single, self-contained repository for students, engineers, and researchers.


Table of Contents


Numerical Methods Included

Root Finding

#ProgramMethodOrder of Convergence
0101_bisection.pyBisection MethodLinear
0202_newton_raphson.pyNewton-Raphson MethodQuadratic
0303_secant.pySecant MethodSuperlinear
0404_false_position.pyFalse Position (Regula Falsi)Linear (usually faster than bisection)

Linear Systems

#ProgramMethodComplexity
0505_gauss_elimination.pyGaussian Elimination (partial pivoting)O(n^3)
0606_lu_decomposition.pyLU Decomposition (Doolittle)O(n^3) factorise, O(n^2) solve
0707_jacobi.pyJacobi Iterative MethodO(n^2) per iteration
0808_gauss_seidel.pyGauss-Seidel Iterative MethodO(n^2) per iteration

Interpolation

#ProgramMethodNotes
0909_lagrange_interpolation.pyLagrange Polynomial InterpolationDegree n polynomial
1010_newton_interpolation.pyNewton's Divided Difference InterpolationIncremental construction
1111_spline_interpolation.pyNatural Cubic Spline InterpolationPiecewise smooth fit

Numerical Integration

#ProgramMethodAccuracy
1212_trapezoidal.pyComposite Trapezoidal RuleO(h^2)
1313_simpson.pyComposite Simpson's 1/3 RuleO(h^4)
1414_romberg.pyRomberg Integration (Richardson extrapolation)Exponential convergence

Numerical Differentiation

#ProgramMethodAccuracy
1515_finite_difference.pyForward & Central Finite DifferencesO(h) / O(h^2)
1616_higher_order_difference.py5-Point Stencil (higher-order)O(h^4)

Ordinary Differential Equations

#ProgramMethodAccuracy
1717_euler_ode.pyEuler's MethodO(h)
1818_rk4_ode.py4th-Order Runge-KuttaO(h^4)

Eigenvalue Problems

#ProgramMethodNotes
1919_eigen_power.pyPower Method (dominant eigenvalue)Iterative
2020_eigen_qr.pyQR Algorithm (all eigenvalues)Modified Gram-Schmidt

Project Structure

python-numerical-methods/
├── README.md # This file
├── LICENSE # MIT License
├── requirements.txt # Optional dependencies (numpy, matplotlib)
├── .gitignore
├── Makefile # Convenience targets
├── utils.py # Shared utilities (print helpers, matrix ops)
├── src/
│ ├── 01_bisection.py
│ ├── 02_newton_raphson.py
│ ├── 03_secant.py
│ ├── 04_false_position.py
│ ├── 05_gauss_elimination.py
│ ├── 06_lu_decomposition.py
│ ├── 07_jacobi.py
│ ├── 08_gauss_seidel.py
│ ├── 09_lagrange_interpolation.py
│ ├── 10_newton_interpolation.py
│ ├── 11_spline_interpolation.py
│ ├── 12_trapezoidal.py
│ ├── 13_simpson.py
│ ├── 14_romberg.py
│ ├── 15_finite_difference.py
│ ├── 16_higher_order_difference.py
│ ├── 17_euler_ode.py
│ ├── 18_rk4_ode.py
│ ├── 19_eigen_power.py
│ └── 20_eigen_qr.py
└── tests/
└── test_all_methods.py # Automated verification of all methods

Running the Programs

Run a single method

# From the project root directory
python src/01_bisection.py
# Or using Make
make run METHOD=01_bisection

Run all methods sequentially

make run-all

Run the test suite

make test# or
python -m pytest tests/
# or (pure stdlib)
python tests/test_all_methods.py

Example output

$ python src/01_bisection.py
╔══════════════════════════════════════════════════════════════╗
║ Bisection Method — Root Finding ║
╚══════════════════════════════════════════════════════════════╝
Enter lower bound (a): 1
Enter upper bound (b): 2
Enter tolerance: 1e-10
Enter maximum iterations: 100
────────────────────────────────────────────────────────────────
Iter a b x_mid f(x_mid)
────────────────────────────────────────────────────────────────
1 1.00000000 2.00000000 1.50000000 -0.12500000
2 1.50000000 2.00000000 1.75000000 1.10937500
...
Converged after 34 iterations.
Root = 1.5213797068
f(root) = -0.0000000003

Each program:

  • Is interactive — prompts you for input values
  • Displays detailed iteration tables showing convergence
  • Shows error estimates comparing to exact solutions (where applicable)
  • Has consistent formatted output via the shared utils.py

Requirements

Core (zero dependencies — Python 3.7+)

All 20 programs use only the Python standard library (math, sys). No installation is needed.

Optional (for tests and plots)

pip install -r requirements.txt
PackagePurpose
numpyOptional — used in test suite for verification
matplotlibOptional — used in test suite for comparison plots
pytestOptional — alternative test runner

Customisation

To implement your own function, simply edit the f() function at the top of any source file. For example, in src/01_bisection.py:

deff(x):
# Change this to your own functionreturnmath.cos(x) -x# root near 0.7391

All root-finding, ODE, integration, and differentiation programs follow this pattern — the target function is defined at the top and clearly labelled.


Comparison with the C++ Version

A companion C++ implementation is available with identical algorithms, method-by-method, for performance-critical applications or CUDA/OpenMP parallelisation.


License

This project is licensed under the MIT License. See LICENSE for details. Free to use, modify, and distribute.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages