Numerical Analysis
Numerical Analysis
This module collects numerical methods I implemented in C++ and exposed through Python.
Source Code
The source code is available on GitHub.
Package Documentation
The available functions and examples are documented below.
Main Reference
The main reference is Numerical Analysis, 9th edition, by Richard L. Burden and J. Douglas Faires.
Why Use Numerical Analysis?
Numerical analysis uses approximation algorithms to solve mathematical problems when a symbolic solution is unavailable or impractical.
Example Applications
-
Example 1
Suppose we manage a city whose population at time is , with net birth rate and immigration rate . We can model the population with the differential equation
Its solution is
Suppose the city had a population of 10,000 last year and received 2,000 immigrants. Its current population is 15,500, giving us
We need to find the net birth rate , but we cannot isolate it algebraically. A numerical root-finding method gives us an approximation.
-
Example 2
Suppose a store's sales depend on its local advertising spend. We can write
If the store spends $50,000 on advertising, we may want to estimate how sales change as spending changes:
Without the functional form of , we must approximate the derivative from observed data.
Documentation
-
Solutions of Single Variable Equations
Approximate such that
Implementation:
The implementation uses the secant method, which generally converges faster than bisection when its assumptions hold.
Implementation Example
Solve for in
Exact Solution:
Approximation:
>>> from pysiclib import numerical >>> def example_func(x): ... return x ** 2 + 2 * x - 1 >>> target_y = 14 >>> numerical.equation_solution(example_func, target_y) 3.0 >>> def no_solution_example(x): ... return x ** 2 + 1 >>> target_y = 0 >>> numerical.equation_solution(no_solution_example, target_y) None -
Differentiation
Approximate given an array .
Implementation:
The implementation uses the five-point midpoint method when .
Implementation Example
Solve
Exact Solution:
Approximation:
>>> from pysiclib import numerical, linalg >>> data = linalg.Tensor([x ** 2 + x for x in range(11)]) >>> x_index = 5 >>> numerical.derivative_at_index(data, x_index) 11.0 -
Integration
Approximate given an array .
Implementation:
The implementation uses composite Simpson's rule.
Implementation Example
Solve
Exact Solution:
Approximation:
>>> from pysiclib import numerical, linalg >>> unit_steps = 100 >>> data = linalg.Tensor( ... [(x / unit_steps) ** 2 + (x / unit_steps) for x in ... range(11 * unit_steps)])] ... ) >>> int_start, int_end = 0, 5 * unit_steps ... #note the interval [start, end] is integrated over >>> numerical.integral_index_interval(data, int_start, int_end) 54.107366 -
Differential Equations - Initial Value problems
For given approximate given an -order system of initial value problems having the form
With an initial condition of such that
Where there may exist an arbitrary amount of
Implementation
The implementation uses a Runge-Kutta method.
Implementation Example
Let , find
Exact Solution:
Approximation:
>>> from pysiclib import numerical, linalg >>> def system_of_eqs(t, var_arr): ... var_arr = var_arr.get_buffer()[:] #temporary for now ... dvar_arr = [0.0, 0.0] ... dvar_arr[0] = -4 * var_arr[0] + 3 * var_arr[1] + 6 ... dvar_arr[1] = 0.6 * dvar_arr[0] - 0.2 * var_arr[1] ... return linalg.Tensor(dvar_arr) >>> init_cond = linalg.Tensor([0.0, 0.0]) >>> init_val = 0.0 >>> target_val = 0.5 >>> numerical.initial_value_problem( ... system_of_eqs, init_cond, target_val, init_val) [1.79352705 1.01441545]