Esempio n. 1
0
def genpis(n):
    polys = []
    # Make P_0
    points = [(i + 1, -1) for i in range(n - 1)]
    points.append((n, 0))
    poly_i = poly(interpolate(points, x))
    polys.append(poly_i)
    # Make P_i
    for i in range(n - 1):
        points = []
        for j in range(n - 1):
            if i == j:
                points.append((j + 1, 2))
            else:
                points.append((j + 1, 0))
        points.append((n, 1))
        poly_i = poly(interpolate(points, x))
        polys.append(poly_i)
    return polys
Esempio n. 2
0
def test_interpolate():
    assert interpolate([1, 4, 9, 16], x) == x**2
    assert interpolate([1, 4, 9, 25], x) == S(3)*x**3/2 - S(8)*x**2 + S(33)*x/2 - 9
    assert interpolate([(1, 1), (2, 4), (3, 9)], x) == x**2
    assert interpolate([(1, 2), (2, 5), (3, 10)], x) == 1 + x**2
    assert interpolate({1: 2, 2: 5, 3: 10}, x) == 1 + x**2
    assert interpolate({5: 2, 7: 5, 8: 10, 9: 13}, x) == \
        -S(13)*x**3/24 + S(12)*x**2 - S(2003)*x/24 + 187
    assert interpolate([(1, 3), (0, 6), (2, 5), (5, 7), (-2, 4)], x) == \
        S(-61)*x**4/280 + S(247)*x**3/210 + S(139)*x**2/280 - S(1871)*x/420 + 6
def test_interpolate():
    assert interpolate([1, 4, 9, 16], x) == x**2
    assert interpolate([1, 4, 9, 25],
                       x) == S(3) * x**3 / 2 - S(8) * x**2 + S(33) * x / 2 - 9
    assert interpolate([(1, 1), (2, 4), (3, 9)], x) == x**2
    assert interpolate([(1, 2), (2, 5), (3, 10)], x) == 1 + x**2
    assert interpolate({1: 2, 2: 5, 3: 10}, x) == 1 + x**2
    assert interpolate({5: 2, 7: 5, 8: 10, 9: 13}, x) == \
        -S(13)*x**3/24 + S(12)*x**2 - S(2003)*x/24 + 187
    assert interpolate([(1, 3), (0, 6), (2, 5), (5, 7), (-2, 4)], x) == \
        S(-61)*x**4/280 + S(247)*x**3/210 + S(139)*x**2/280 - S(1871)*x/420 + 6
def smarter():
    """ using the sympy to solve
    scipy may can not guarantee the integer form
    """
    import sympy
    from sympy.polys.polyfuncs import interpolate
    from sympy.abc import x
    total = 0
    for i in range(1, N + 1):
        fy = interpolate(ylist[:i], x)
        y_i = fy.evalf(subs={x: i + 1})
        print("func fit as:", fy, ", find BOP as:", y_i)
        total = total + y_i
    print("sum of BOP is ", total)
Esempio n. 5
0
 def onclick(event):
     self.a.cla()
     self.a.set_xlim([-10, 10])
     self.a.set_ylim([-10, 10])
     self.a.xaxis.set_ticks(np.arange(-10, 10, 1))
     self.a.yaxis.set_ticks(np.arange(-10, 10, 1))
     self.a.grid(True)
     #print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %(event.button, event.x, event.y, event.xdata, event.ydata))
     plt.plot(round(event.xdata), round(event.ydata), 'bo')
     self.mylist.append(str(round(event.xdata)))
     self.mylist1.append(str(round(event.ydata)))
     self.supp.append((int(round(event.xdata)),int(round(event.ydata))))
     self.a.scatter([u for u,v in iter(self.supp)],[v for u,v in iter(self.supp)], color='red')
     f=lambdify((x,),interpolate(self.supp,x))
     T=np.linspace(-10,10,1000)
     ft=[f(i) for i in T]
     plt.plot(T,ft,'g')
     plt.title(r"$P_n(x)="+latex(interpolate(self.supp,x))+"$",fontsize=12, color='red')
     ID1.set(self.mylist)
     ID2.set(self.mylist1)
     self.fig.canvas.draw()
     print(self.supp)
     print(interpolate(self.supp,x))
Esempio n. 6
0
def test_interpolate():
    assert interpolate([1,4,9,16], x) == x**2
    assert interpolate([(1, 1), (2, 4), (3, 9)], x) == x**2
    assert interpolate([(1, 2), (2, 5), (3, 10)], x) == 1 + x**2
    assert interpolate({1: 2, 2: 5, 3: 10}, x) == 1 + x**2
Esempio n. 7
0
def test_interpolate():
    assert interpolate([1, 4, 9, 16], x) == x**2
    assert interpolate([(1, 1), (2, 4), (3, 9)], x) == x**2
    assert interpolate([(1, 2), (2, 5), (3, 10)], x) == 1 + x**2
    assert interpolate({1: 2, 2: 5, 3: 10}, x) == 1 + x**2
# %% Funciones de forma del elemento rectangular lagrangiano de 16 nodos

import matplotlib.pyplot as plt
from matplotlib import cm
from sympy.polys.polyfuncs import interpolate

import numpy as np
import sympy as sp

X, Y = 0, 1

# %% Calculo las funciones de forma unidimensionales
xi, eta = sp.symbols('xi eta')
L4_xi = 4 * [None]  # contenedor para las funciones de forma (en dir XI)
L4_xi[0] = sp.nsimplify(
    interpolate([(-1, 1), (-1 / 3, 0), (1 / 3, 0), (1, 0)], xi))
L4_xi[1] = sp.nsimplify(
    interpolate([(-1, 0), (-1 / 3, 1), (1 / 3, 0), (1, 0)], xi))
L4_xi[2] = sp.nsimplify(
    interpolate([(-1, 0), (-1 / 3, 0), (1 / 3, 1), (1, 0)], xi))
L4_xi[3] = sp.nsimplify(
    interpolate([(-1, 0), (-1 / 3, 0), (1 / 3, 0), (1, 1)], xi))

L4_eta = 4 * [None]  # contenedor para las funciones de forma (en dir ETA)
L4_eta[0] = sp.nsimplify(
    interpolate([(-1, 1), (-1 / 3, 0), (1 / 3, 0), (1, 0)], eta))
L4_eta[1] = sp.nsimplify(
    interpolate([(-1, 0), (-1 / 3, 1), (1 / 3, 0), (1, 0)], eta))
L4_eta[2] = sp.nsimplify(
    interpolate([(-1, 0), (-1 / 3, 0), (1 / 3, 1), (1, 0)], eta))
L4_eta[3] = sp.nsimplify(
Esempio n. 9
0
import numpy as np
import sympy as sp
from sympy.polys.polyfuncs import interpolate
from sympy.matrices import Matrix
sp.init_printing(pretty_print=True)

# %% Definición de variables
xi, x1, x2, x3, L, E, A, b = sp.symbols('xi x1 x2 x3 L E A b')

# %% Defino las posiciones de los nodos
x3 = x1 + L
x2 = (x1 + x3) / 2

# %% Funciones de forma lagrangianas
N1 = interpolate([(-1, 1), (0, 0), (1, 0)], xi)  # = xi*(xi-1)/2
N2 = interpolate([(-1, 0), (0, 1), (1, 0)], xi)  # = (1+xi)*(1-xi)
N3 = interpolate([(-1, 0), (0, 0), (1, 1)], xi)  # = xi*(xi+1)/2

# %% Interpolación de la geometría y sus derivadas
x = sp.simplify(N1 * x1 + N2 * x2 + N3 * x3)
dx_dxi = sp.diff(x, xi)

# NOTA: el siguiente comando solo se puede realizar si x(xi) es una función
# continua e inyectiva. Este es nuestro caso:
# https://en.wikipedia.org/wiki/Inverse_functions_and_differentiation
dxi_dx = 1 / dx_dxi
# recuerde que se debe garantizar que dx_dxi>0 y dxi_dx>0

# %% Definición de la matriz de forma N y matriz de deformación del elemento B
N = Matrix([[N1, N2, N3]])
    def testLagrange(self):
        x = sympy.symbols('x')

        assert sympy.simplify(fonctionsInterpolation.Lagrange([1, 2, 3], [1, 4, 9])) == x ** 2
        assert sympy.simplify(fonctionsInterpolation.Lagrange([1, 2, 3], [1, 4, 9])) == interpolate(
            [(1, 1), (2, 4), (3, 9)], x)
Esempio n. 11
0
def test_interpolate():
    assert interpolate([1, 4, 9, 16], x) == x**2
    assert (interpolate([1, 4, 9, 25], x) == S(3) * x**3 / 2 - S(8) * x**2 +
            S(33) * x / 2 - 9)
    assert interpolate([(1, 1), (2, 4), (3, 9)], x) == x**2
    assert interpolate([(1, 2), (2, 5), (3, 10)], x) == 1 + x**2
    assert interpolate({1: 2, 2: 5, 3: 10}, x) == 1 + x**2
    assert (interpolate({
        5: 2,
        7: 5,
        8: 10,
        9: 13
    }, x) == -S(13) * x**3 / 24 + S(12) * x**2 - S(2003) * x / 24 + 187)
    assert (interpolate([(1, 3), (0, 6), (2, 5), (5, 7), (-2, 4)],
                        x) == S(-61) * x**4 / 280 + S(247) * x**3 / 210 +
            S(139) * x**2 / 280 - S(1871) * x / 420 + 6)
    assert interpolate((9, 4, 9), 3) == 9
    assert interpolate((1, 9, 16), 1) is S.One
    assert interpolate(((x, 1), (2, 3)), x) is S.One
    assert interpolate(dict([(x, 1), (2, 3)]), x) is S.One
    assert interpolate(((2, x), (1, 3)), x) == x**2 - 4 * x + 6