scipy.integrate.quad()
>>> import scipy
>>> from math import *
>>> def f(x):
... return exp(sin(cos(sinh(cosh(atan(log(x)))))))
>>> scipy.integrate.quad(f, 1, 2000)
(1514.7806778270256, 4.231109731546272e-06)
people.sc.fsu.edu/~jburkardt/cpp_src/triangle_fekete_rule_test
Number of points | Quadrature points | Quadrature weights |
---|---|---|
1 | 0 | 2 |
2 | $-1/{\sqrt{3}}, 1/{\sqrt{3}}$ | $1, 1$ |
3 | $-\sqrt{3/5}, 0, \sqrt{3/5}$ | $5/9, 8/9, 5/9$ |
… | … | … |
\[ \begin{aligned} f''(x) &= \frac{f(x+h) - 2f(x) + f(x-h)}{h^2} - \frac{f^{(4)}(x)}{12}h^2 + \cdots\\ &\approx \htmlClass{color1}{ \frac{f(x+h) -2f(x) + f(x-h)}{h^2}}\end{aligned} \]
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> n = 11
>>> h = 1 / (n - 1)
>>> D = np.diag(-np.ones(n) / h) + np.diag(np.ones(n - 1) / h, 1)
>>> plt.spy(D)
>>> plt.show()
scipy.integrate
has functions to solve initial value problems for ODEs
odeint()
, uses lsoda()
from FORTRAN library odepack
solve_ivp()
, modern alternative with various methodsProof (1/3)
Proof (2/3)
Proof (3/3)
$h$ | $E_\text{Euler}$ | $E_\text{trap}$ |
---|---|---|
2.0e-2 | 2.67e-2 | 9.06e-05 |
1.0e-2 | 1.35e-2 | 2.26e-05 |
5.0e-3 | 6.76e-3 | 5.66e-06 |
2.5e-3 | 3.39e-3 | 1.41e-06 |
\[ h \to h/2 \implies E_\text{Euler} \to E_\text{Euler}/2 \]
\[ h \to h/2 \implies E_\text{trap} \to E_\text{trap}/4 \]
scipy.integrate.solve_ivp
)\[ \def\arraystretch{1.25} \begin{array}{c|cccccc} 0 & & & & & \\ \frac{1}{4} & \frac{1}{4} & & & & & \\ \frac{3}{8} & \frac{3}{32} &\frac{9}{32} & & & & \\ \frac{12}{13} & \frac{1932}{2197} & -\frac{7200}{2197} & \frac{7296}{2197} & & &\\ 1 & \frac{439}{216} & -8 & \frac{3680}{513} & -\frac{845}{4104} & &\\ \frac{1}{2} & \frac{-8}{27} & 2 & \frac{-3544}{2565} & \frac{1859}{4104} & \frac{-11}{40} & \\ \hline y_{k+1} & \frac{25}{216} & 0 & \frac{1408}{2565} & \frac{2197}{4104} & -\frac{1}{5} & 0 \\ \hat{y}_{k+1} & \frac{16}{135} & 0 & \frac{6656}{12825} & \frac{28561}{56430} & -\frac{9}{50} & \frac{2}{55} \end{array} \]
\[ \def\arraystretch{1.25} \begin{array}{c|ccccccccccccc}\textstyle 0 & & & & & & & & & & & & & \\ \frac 2{27} & \frac 2{27} & & & & & & & & & & & & \\ \frac 19 & \frac 1{36} & \frac 1{12} & & & & & & & & & & & \\ \frac 16 & \frac 1{24} & 0 & \frac 18 & & & & & & & & & & \\ \frac 5{12} & \frac 5{12} & 0 & -\frac{25}{16} & \frac{25}{16} & & & & & & & & & \\ \frac 12 & \frac 1{20} & 0 & 0 & \frac 14 & \frac 15 & & & & & & & & \\ \frac 56 & -\frac{25}{108} & 0 & 0 & \frac{125}{108} & -\frac{65}{27} & \frac{125}{54} & & & & & & & \\ \frac 16 & \frac{31}{300} & 0 & 0 & 0 & \frac{61}{225} & -\frac 29 & \frac{13}{900} & & & & & & \\ \frac 23 & 2 & 0 & 0 & -\frac{53}6 & \frac{704}{45} & -\frac{107}9 & \frac{67}{90} & 3 & & & & & \\ \frac 13 & -\frac{91}{108} & 0 & 0 & \frac{23}{108} & -\frac{976}{135} & \frac{311}{54} & -\frac{19}{60} & \frac{17}6 & -\frac 1{12} & & & & \\ 1 & \frac{2383}{4100} & 0 & 0 & -\frac{341}{164} & \frac{4496}{1025} & -\frac{301}{82} & \frac{2133}{4100} & \frac{45}{82} & \frac{45}{164} & \frac{18}{41} & & & \\ 0 & \frac 3{205} & 0 & 0 & 0 & 0 & -\frac 6{41} & -\frac 3{205} & -\frac 3{41} & \frac 3{41} & \frac 6{41} & 0 & & \\ 1 & -\frac{1777}{4100} & 0 & 0 & -\frac{341}{164} & \frac{4496}{1025} & -\frac{289}{82} & \frac{2193}{4100} & \frac{51}{82} & \frac{33}{164} & \frac{12}{41} & 0 & 1 & \\ \hline y_{k+1} & \frac{41}{840} & 0 & 0 & 0 & 0 & \frac{34}{105} & \frac 9{35} & \frac 9{35} & \frac 9{280} & \frac 9{280} & \frac{41}{840} & 0 & 0 \\ \hat{y}_{k+1} & 0 & 0 & 0 & 0 & 0 & \frac{34}{105} & \frac 9{35} & \frac 9{35} & \frac 9{280} & \frac 9{280} & 0 & \frac{41}{840} & \frac{41}{840} \end{array} \]
$h$ | error |
---|---|
$2.0\times 10^{-2}$ | $5.07\times 10^{-3}$ |
$1.0\times 10^{-2}$ | $1.26\times 10^{-3}$ |
$5.0\times 10^{-3}$ | $3.17\times 10^{-4}$ |
$2.5\times 10^{-3}$ | $7.92\times 10^{-5}$ |
equation type | prototypical example | equation |
---|---|---|
hyperbolic | wave equation | $u_{tt} -u_{xx} = 0$ |
parabolic | heat equation | $u_t - u_{xx} = f$ |
elliptic | Poisson equation | $u_{xx} + u_{yy} = f$ |
\[ au_{xx} + bu_{xy} + c u_{yy} + du_x + e u_y + f u + g = 0 \]
\[ u_t + cu_x = 0 \]
For a convergent scheme, the domain of dependence of the PDE must lie within the domain of dependence of the numerical method
For a consistent finite difference approximation to a linear evolutionary problem, the stability of the scheme is necessary and sufficient for convergence
scipy.integrate.odeint
)scipy.sparse