This page was generated from docs/source/notebooks/approx1d.ipynb.

Approximation#

Approximation in 1d using Lagrange Polynomials#

[19]:
from sigmaepsilon.math.function import Function
from sigmaepsilon.math.approx.lagrange import gen_Lagrange_1d
[20]:
gen_Lagrange_1d(x=[-1, 0, 1])
[20]:
{1: {'symbol': '\\phi_{1}', 0: x*(x - 1)/2, 1: x - 1/2, 2: 1, 3: 0},
 2: {'symbol': '\\phi_{2}', 0: 1 - x**2, 1: -2*x, 2: -2, 3: 0},
 3: {'symbol': '\\phi_{3}', 0: x*(x + 1)/2, 1: x + 1/2, 2: 1, 3: 0}}
[21]:
gen_Lagrange_1d(i=[1, 2], sym=True)[1][0]
[21]:
$\displaystyle \frac{x - x_{2}}{x_{1} - x_{2}}$
[22]:
gen_Lagrange_1d(i=[1, 2], sym=False)[1][0]
[22]:
$\displaystyle 0.5 - 0.5 x$
[23]:
gen_Lagrange_1d(x=[-1, 1])[1][0]
[23]:
$\displaystyle \frac{1}{2} - \frac{x}{2}$
[24]:
inds = [1, 2, 3, 4]
data = gen_Lagrange_1d(i=inds)
[25]:
data[1][0]
[25]:
$\displaystyle - 0.5625 x^{3} + 0.5625 x^{2} + 0.0625 x - 0.0625$
[26]:
data[1][0].subs({'x': 2})
[26]:
$\displaystyle -2.1875$
[27]:
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np
import sympy as sy

inds = [1, 2, 3, 4]
data = gen_Lagrange_1d(i=inds)

fig = plt.figure(figsize=(4, 7))  # in inches
fig.patch.set_facecolor('white')
gs = gridspec.GridSpec(len(inds), 1)

xdata = np.linspace(-1, 1, 100)

for i, ind in enumerate(inds):
    ax = fig.add_subplot(gs[i])
    label = '$' + data[ind]['symbol'] + '$'
    ax.set_title(label)
    fnc = Function(data[ind][0])
    fdata = fnc([xdata])
    ax.plot(xdata, fdata)
    ax.hlines(y=0, xmin=-1, xmax=1, colors='k', zorder=-10, lw=1.0)

fig.tight_layout()

../_images/notebooks_approx1d_10_0.png