示例#1
0
import matplotlib.pyplot as plt
import function as fun
import numpy as np
import scipy as sp

x, y = fun.openFile('data.txt')
plt.plot(x, y, 'orange')
plt.xlim(0, 2)
plt.ylim(0, 2)


def approximation(x, y):
    d = 1
    fp, residuals, rank, sv, rcond = sp.polyfit(x, y, d, full=True)
    f = sp.poly1d(fp)
    # print('Коэффициент -- a %s  '%round(fp[0],4))
    # print('Коэффициент-- b %s  '%round(fp[1],4))
    # print('Коэффициент -- c %s  '%round(fp[2],4))
    # y1=[fp[0]*x[i]**2+fp[1]*x[i]+fp[2] for i in range(0,len(x))] # значения функции a*x**2+b*x+c
    # so=round(sum([abs(y[i]-y1[i]) for i in range(0,len(x))])/(len(x)*sum(y))*100,4) # средняя ошибка
    # print('Average quadratic deviation '+str(so))
    fx = sp.linspace(x[0], x[-1] + 1, len(x))
    plt.plot(x, y, 'o', markersize=10)
    plt.plot(fx, f(fx), linewidth=2)
    plt.grid(True)
    plt.show()


approximation(x, y)
示例#2
0
import matplotlib.pyplot as plt
import function as fun
import numpy as np

cx, cy = fun.openFile('coord')
yarray = np.array(cy, dtype=float)
xarray = np.array(cx, dtype=float)
xnew = np.linspace(np.min(xarray), np.max(yarray))
ynew = [fun.funLagranz(xarray, yarray, i) for i in xnew]
plt.plot(xnew, ynew, 'red')
plt.scatter(cx, cy, color='black')
plt.grid(True)
plt.xlabel('X')
plt.ylabel('Y')
plt.legend(("Interpol F(x)", "F(x)"))
plt.show()
示例#3
0
import matplotlib.pyplot as plt
import function as fun
import numpy as np

x, y = fun.openFile('some.txt')
xa = np.array(x, dtype=float)           # np.array() создает массив из списков
ya = np.array(y, dtype=float)
x1 = np.linspace(np.min(xa), np.max(ya))
y1 = [fun.funLagranzh(xa, ya, i) for i in x1]
                                    # np.linspace() создает массив из чисел

plt.plot(x1, y1, 'green')
plt.scatter(x, y)
plt.xlabel('X', fontsize=10)
plt.ylabel('Y', fontsize=10)
plt.legend(('Interpolation F(x)', 'F(x)'))
plt.grid(True)
plt.show()