Complete numerical analysis framework for solving first-order initial value problems (IVPs), systems of IVPs, and approximating solutions with polynomial fitting methods.
This project implements:
Numerical Integration Methods (Single IVP):
- Euler Method
- Predictor-Corrector (Heun)
- Ralston (RK2.2)
- Runge-Kutta 3rd Order (RK3)
- Classical Runge-Kutta (RK4)
Numerical Integration Methods (System IVP & Higher Order):
- Heun Method & RK4 Method for systems
- Multi-method system comparison wrappers
- Higher-order ODE reduction to 1st-order systems
- Boundary Value Problem (BVP) Shooting Method wrapper
- Direct finite difference workflows for higher-order IVP/BVP
Polynomial Fitting Methods:
- Vandermonde Matrix Method
- Lagrange Interpolation
- Least-Squares fitting comparison across selected methods
Supporting Features:
- Finite Difference derivative calculations
- Smart sampling and validation controls
- Optional actual/exact solution comparison
- CSV export of results
- Comparative visualization and analysis
├── numerical_methods/ # Canonical Python package (import from here)
│ ├── problems/ # Edit configs here (single IVP, systems, shooting, etc.)
│ ├── methods/ # Individual method scripts (Euler/Heun/RK...)
│ ├── main/ # Main drivers (solver/systems/shooting/function)
│ ├── fitting/ # Vandermonde/Lagrange/least-squares modules
│ ├── fd/ # Finite-difference workflows
│ ├── experiments/ # Scratch/analysis scripts
│ ├── utils.py
│ ├── matrix.py
│ └── paths.py # out/ path helpers
├── docs/
│ └── index.html
├── out/ # generated outputs (ignored by git)
├── root/ # optional wrapper scripts (python root/solver.py, etc.)
└── README.md
- Use
numerical_methods/problems/ivp.pyfor single ODEs:y' = f(x, y) - Use
numerical_methods/problems/ivpsystems.pyfor systems:x' = f(x, y, z, t)y' = g(x, y, z, t)z' = z_prime(x, y, z, t)
- Use
numerical_methods/problems/ivpreduction.pyfor reducing higher-order ODEs to first-order systems (up to 4 statesu1,u2,u3,u4). - Use
numerical_methods/problems/ivpshooting.pyfor BVP utilizing the shooting method with initial guesses (z0/gamma). - Use
numerical_methods/problems/ivphigherorder.pyfor direct finite difference matrix methods on higher-order equations.
- In
numerical_methods/problems/ivp.py, set:method = 'euler' | 'heun' | 'rk22' | 'rk3' | 'rk4'ls_methods = [...]for least-squares comparisons
- In
numerical_methods/problems/ivpsystems.py(and others likeivpreduction.py,ivpshooting.py), set:method = 'heun' | 'rk4'(for method-focused run scripts)
- In orchestrators like
numerical_methods/main/systems.py,reduction.py, orshooting.py, use:active_methods = ['euler', 'heun', 'ral', 'rk3', 'rk4'](run any subset simultaneously)
# Preferred (module runs)
python -m numerical_methods.main.function
python -m numerical_methods.main.solver
python -m numerical_methods.main.systems
python -m numerical_methods.main.reduction
python -m numerical_methods.main.shooting
python -m numerical_methods.fd.FD
# Optional wrapper scripts (if you prefer python <file>.py style)
python root/function.py
python root/solver.py
python root/systems.py
python root/reduction.py
python root/shooting.py
python root/FD.pypython -m numerical_methods.methods.euler
python -m numerical_methods.methods.heun
python -m numerical_methods.methods.rk22
python -m numerical_methods.methods.rk3
python -m numerical_methods.methods.rk4
python -m numerical_methods.methods.heunsystems
python -m numerical_methods.methods.rk4systems
# Optional wrappers
python root/rk4.pyOutputs that are written to disk (CSVs) are written under out/csv/.
All single-IVP methods solve y' = f(x, y) with initial condition y(x0) = y0.
Implementation note: counter-based loops are used to avoid floating-point drift in endpoint stepping.
y_{n+1} = y_n + h*f(x_n, y_n)
Predictor: y_p = y_n + h*f(x_n, y_n)
Corrector: y_{n+1} = y_n + (h/2)*[f(x_n, y_n) + f(x_{n+1}, y_p)]
k1 = h*f(x_n, y_n)
k2 = h*f(x_n + (3/4)h, y_n + (3/4)k1)
y_{n+1} = y_n + (1/3)k1 + (2/3)k2
k1 = h*f(x_n, y_n)
k2 = h*f(x_n + h/2, y_n + k1/2)
k3 = h*f(x_n + h, y_n - k1 + 2k2)
y_{n+1} = y_n + (1/6)*(k1 + 4k2 + k3)
k1 = h*f(x_n, y_n)
k2 = h*f(x_n + h/2, y_n + k1/2)
k3 = h*f(x_n + h/2, y_n + k2/2)
k4 = h*f(x_n + h, y_n + k3)
y_{n+1} = y_n + (1/6)*(k1 + 2k2 + 2k3 + k4)
System scripts solve:
x' = f(x, y, z, t)
y' = g(x, y, z, t)
z' = z_prime(x, y, z, t)
Implemented system solvers:
- Heun predictor-corrector (
heunsystems.py) - RK4 (
rk4systems.py) - Combined comparison with tables and plots (
numerical_methods/main/systems.py)
-
Reduction:
numerical_methods/main/reduction.pysolves higher-order ODEs up to 4 variables ($u_1, u_2, u_3, u_4$ ) by reducing them to first-order systems. Configured viaproblems/ivpreduction.py. -
Shooting Method:
numerical_methods/main/shooting.pysolves boundary value problems by treating them as IVPs with varied initial values ($z_0$ or$\gamma$ ) to hit target boundary conditions. Configured viaproblems/ivpshooting.py. -
FDM:
numerical_methods/problems/ivphigherorder.pyprovides interfaces for finite difference direct integration.
To avoid overfitting and enforce valid polynomial selection:
- Validate spacing/selection constraints
- Sample points consistently across the interval
- Build polynomial using selected points
Constructs and solves:
V * a = y
where a contains polynomial coefficients.
Run standalone:
python vandermonde.py
python vandermonde_manual.pyConstructs interpolation polynomial directly from selected data points.
Run standalone:
python lagrange.pyFits degree-p polynomial models for methods listed in ls_methods.
Run via:
python function.pyModule FD.py computes finite-difference approximations from numerical method outputs.
Implemented formulas include:
- Forward difference
- Central difference
- Backward difference
import math
def f(x, y):
return math.sqrt(x) * math.sin(2*x) - 5*y
x0 = 0
y0 = 0
xn = 2.4
# choose one style
m = 1
h = 0.3 / (2 ** m)
# n = 9
# h = xn / (n - 1)
method = 'heun' # euler, heun, rk22, rk3, rk4
p = 10
ls_methods = ['euler', 'heun']
y_actual = None # or list of values, or generated valuesimport math
def f(x, y, t):
return x*y + t
def g(x, y, t):
return y*t + x
x0 = 1
y0 = -1
t0 = 0
tn = 0.2
m = 0
h = 0.05 / (2 ** m)
# n = 9
# h = tn / (n - 1)
method = 'rk4' # heun or rk4
p = 10
ls_methods = ['heun', 'rk4']
x_actual, y_actual = None, None # or generated/manual listsSingle-IVP end-to-end workflow:
- Compute numerical solution for
method - Build Vandermonde and Lagrange polynomials
- Run least-squares across
ls_methods - Plot and export results to
out/csv/output_fit.csv
Compares single-IVP methods in one run and can compute errors when y_actual is provided.
Compares selected system-IVP methods (active_methods) with optional actual-solution overlays and error plots (now supports systems of 3 equations: x, y, z).
Systematically reduces higher-order initial value problems down to first-order equation systems (supporting up to 4 states), running comparisons on them.
Resolves Boundary Value Problems by running various IVP trajectories dynamically across varying initial slope guesses (gamma/z0), visualizing hits versus boundaries.
Used to compare selected outputs across refinements and inspect consistency.
Run with:
python test.pyGenerated files may include:
out/csv/output.csv(single-IVP multi-method comparison)out/csv/output_fit.csv(fitting workflow export)out/csv/output_systems_x.csv,_y.csv,_z.csv(system-IVP and shooting workflow comparison tables)out/csv/output_reduction_u1.csvthrough_u4.csv(higher-order reduction state arrays)out/csv/FD.csv(finite difference table)
Helper functions include:
print_table(...)print_table_csv(...)plot_polynomial(...)plot_polynomials_compare(...)
Utilities live at numerical_methods/utils.py and are imported as numerical_methods.utils.
- Set in
numerical_methods/problems/ivp.py:
def f(x, y):
return -2*x*y
x0 = 0
y0 = 1
xn = 2
method = 'rk4'
y_actual = None- Run:
python -m numerical_methods.main.solver- Set in
numerical_methods/problems/ivpsystems.py:
def f(x, y, t):
return x + y
def g(x, y, t):
return x - y
x0 = 1
y0 = 0
t0 = 0
tn = 1- Run:
python -m numerical_methods.main.systems- Accurate step progression with counter-based loops
- Multiple numerical methods for single and system IVPs
- Optional exact/actual-solution error analysis
- Polynomial approximation (Vandermonde, Lagrange, least-squares)
- CSV + console + plot outputs for analysis
- Scripts usable as standalone runs for coursework workflows
TypeError: object of type 'NoneType' has no len()
- Cause: Script expects actual data but
y_actual(orx_actual, y_actual) isNone. - Fix: Use updated scripts and keep actual arrays as either valid lists or
Noneconsistently.
Actual/reference arrays do not match expected length
- Cause: Provided actual values do not align with computed checkpoints.
- Fix: Regenerate from helper functions using the same interval/step settings, or supply lists with consistent lengths.
Unknown method error in function.py
- Cause:
methodnot in supported set for that script. - Fix: Use one of
euler,heun,rk22,rk4forfunction.py.
Plots not appearing
- Cause: Missing matplotlib or non-interactive backend.
- Fix:
pip install matplotliband run in a graphical session.
Numerical instability or oscillation
- Cause: Step size too large for the problem dynamics.
- Fix: Reduce
h(or increase refinementm/n) and verify ODE/system definitions.
- Python 3.7+
- NumPy
- Matplotlib
math(standard library)
Install with:
pip install numpy matplotlibAcademic use for numerical methods coursework.