Exemplo n.º 1
0
def cubesplineinterp(f, df, a, b, N):
    # split on sections
    x = sectionbreak(a, b, N)
    step = ((b - a) / N)

    # find m for twice continuous
    M = np.zeros((N, N))
    M[0, 0] = 1
    M[N - 1, N - 1] = 1
    for i, row in enumerate(M[1:-1]):
        i += 1
        row[i - 1] = 1
        row[i] = 4
        row[i + 1] = 1
    b = [df(x[0])] + [(3 / step) * (f(x[i + 1]) - f(x[i - 1]))
                      for i, p in enumerate(x[1:-1])] + [df(x[-1])]

    b = np.array(b)
    m = np.linalg.solve(M, b)
    print(m)
    print(df(x))
    m = df(x)

    # find coefficient of poly
    splines = []
    for i, val in enumerate(x[:-1]):
        A = [[x[i]**3, x[i]**2, x[i], 1]]
        A += [[x[i + 1]**3, x[i + 1]**2, x[i + 1], 1]]
        A += [[3 * x[i]**2, 2 * x[i], 1, 0]]
        A += [[3 * x[i + 1]**2, 2 * x[i + 1], 1, 0]]
        A = np.array(A)

        b = [f(x[i]), f(x[i + 1]), m[i], m[i + 1]]
        b = np.array(b)
        #print(b)
        splines.append(np.linalg.solve(A, b))
    return [x, splines]
Exemplo n.º 2
0
def main():
    # N - number of points
    N = 5
    a, b = -2, 3
    x, splines = cubesplineinterp(f, df, a, b, N)

    # plot spline interp
    plt.subplot(211)
    for i, val in enumerate(x[:-1]):
        st = np.arange(x[i], x[i + 1], 0.01)
        plt.plot(st, Poly(splines[i]).eval(st))

    a, b = -2, 2
    x = np.linspace(a, b, 100)
    plt.subplot(212)
    plt.plot(x, f(x))
    plt.show()
    c = np.array(c, ndmin=1, copy=0)

    x2 = x * 2
    if len(c) == 1:
        c0 = c[0]
        c1 = 0
    elif len(c) == 2:
        c0 = c[0]
        c1 = c[1]
    else:
        nd = len(c)
        c0 = c[-2]
        c1 = c[-1]
        for i in range(3, len(c) + 1):
            tmp = c0
            nd = nd - 1
            c0 = c[-i] - c1 * (2 * (nd - 1))
            c1 = tmp + c1 * x2
    return c0 + c1 * x2


x = np.linspace(-2, 2, 19)

coef = list((hermfit(x, f(x), 10)))
print(f(x))

s = np.arange(-2, 2, 0.01)
y = [evalpol(r, coef) for r in s]
plt.plot(s, hermval(s, coef))
plt.show()
from interpolation import f, newton_interpolation_f, chebyshev_range, np
import matplotlib.pyplot as plt

# Number of points
n = 30

# Interpolation function range
range_start = 2
range_end = 10
step_size = (range_end - range_start) / n

# X and Y values calculation using chebyshev polynomial
x_range_cheb = chebyshev_range(n, range_start, range_end)
y_range_cheb = [f(x) for x in x_range_cheb]

# Made interpolation function
interpol_f = newton_interpolation_f(x_range_cheb, y_range_cheb)

# Shows f(x) and interpolation graphs
plt.plot(np.arange(range_start, range_end, 0.01), [f(x) for x in np.arange(range_start, range_end, 0.01)], 'b',
         label='f(x)')
plt.plot(np.arange(range_start, range_end, 0.01), [interpol_f(x) for x in np.arange(range_start, range_end, 0.01)], 'r',
         label=f'{n} points interpolation function')
plt.scatter(x_range_cheb, y_range_cheb, color='g', label='Interpolation points')

plt.title('f(x) interpolation using Chebyshev x')
plt.xlabel('x')
plt.ylabel('f(x)')

plt.grid(True)
plt.legend()
def main():
    x = np.linspace(-2, 2, 4)
    seg = np.arange(-2, 2, 0.01)
    plt.plot(seg, newton_polynomial(x, f(x), seg))
    plt.show()