def get_points(n, a, b): points = [] r = b - a step = r / n for i in arange(-2 * pi, 2 * pi, step): points.append((i, calculate_f_x(i))) points.append((b, calculate_f_x(b))) return points
def get_points(n): points = [] r = end - start step = r / n for i in arange(-2 * pi, 2 * pi, step): points.append((i, calculate_f_x(i))) points.append((end, calculate_f_x(end))) return points
def interpolate(n): points = get_points(n - 1) points = map(list, zip(*points)) a, b = points t = points[0] y = points[1] cubic_spline_natural = create_spline_function(t, y, calculate_cubic_value) cubic_spline_not_a_knot = create_spline_function(t, y, calculate_cubic_value_not_a_knot) quadratic_spline_natural = create_quadratic_function(t, y, calculate_quadratic_value) quadratic_spline_not_a_knot = create_quadratic_function(t, y, calculate_quadratic_value_not_a_knot) x = arange(-2 * pi, 2 * pi, 0.05) w = map(cubic_spline_natural, x) w1 = map(quadratic_spline_natural, x) w2 = map(cubic_spline_not_a_knot, x) w3 = map(quadratic_spline_not_a_knot, x) plt.plot(x, calculate_f_x(x), 'b', linewidth=2.5, label="given f") plt.plot(x, w, 'r--', linewidth=2.5, label="interpolated cubic") # plt.plot(x + 0.2, w2, 'g--', linewidth=2.5, label="interpolated cubic") plt.plot(a, b, 'yo') # plt.plot(x, [cubicSpline(a) for a in x], 'r--', linewidth=2.5, label="interpolated cubic") plt.plot(x, w1, 'c--', linewidth=2.5, label="interpolated quadratic") plt.plot(x, w3, 'm--', linewidth=2.5, label="interpolated quadratic") # f = sci.interp1d(a, b, kind="cubic") # plt.plot(x, f(x), '--', label='interpolated test') plt.legend(loc='upper left') plt.grid(True) plt.show()
def interpolate(n): points = get_points(n - 1) points = map(list, zip(*points)) a, b = points t = points[0] y = points[1] cubic_spline_natural = create_spline_function(t, y, calculate_cubic_value) cubic_spline_not_a_knot = create_spline_function( t, y, calculate_cubic_value_not_a_knot) quadratic_spline_natural = create_quadratic_function( t, y, calculate_quadratic_value) quadratic_spline_not_a_knot = create_quadratic_function( t, y, calculate_quadratic_value_not_a_knot) x = arange(-2 * pi, 2 * pi, 0.05) w = map(cubic_spline_natural, x) w1 = map(quadratic_spline_natural, x) w2 = map(cubic_spline_not_a_knot, x) w3 = map(quadratic_spline_not_a_knot, x) plt.plot(x, calculate_f_x(x), 'b', linewidth=2.5, label="given f") plt.plot(x, w, 'r--', linewidth=2.5, label="interpolated cubic") # plt.plot(x + 0.2, w2, 'g--', linewidth=2.5, label="interpolated cubic") plt.plot(a, b, 'yo') # plt.plot(x, [cubicSpline(a) for a in x], 'r--', linewidth=2.5, label="interpolated cubic") plt.plot(x, w1, 'c--', linewidth=2.5, label="interpolated quadratic") plt.plot(x, w3, 'm--', linewidth=2.5, label="interpolated quadratic") # f = sci.interp1d(a, b, kind="cubic") # plt.plot(x, f(x), '--', label='interpolated test') plt.legend(loc='upper left') plt.grid(True) plt.show()
def gauss_legendre_integrate(n, a, b): [Ws, xs, err] = GaussLegendreWeights(n) half_lenght = (b - a) / 2. # print half_lenght if err == 0: ans = half_lenght * sum(Ws * calculate_f_x(half_lenght * xs + (b + a) * 0.5)) else: err = 1 ans = None return [ans, err]
def interpolate2(n, spline_creator, boundary_conditions, label=0, caption=0): points = get_points(n - 1) points = map(list, zip(*points)) t = points[0] y = points[1] spline = spline_creator(t, y, boundary_conditions) x = arange(-2 * pi, 2 * pi, 0.05) f_i = map(spline, x) f_y = map(calculate_f_x, x) if caption != 0: plt.figure(num=1, figsize=(10, 6), dpi = 150) plt.plot(x, calculate_f_x(x), 'b--', linewidth=1, label="given f") plt.plot(x, f_i, 'r--', linewidth=1, label=label) plt.plot(t, y, 'yo', label="Nodes: " + str(n)) plt.legend(loc='upper left', prop={'size':8}) plt.grid(True) # plt.savefig(str(n) + "_" + caption + ".png", dpi=150) # plt.show() plt.close() print ";" + str("{:1.7f}".format(calculate_mean_square_error(x, f_y, f_i))),
def interpolate2(n, spline_creator, boundary_conditions, label=0, caption=0): points = get_points(n - 1) points = map(list, zip(*points)) t = points[0] y = points[1] spline = spline_creator(t, y, boundary_conditions) x = arange(-2 * pi, 2 * pi, 0.05) f_i = map(spline, x) f_y = map(calculate_f_x, x) if caption != 0: plt.figure(num=1, figsize=(10, 6), dpi=150) plt.plot(x, calculate_f_x(x), 'b--', linewidth=1, label="given f") plt.plot(x, f_i, 'r--', linewidth=1, label=label) plt.plot(t, y, 'yo', label="Nodes: " + str(n)) plt.legend(loc='upper left', prop={'size': 8}) plt.grid(True) # plt.savefig(str(n) + "_" + caption + ".png", dpi=150) # plt.show() plt.close() print ";" + str("{:1.7f}".format(calculate_mean_square_error(x, f_y, f_i))),