import numpy as np
import sympy as sym
import IPython.display as ipd
import matplotlib.pyplot as pp
def f( x ):
return np.sin( x )
def trapezio( f, a, b, N ):
x = np.linspace( a, b, N + 1 )
fx = f( x )
retval = fx[ 0 ] + 2.0 * fx[ 1 : -1 ].sum() + fx[ -1 ]
retval = retval * ( b - a ) / ( 2.0 * N )
return retval
def If( a, b ):
return -np.cos( b ) + np.cos( a )
print( If( 0, 1 ) )
print( trapezio( f, 0, 1, 10 ) )
$$ \left|\frac{(b - a)^3}{12N^2}f''( \eta )\right| \leq \epsilon $$
$$ \left|\frac{(b - a)^3}{12N^2}\right||f''( \eta )| \leq \epsilon $$
$$ M = \max_{x \in [ a, b ]}|f''(x)| $$
$$ \left|\frac{(b - a)^3}{12N^2}\right||f''( \eta )| \leq \left|\frac{(b - a)^3}{12N^2}\right|M \leq \epsilon $$
$$ \frac{(b - a)^3}{12N^2}M \leq \epsilon $$
$$ \frac{12N^2}{(b - a)^3M} \geq \frac 1\epsilon $$
$$ N^2 \geq \frac {(b - a)^3M}{12\epsilon} $$
$$ N \geq \sqrt{\frac {(b - a)^3M}{12\epsilon}} $$
eps = 1e-12
M = abs( np.sin( 1.0 ) )
b = 1.0
a = 0.0
N = ( ( ( b - a ) ** 3 * M ) / ( 12.0 * eps ) ) ** 0.5
print( N )
print( trapezio( f, 0, 1, 264807 ) - If( 0.0, 1.0 ) )
def create_ell( i, points, x = sym.symbols( 'x' ) ):
ell = 1
for j in range( len( points ) ):
if j != i:
ell = ell * \
( x - points[ j ] ) / ( points[ i ] - points[ j ] )
return ell
def show_expr( expr ):
ipd.display( ipd.Math( sym.latex( expr ) ) )
( x, a, b ) = sym.symbols( 'x a b' )
def create_points( n, a = sym.symbols( 'a' ), b = sym.symbols( 'b' ) ):
delta = ( b - a ) / n
retval = []
for i in range( n + 1 ):
retval.append( a + delta * i )
return retval
points = create_points( 2 )
print( points )
ells = [ create_ell( i, points ) for i in range( len( points ) ) ]
As = [
sym.factor( sym.integrate( ell, ( x, a, b ) ) ) for
ell in ells
]
print( As )
print(
sym.factor(
sym.integrate(
( x - a ) * ( x - ( a + b ) / 2 ) * ( x - b ),
( x, a, b )
)
)
)
show_expr(
sym.factor(
sym.integrate(
( x - a ) * ( ( x - ( a + b ) / 2 ) ** 2 ) * ( x - b ),
( x, a, b )
)
)
)
24 * 120 == 90 * 2 ** 5
$$ \frac{(b - a)^5}{180N^4}f^{(4)}( \eta ) $$
$$ M := \max_{x \in [ a, b ]}|f^{( 4 )}( x )| $$
$$ \left|\frac{(b - a)^5}{180N^4}f^{(4)}( \eta )\right| = \left|\frac{(b - a)^5}{180N^4}\right||f^{(4)}( \eta )| $$
$$ \left|\frac{(b - a)^5}{180N^4}f^{(4)}( \eta )\right| = \left|\frac{(b - a)^5}{180N^4}\right||f^{(4)}( \eta )| \le \frac{(b - a)^5}{180N^4}M \le \epsilon $$
$$ \frac{(b - a)^5}{180N^4}M \le \epsilon $$
$$ \frac{(b - a)^5}{180\epsilon}M \le N^4 $$
$$ N \ge \sqrt[4]{\frac{(b - a)^5}{180\epsilon}M} $$
M = np.sin( 1 )
eps = 1e-12
a = 0.0
b = 1.0
N = ( ( b - a ) ** 5 * M / ( 180 * eps ) ) ** 0.25
print( N )
def simpson( f, a, b, N ):
x = np.linspace( a, b, N + 1 )
fx = f( x )
retval = fx[ 0 ] + \
4.0 * fx[ 1 : -1 : 2 ].sum() + \
2.0 * fx[ 2 : -1 : 2 ].sum() + \
fx[ -1 ]
retval = retval * ( b - a ) / ( 3.0 * N )
return retval
print( simpson( f, a, b, 262 ) - If( a, b ) )
X = sym.symbols( 'x' )
n = 2
polys = [ X ** i for i in range( n + 2 ) ]
print( polys )
def inner_prod( f, g, X = sym.symbols( 'x' ) ):
return sym.integrate( f * g, ( X, -1, 1 ) )
def gram_schmidt( x, inner_prod ):
n = len( x )
v = []
for k in range( n ):
tmp = x[ k ]
for i in range( k - 1 ):
tmp -= inner_prod( x[ k ], v[ i ] ) * v[ i ]
v.append( tmp / ( sym.sqrt( inner_prod( tmp, tmp ) ) ) )
return v
qs = gram_schmidt( polys, inner_prod )
print( qs )
points_g = sym.solve( qs[ -1 ] )
points_g.sort()
print( points_g )
ells_g = [ create_ell( i, points_g ) for i in range( len( points_g ) ) ]
print( ells_g )
As_g = [
sym.factor( sym.integrate( ell, ( X, -1, 1 ) ) ) for
ell in ells_g
]
print( As_g )
sym.factor(
sym.integrate(
( ( X - points_g[ 0 ] ) * ( X - points_g[ 1 ] ) * ( X - points_g[ 2 ] ) ) ** 2,
( X, -1, 1 )
)
)
print( inner_prod( qs[ 3 ], X ** 2 ) )
def gauss_quad( n ):
x = sym.symbols( 'x' )
polys = [ x ** i for i in range( n + 2 ) ]
q = gram_schmidt( polys, inner_prod )[ -1 ]
nodes = sym.solve( q )
nodes.sort()
As =[
sym.factor( sym.integrate( create_ell( i, nodes ), ( x, -1, 1 ) ) )
for i in range( len( nodes ) )
]
return ( nodes, As )
( nodes, As ) = gauss_quad( 2 )
print( nodes )
print( As )
def gauss_rep( f, a, b, M, nodes, As ):
nodes = np.array( [ float( node ) for node in nodes ] )
As = np.array( [ float( A ) for A in As ] )
h = ( b - a ) / ( 2.0 * M )
retval = 0.0
for i in range( M ):
c = i * 2.0 * h + h
retval += ( f( nodes * h + c ) * As ).sum()
return retval * h
print( gauss_rep( f, a, b, 9, nodes, As ) - If( a, b ) )
$$ \left( \frac{b - a}2 \right)^{7}\frac{f^{(6)}( \eta )}{15750M^6} $$
$$ \left| \left( \frac{b - a}2 \right)^{7}\frac{f^{(6)}( \eta )}{15750M^6} \right| = \left( \frac{b - a}2 \right)^{7}\frac{|f^{(6)}( \eta )|}{15750M^6} $$
$$ U := \max_{x \in [ a, b ]}|f^{(6)}( x )| $$
$$ \left| \left( \frac{b - a}2 \right)^{7}\frac{f^{(6)}( \eta )}{15750M^6} \right| = \left( \frac{b - a}2 \right)^{7}\frac{|f^{(6)}( \eta )|}{15750M^6} \leq \left( \frac{b - a}2 \right)^{7}\frac{U}{15750M^6} $$
$$ \left( \frac{b - a}2 \right)^{7}\frac{U}{15750M^6} \leq \epsilon $$
$$ M^6 \geq \left( \frac{b - a}2 \right)^{7}\frac{U}{15750\epsilon} $$
$$ M \geq \sqrt[6]{\left( \frac{b - a}2 \right)^{7}\frac{U}{15750\epsilon}} $$
U = np.sin( b )
print( ( ( ( b - a ) / 2.0 ) ** 7 * U / ( 15750 * eps ) ) ** ( 1.0 / 6.0 ) )
b = 2.0
M = int( np.ceil( ( ( ( b - a ) / 2.0 ) ** 7 * U / ( 15750 * eps ) ) ** ( 1.0 / 6.0 ) ) )
print( M )
print( gauss_rep( f, a, b, M, nodes, As ) - If( a, b ) )