Numerical Analysis
A Module based on Numerical Analysis
This project is a collection of various Numerical Analysis techniques that I coded in C++ and made available as a Python package.
Source Code
Source Code is available here.
Package Documentation
Documentation can be found below
Main Reference
I have used Numerical Analysis 9th edition by R. L. Burden, J. D. Faires as the main reference.
What is and Why use Numerical Analysis?
Numerical Analysis is the study of algorithms that use numerical approximation rather than using symbolic manipulation to find exact solutions to solve math problems
Example Applications
-
Example 1
Suppose we were in charge of managing a city where the population can be modeled at time by , a net birth rate of , an immigration rate of . Then can be modeled by the differential equation
Which is solved into
Suppose the previous year we had a population of 10,000, and after including the immigration of 2,000 individuals we have population 15,500 for the current year, giving us
We are tasked at finding the net birth rate . The problem arises in that it is impossible to explicitly solve for , and hence we will have to use an approximation technique
-
Example 2
Suppose we manage a store where sales can be explained by the number of dollars spent on local advertising, we can then write
Suppose we are currently are spending $50,000 on advertising and are tasked with finding how sales change by the amount spent on advertising, or
Given we don't know the functional form of we cannot solve for this explicitly and would have to use a numerical approximation technique.
Documentation
-
Solutions of Single Variable Equations
Approximate such that
Implementation:
We utilize the Secant Method which offers much better convergence than binary search
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:
We utilize 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:
We utilize Composite Simpson's Rule (Will switch to something with lower error soon)
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
We utilize 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]