Francesco-Zeno-Costanzo / Root-finding

simple code to solve algebraic equation and system of equations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Root-finding

simple code to solve algebraic equation and system of equations.

Bisection method

The bisection algorithm is basically a binary search, and is based on the zeros theorem, i.e. if a function is good enough then there is a zero. Clearly then we more or less need to know where to look because it is necessary that the selected region contains zero. Once an interval has been chosen, the midpoint is sought and the function is evaluated at that point, depending on a condition, the midpoint becomes the new extreme of the interval, and so on the interval decreases (practically the function calculated at an extreme must have opposite sign compared to the same calculated in the other extreme) The implementation of the method is shown in bisection.py

Secant method

The method consists in constructing a sequence of points with the following criterion: given two initial points x_0,x_1, for each n>=1 the point x_{n+1} is the zero of the line passing through the points (x_{n- 1},f(x_{n-1})),(x_{n},f(x_{n}))

The implementation of the method is shown in secanti.py

Newton method

If we consider a x_0 very close to the solution we can expand in Taylor series and obtain:

which leads to the iterative method:

Obviously, in addition to the zeros of a single function, we can also solve a system; Basically newton raphson is like newton's rule seen above, only now x is a vector and instead of the derivative we need to compute the matrix of derivatives and invert it:

Where J is defined as:

The implementation of the method in 1D is shown in tangenti.py, while in sistemi_non_linerai.py an implementation in the 2D case is shown since it is easy to invert a 2D matrix by hand. In the last one there is also a comparison with the functions of scipy

The Kaczmarz method

Let Ax=b be a system of linear equations, let m be the number of rows of A, a_{i} be the i-th row of complex-valued matrix A, and let x^{0} be arbitrary complex-valued initial approximation to the solution of Ax=b. The method consists in iteratively calculating:

where i=k mod m and \overline{a_i} denotes complex conjugation a_{i}

The implementation of the method is shown in kaczmarz.py, and there is also a comparison with the functions of numpy.

Tridiagonal matrix algorithm

The tridiagonal matrix algorithm, is a simplified form of Gaussian elimination that can be used to solve tridiagonal systems of equations:

For such systems, the solution can be obtained in O(n) operations instead of O(n^{3}) required by Gaussian elimination. The steps are:

The solution is then obtained by back substitution

The implementation of the method is shown in tri_sor.f

Successive over-relaxation

Successive over-relaxation (SOR) is a variant of the Gauss–Seidel method:

https://latex.codecogs.com/svg.image?x^{(k+1)}_i = (1-\omega)x^{(k)}_i + \frac{\omega}{a_{ii}} \left(b_i - \sum_{ji} a_{ij}x^{(k)}_j \right),\quad i=1,2,\ldots,n " />

if \omega=1 we get Gauss–Seidel

The implementation of the method is shown in tri_sor.f

Conjugate gradient

POI LO SCRIVO

About

simple code to solve algebraic equation and system of equations

License:GNU General Public License v3.0


Languages

Language:Python 76.5%Language:Fortran 23.5%