pl = np.zeros(zvals.shape, dtype=float) # allocate storage for hermite interpolant (evaluated using modified divided diffrence method) phdd = np.zeros(zvals.shape, dtype=float) # run test for varying numbers of interpolation nodes for n in nvals: lagrange_nodes = np.linspace(-L, L, n+1) # lagrange interpolation nodes lagrange_f_yvals = f(lagrange_nodes) # function values hermite_nodes = np.linspace(-L, L, int((n+1)//2)) hermite_f_yvals = f(hermite_nodes) hermite_df_yvals = df(hermite_nodes) # evaluate interpolant lagrange pl = lagrange(lagrange_nodes, lagrange_f_yvals, zvals) # evaluate interpolant hermite using modified divided diffrence method phdd = hermite(hermite_nodes, hermite_f_yvals, hermite_df_yvals, zvals, method='modified_divided_difference') # generate comparison plots fig, axarr = plt.subplots(1,2) # # overlayed interpolant (normal) plots # ftitle = '$f(z)$' ltitle = '$p_L(z)$' htitle = '$p_H(z)$'
import lagrange as lagrange from Interpolacion import * import pylab as pl x = [2, 4, 5] y = [5, 6, 3] p = lagrange(x, y) print(p) print() r = lagrange(x, y, 4.25) print(r) print() r = lagrange(x, y, [3.15, 3.74, 4.25]) print(r)
def ployinterp_column(s, n, k=5): y = s[list(range(n - k, n)) + list(range(n + 1, n + 1 + k))] # 取数 y = y[y.notnull()] # 剔除空值 return lagrange(y.index, list(y))(n) # 插值并返回插值结果 # #逐个元素判断是否需要插值
# Definição dos valores dos eixos de interpolação XsIE = igualmenteEspacados(50, -180, 180) XsCheb = chebyshev(50, -180, 180) senoIE = list(map(seno, XsIE)) senoCheb = list(map(seno, XsCheb)) # Definição dos pontos a serem usados para os testes testes = igualmenteEspacados(500, -180, 180) senoTeste = list(map(seno, testes)) ###################################################################################### ''' Valores igualmente Espaçados ''' # Cálculos de interpolação com função Seno lagrangeIE = lagrange(testes, XsIE, senoIE) baricentricoIE = baricentrico(testes, XsIE, senoIE) newtonIE = newton(testes, XsIE, senoIE) nevilleIE = neville(testes, XsIE, senoIE) # Gráfico 1 : Resultados da interpolação em todos os algoritmos com função seno plt.figure(1) plt.subplot(211) plt.plot(testes, lagrangeIE, color='green') plt.plot(testes, baricentricoIE, color='blue') plt.plot(testes, newtonIE, color='red') plt.plot(testes, nevilleIE, color='aqua') plt.plot(testes, senoTeste, color='yellow') plt.title('Valores igualmente espaçados') plt.ylabel('Valores Calculados')
from turtle import * from math import * from lagrange import * color("red") # for j in range(0, len(h)): # f = h[j] f = "exp(x)-1.0" w, g = lagrange(x, y, 1.0) h = ["exp(x)-1.0", w] x = 0.0 yo = eval(f) def graficar(h, desde, hasta): tracer(3) penup() goto(300, 0) right(180) pendown() forward(600) penup() goto(0, -300) right(90) pendown() forward(600) espaciado = 30 penup() goto(0, 0) puntos = 300