def integrate_parabola(): v = lambda x: x * (x - 1) n_1 = 2 n_2 = 100 tr_1 = trapezoidal(v, 2., 6, n_1) rec_1 = rectangular(v, 2., 6, n_1) tr_2 = trapezoidal(v, 2., 6, n_2) rec_2 = rectangular(v, 2., 6, n_2) error_2 = tr_1 - rec_1 error_100 = tr_2 - tr_1 print("error for 2 breaks: ", error_2) print("error for 100 breaks", error_100)
def rectangular_test_func(): exact = 33.5 v = lambda x: 2 * x**3 n = 2 numerical = rectangular(v, 1., 3, n) error = abs(exact - numerical) print('error: ', error)
def rectangular_methods(f, a, b, n, rec): if rec == 'right': return right_rectangular_method(f, a, b, n) elif rec == 'left': return left_rectangular_method(f, a, b, n) elif rec == 'mid': return rectangular(f, a, b, n)
def test_rectangular_byhand(): expected = 38 v = lambda x: 2 * x**3 computed = rectangular(v, 1., 3, 2) tol = 1E-15 rel_error = abs(expected - computed) success = rel_error < tol msg = "погрешность = {0} больше допуска = {1}".format(rel_error, tol) assert success, msg
def test_rectangular_linear(): v = lambda x: 2 V = lambda x: 2 * x a = 1.1 b = 2.9 expected = V(b) - V(a) computed = rectangular(v, a, b, 2) tol = 1E-15 rel_error = abs(expected - computed) success = rel_error < tol msg = "Погрешность: {0}".format(rel_error) assert success, msg
def rectangular_convergence_rates(f, F, a, b, num_expected=14): expected = F(b) - F(a) n = zeros(num_expected, dtype=int) E = zeros(num_expected) r = zeros(num_expected) for i in range(num_expected): n[i] = 2**(i + 1) computed = rectangular(f, a, b, n[i]) E[i] = abs((expected - computed) / expected) if i > 0: r_im1 = log(E[i] / E[i - 1]) / log(float(n[i] / n[i - 1])) r[i - 1] = float('%.2f' % r_im1) return r