numerical package

Submodules

numerical.fit module

Function and approximation.

gauss(x, *p)[source]

Gauss distribution function.

\[f(x)=ae^{-(x-b)^{2}/(2c^{2})}\]
Parameters
  • x (int or float or list or numpy.ndarray) – positions where the gauss function will be calculated

  • p (list) –

    gauss parameters [a, b, c, d]:

    • a – amplitude (\(\int y \,\mathrm{d}x=1 \Leftrightarrow a=1/(c\sqrt{2\pi})\) )

    • b – expected value \(\mu\) (position of maximum, default = 0)

    • c – standard deviation \(\sigma\) (variance \(\sigma^2=c^2\))

    • d – vertical offset (default = 0)

Returns

gauss values at given positions x

Return type

numpy.ndarray

gauss_fit(x, y, e=None, x_fit=None, verbose=False)[source]

Fit Gauss distribution function to data.

Parameters
  • x (int or float or list or numpy.ndarray) – positions

  • y (int or float or list or numpy.ndarray) – values

  • e (int or float or list or numpy.ndarray) – error values (default = None)

  • x_fit (int or float or list or numpy.ndarray) – positions of fitted function (default = None, if None then x is used)

  • verbose (bool) – verbose information (default = False)

Returns

  • numpy.ndarray – fitted values (y_fit)

  • numpy.ndarray – parameters of gauss distribution function (popt: amplitude a, expected value \(\mu\), standard deviation \(\sigma\), vertical offset d)

  • numpy.float64 – full width at half maximum (FWHM)

Return type

tuple

See also

gauss()

numerical.integration module

Numerical integration, numerical quadrature.

de: numerische Integration, numerische Quadratur.

Date

2015-10-15

trapez(f, a=0, b=1, N=10, x=None, verbose=False, save_values=False)[source]

Integration of \(f(x)\) using the trapezoidal rule (Simpson’s rule, Kepler’s rule).

de: Trapezregel, Simpsonregel (Thomas Simpson), Keplersche Fassregel (Johannes Kepler)

Parameters
  • f (function or list) – function to integrate.

  • a (float) – lower limit of integration (default = 0).

  • b (float) – upper limit of integration (default = 1).

  • N (int) – specify the number of subintervals.

  • x (list) – variable of integration, necessary if f is a list (default = None).

  • verbose (bool) – print information (default = False)

Returns

the definite integral as approximated by trapezoidal rule.

Return type

float

The trapezoidal rule approximates the integral by the area of a trapezoid with base h=b-a and sides equal to the values of the integrand at the two end points.

\[f_n(x) = f(a)+\frac{f(b)-f(a)}{b-a}(x-a)\]
\[\begin{split}I &= \int\limits_a^b f(x) \,\mathrm{d}x \\ I &\approx \int\limits_a^b f_n(x) \,\mathrm{d}x \\ &= \int\limits_a^b \left( f(a)+\frac{f(b)-f(a)}{b-a}(x-a) \right) \mathrm{d}x \\ &= \left.\left( f(a)-a\frac{f(b)-f(a)}{b-a} \right) x \right\vert_a^b + \left. \frac{f(b)-f(a)}{b-a} \frac{x^2}{2} \right\vert_a^b \\ &= \frac{b-a}{2}\left[f(a)+f(b)\right]\end{split}\]

The composite trapezium rule. If the interval is divided into n segments (not necessarily equal)

\[a = x_0 \leq x_1 \leq x_2 \leq \ldots \leq x_n = b\]
\[\begin{split}I &\approx \sum\limits_{i=0}^{n-1} \frac{1}{2} (x_{i+1}-x_i) \left[f(x_{i+1})+f(x_i)\right] \\\end{split}\]

Special Case (Equaliy spaced base points)

\[x_{i+1}-x_i = h \quad \forall i\]
\[I \approx h \left\{ \frac{1}{2} \left[f(x_0)+f(x_n)\right] + \sum\limits_{i=1}^{n-1} f(x_i) \right\}\]

Example

\[\begin{split}I &= \int\limits_a^b f(x) \,\mathrm{d}x \\ f(x) &= x^2 \\ a &= 0 \\ b &= 1\end{split}\]

analytical solution

\[I = \int\limits_{0}^{1} x^2 \,\mathrm{d}x = \left. \frac{1}{3} x^3 \right\vert_0^1 = \frac{1}{3}\]

numerical solution

>>> f = lambda(x): x**2
>>> trapez(f, 0, 1, 1)
0.5
>>> trapez(f, 0, 1, 10)
0.3350000000000001
>>> trapez(f, 0, 1, 100)
0.33335000000000004

numerical.ode module

Numerical solver of ordinary differential equations.

Solves the initial value problem for systems of first order ordinary differential equations.

Date

2015-09-21

dxdt_Dt(f, x, t, Dt, *p)[source]
Parameters
  • f (function) – \(f = \dot{x}\)

  • Dt\(\Delta{t}\)

Returns

\(\Delta x = \dot{x} \Delta t\)

e1(f, x0, t, *p, verbose=False)[source]

Explicit first-order method / (standard, or forward) Euler method / Runge-Kutta 1st order method.

de: Euler’sche Polygonzugverfahren / explizite Euler-Verfahren / Euler-Cauchy-Verfahren / Euler-vorwärts-Verfahren

Parameters
  • f (function) – the function to solve

  • x0 (list) – initial condition

  • t (list) – time

  • *p – parameters of the function (thickness, diameter, …)

  • verbose (bool) – print information (default = False)

Approximate the solution of the initial value problem

\[\begin{split}\dot{x} &= f(t,x) \\ x(t_0) &= x_0\end{split}\]

Choose a value h for the size of every step and set

\[t_i = t_0 + i h ~,\quad i=1,2,\ldots,n\]

The derivative of the solution is approximated as the forward difference equation

\[\dot{x}_i = f(t_i, x_i) = \frac{x_{i+1} - x_i}{t_{i+1}-t_i}\]

Therefore one step \(h\) of the Euler method from \(t_i\) to \(t_{i+1}\) is

\[\begin{split}x_{i+1} &= x_i + (t_{i+1}-t_i) f(t_i, x_i) \\ x_{i+1} &= x_i + h f(t_i, x_i) \\\end{split}\]

Example 1:

\[\begin{split}m\ddot{u} + d\dot{u} + ku = f(t) \\ \ddot{u} = m^{-1}(f(t) - d\dot{u} - ku) \\\end{split}\]

with

\[\begin{split}x_1 &= u &\quad \dot{x}_1 = \dot{u} = x_2 \\ x_2 &= \dot{u} &\quad \dot{x}_2 = \ddot{u} \\\end{split}\]

becomes

\[\begin{split}\dot{x}_1 &= x_2 \\ \dot{x}_2 &= m^{-1}(f(t) - d x_2 - k x_1) \\\end{split}\]

or

\[\begin{split}\dot{x} &= f(t,x) \\ \begin{bmatrix} \dot{x}_1 \\ \dot{x}_2 \end{bmatrix} &= \begin{bmatrix} x_2 \\ m^{-1}(f(t) - d x_2 - k x_1) \end{bmatrix} \\ &= \begin{bmatrix} 0 \\ m^{-1} f(t) \end{bmatrix} + \begin{bmatrix} 0 & 1 \\ -m^{-1} k & -m^{-1} d \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}\end{split}\]

Example 2:

\[\begin{split}m(u)\ddot{u} + d(u,\dot{u})\dot{u} + k(u)u = f(t) \\ \ddot{u} = m^{-1}(u)(f(t) - d(u,\dot{u})\dot{u} - k(u)u) \\\end{split}\]

with

\[\begin{split}x_1 &= u &\quad \dot{x}_1 = \dot{u} = x_2 \\ x_2 &= \dot{u} &\quad \dot{x}_2 = \ddot{u} \\\end{split}\]

becomes

\[\begin{split}\dot{x}_1 &= x_2 \\ \dot{x}_2 &= m^{-1}(x_1)(f(t) - d(x_1,x_2) x_2 - k(x_1) x_1) \\\end{split}\]

or

\[\begin{split}\dot{x} &= f(t,x) \\ \begin{bmatrix} \dot{x}_1 \\ \dot{x}_2 \end{bmatrix} &= \begin{bmatrix} x_2 \\ m^{-1}(x_1)(f(t) - d(x_1,x_2) x_2 - k(x_1) x_1) \end{bmatrix} \\ &= \begin{bmatrix} 0 \\ m^{-1}(x_1) f(t) \end{bmatrix} + \begin{bmatrix} 0 & 1 \\ -m^{-1}(x_1) k(x_1) & -m^{-1} d(x_1,x_2) \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}\end{split}\]

The Euler method is a first-order method, which means that the local error (error per step) is proportional to the square of the step size, and the global error (error at a given time) is proportional to the step size.

e2(f, x0, t, *p, verbose=False)[source]

Explicit second-order method / Runge-Kutta 2nd order method.

Parameters
  • f (function) – the function to solve

  • x0 (list) – initial condition

  • t (list) – time

  • *p – parameters of the function (thickness, diameter, …)

  • verbose (bool) – print information (default = False)

e4(f, x0, t, *p, verbose=False)[source]

Explicit fourth-order method / Runge-Kutta 4th order method.

Parameters
  • f (function) – the function to solve

  • x0 (list) – initial condition

  • t (list) – time

  • *p – parameters of the function (thickness, diameter, …)

  • verbose (bool) – print information (default = False)

fixed_point_iteration(f, xi, t, max_iterations=1000, tol=1e-09, verbose=False)[source]
Parameters
  • f (function) – the function to iterate \(f = \Delta{x}(t)\)

  • xi (list) – initial condition \(x_i\)

  • t (float) – time \(t\)

  • *p – parameters of the function (thickness, diameter, …)

  • max_iterations (int) – maximum number of iterations

  • tol (float) – tolerance against residuum (default = 1e-9)

  • verbose (bool) – print information (default = False)

Returns

\(x_{i+1}\)

\[x_{i+1} = x_i + \Delta x\]

See also

dxdt_Dt() for \(\Delta x\)

i1(f, x0, t, *p, max_iterations=1000, tol=1e-09, verbose=False)[source]

Implicite first-order method / backward Euler method.

Parameters
  • f (function) – the function to solve

  • x0 (list) – initial condition

  • t (list) – time

  • *p – parameters of the function (thickness, diameter, …)

  • max_iterations (int) – maximum number of iterations

  • tol (float) – tolerance against residuum (default = 1e-9)

  • verbose (bool) – print information (default = False)

The backward Euler method has order one and is A-stable.

i1n(f, x0, t, *p, max_iterations=1000, tol=1e-09, verbose=False)[source]
newmark_newtonraphson(f, x0, xp0, xpp0, t, *p, gamma=0.5, beta=0.25, max_iterations=1000, tol=1e-09, verbose=False)[source]

Newmark method.

Parameters
  • f (function) – the function to solve

  • x0 (list) – initial condition

  • xp0 (list) – initial condition

  • xpp0 (list) – initial condition

  • t (list) – time

  • *p – parameters of the function (thickness, diameter, …)

  • gamma (float) – newmark parameter for velocity (default = 0.5)

  • beta (float) – newmark parameter for displacement (default = 0.25)

  • max_iterations (int) – maximum number of iterations

  • tol (float) – tolerance against residuum (default = 1e-9)

  • verbose (bool) – print information (default = False)

newmark_newtonraphson_rdk(fnm, x0, xp0, xpp0, t, *p, gamma=0.5, beta=0.25, maxIterations=1000, tol=1e-09, verbose=False)[source]

Newmark method.

Parameters
  • f (function) – the function to solve

  • x0 (list) – initial condition

  • xp0 (list) – initial condition

  • xpp0 (list) – initial condition

  • t (list) – time

  • *p – parameters of the function (thickness, diameter, …)

  • gamma (float) – newmark parameter for velocity (default = 0.5)

  • beta (float) – newmark parameter for displacement (default = 0.25)

  • max_iterations (int) – maximum number of iterations

  • tol (float) – tolerance against residuum (default = 1e-9)

  • verbose (bool) – print information (default = False)

numerical.ode_model module

Mathmatical models governed by ordinary differential equations.

Describes initial value problems as systems of first order ordinary differential equations.

Date

2019-05-25

disk(x, t, *p)[source]

Rotation of an eccentric disk.

Parameters
  • x (list) – values of the function

  • t (list) – time

  • *p

    parameters of the function

    • diameter

    • eccentricity

    • torque

disk_nm(xn, xpn, xppn, t, *p)[source]

Rotation of an eccentric disk.

Parameters
  • xn (list) – values of the function

  • xpn (list) – first derivative values of the function

  • xppn (list) – second derivative values of the function

  • t (list) – time

  • *p

    parameters of the function

    • diameter

    • eccentricity

    • torque

disk_nmmdk(xn, xpn, xppn, t, *p)[source]

Rotation of an eccentric disk.

Parameters
  • xn (list) – values of the function

  • xpn (list) – derivative values of the function

  • xppn (list) – second derivative values of the function

  • t (list) – time

  • *p

    parameters of the function

    • diameter

    • eccentricity

    • torque

Module contents