Beispiel #1
0
# 補間点設定
num_points = 11
min_x, max_x = -5.0, 5.0

# 補間点算出
x = np.linspace(min_x, max_x, num_points)
y = true_func(x)

# 補間多項式導出
interpoly_linear = scinp.interpolate.interp1d(x, y)  # linearがデフォルト
interpoly_cubic = scinp.CubicSpline(x, y)  # 3次スプライン

# 誤差導出
x_in_detail = np.linspace(min_x, max_x, num_points * 10)
y_in_detail = true_func(x_in_detail)

y_interpoly_linear = interpoly_linear(x_in_detail)
y_interpoly_cubic = interpoly_cubic(x_in_detail)

relerr_linear = relerr(y_interpoly_linear, y_in_detail)
relerr_cubic = relerr(y_interpoly_cubic, y_in_detail)

print('max relerr of linear interpoly: ', np.max(relerr_linear))
print('max_relerr of cubic interpoly : ', np.max(relerr_cubic))

# -------------------------------------
# Copyright (c) 2021 Tomonori Kouya
# All rights reserved.
# -------------------------------------
Beispiel #2
0
rtol = 1.0e-15
atol = 0.0
max_deg = 1000
x_array = np.linspace(-10, 10, num=10)  # [-10, 10]
maclaurin_val = [0, 0]
deg = [0, 0]
reldiff = [0, 0]

print('                    sin(x)               cos(x)')
print('     x    ,  relerr[0], deg[0], relerr[1], deg[1]')
for x in x_array:
    # sin(x)
    maclaurin_val[0], deg[0] = maclaurin_sin(x, rtol, atol, max_deg)
    math_val = math.sin(x)  # math.sin
    reldiff[0] = relerr(maclaurin_val[0], math_val)

    # cos(x)
    maclaurin_val[1], deg[1] = maclaurin_cos(x, rtol, atol, max_deg)
    math_val = math.cos(x)  # math.cos
    reldiff[1] = relerr(maclaurin_val[1], math_val)

    print(
        f'{x:10.3e}, {reldiff[0]:10.3e}, {deg[0]:5d}, {reldiff[1]:10.3e}, {deg[1]:5d}'
    )

x = 1.5
print('x = 1.5')
# sin(1.5)
maclaurin_val[0], deg[0] = maclaurin_sin(x, rtol, atol, max_deg)
math_val = math.sin(x)  # math.sin
Beispiel #3
0
n = 10

# 乱数の種セット
np.random.seed(n)

# n個の乱数セット
a = npmat.rand(n)

print('a = ', a)

# 立方根
sc_c = scspf.cbrt(a)  # SciPy.special
np_c = np.cbrt(a)  # NumPy

# 相対誤差の最大値と最小値
relerr_vec = relerr(sc_c, np_c)
print('max(reldiff(sc_c, np_c)) = ', np.max(relerr_vec))
print('min(reldiff(sc_c, np_c)) = ', np.min(relerr_vec))

# expm1(a) = exp(a) - 1
sc_c = scspf.expm1(a)
np_c = np.exp(a) - 1

print('SciPy expm1(a) = ', sc_c)
print('NumPy exp(a)-1 = ', np_c)

# 相対誤差の最大値と最小値
relerr_vec = relerr(sc_c, np_c)
print('max(reldiff(sc_c, np_c)) = ', np.max(relerr_vec))
print('min(reldiff(sc_c, np_c)) = ', np.min(relerr_vec))
Beispiel #4
0
# 最高精度を求める
min_approx = 0.0
min_relerr = 1.0
min_n = 3
min_dx = 0.1

print('dx -> ', end='')
for dx in np.logspace(1, 10, num=10, endpoint=1.0e-15, base=0.1):
    print(f'{dx:10.3e}', end=' ')
print()
for n in range(3, 20, 2):
    print(f'{n:3d}', end=':')
    for dx in np.logspace(1, 10, num=10, endpoint=1.0e-15, base=0.1):
        approx_diff = scmisc.derivative(org_func, x, dx, order=n)
        relerr_val = relerr(approx_diff, diff_func(x))
        print(f'{relerr_val:10.3e}', end=' ')
        if min_relerr > relerr_val:
            min_approx = approx_diff
            min_relerr = relerr_val
            min_n = n
            min_dx = dx
    print()

print('最高精度:')
print(
    f'n = {min_n:3d}, dx = {min_dx:10.3e}, relerr = {min_relerr:10.3e}, approx_diff = {min_approx:25.17e}'
)

# -------------------------------------
# Copyright (c) 2021 Tomonori Kouya
Beispiel #5
0
deg = [0, 0]
reldiff = [0, 0, 0, 0, 0]

print(
    '                  maclaurin_sinh(x), cosh(x)     , mysinh(x),  mycosh(x),  mytanh(x)'
)
print(
    '     x    , relerr[0] , relerr[1] ,deg[0],deg[1],  relerr[2],  relerr[3],  relerr[4]'
)
for x in x_array:
    maclaurin_val[0], deg[0] = maclaurin_sinh(x, rtol, atol, max_deg)
    maclaurin_val[1], deg[1] = maclaurin_cosh(x, rtol, atol, max_deg)
    math_val[0] = math.sinh(x)
    math_val[1] = math.cosh(x)
    math_val[2] = math.tanh(x)

    reldiff[0] = relerr(maclaurin_val[0], math_val[0])
    reldiff[1] = relerr(maclaurin_val[1], math_val[1])
    reldiff[2] = relerr(mysinh(x), math_val[0])
    reldiff[3] = relerr(mycosh(x), math_val[1])
    reldiff[4] = relerr(mytanh(x), math_val[2])

    print(
        f'{x:10.3e}, {reldiff[0]:10.3e}, {reldiff[1]:10.3e}, {deg[0]:5d}, {deg[1]:5d}, {reldiff[2]:10.3e}, {reldiff[3]:10.3e}, {reldiff[4]:10.3e}'
    )

# -------------------------------------
# Copyright (c) 2021 Tomonori Kouya
# All rights reserved.
# -------------------------------------
Beispiel #6
0
# 初期値
# y(0) = 1
y0 = np.array([1.0])

# t = [0, 1]
t_interval = [0.0, 1.0]
print('t in ', t_interval)

# 刻み数と刻み幅
max_div_t = 10

# 常微分方程式を解く: オイラー法
euler_ret_t, euler_ret_y = euler(t_interval[1], func, t_interval[0], y0,
                                 max_div_t)
euler_relerr_y = relerr(euler_ret_y, true_y(euler_ret_t, y0).T)

# 常微分方程式を解く: 中点法
mid_point_ret_t, mid_point_ret_y = mid_point(t_interval[1], func,
                                             t_interval[0], y0, max_div_t)
mid_point_relerr_y = relerr(mid_point_ret_y, true_y(mid_point_ret_t, y0).T)

# 常微分方程式を解く: 古典的ルンゲ・クッタ法
erk44_ret_t, erk44_ret_y = erk44(t_interval[1], func, t_interval[0], y0,
                                 max_div_t)
erk44_relerr_y = relerr(erk44_ret_y, true_y(erk44_ret_t, y0).T)

# 表形式で出力
h = (t_interval[1] - t_interval[0]) / max_div_t
print('h = ', h)
print('                        Relative error of y[0]   ')
Beispiel #7
0

# 問題12.4
print('\nProblem 12.4')
a, b = 0, 4/5
print('Trapezoidal    : ', trapezoidal_int(func1, a, b, 4))
print('Simpson13      : ', simpson13_int(func1, a, b, 4))

# 問題12.5
print('\nProblem 12.5')
a, b = 0, np.pi
gval2 = gauss2_int(func2, a, b, 1)
gval3 = gauss3_int(func2, a, b, 1)
gval4 = gauss4_int(func2, a, b, 1)
true_val = true_integral2(a, b)
print('gauss2, relerr : ', gval2, relerr(gval2, true_val))
print('gauss3, relerr : ', gval3, relerr(gval3, true_val))
print('gauss4, relerr : ', gval4, relerr(gval4, true_val))

# 演習問題2
print('\nExercise 2')
a, b = 1, 2
trap = trapezoidal_int(func3, a, b, 4)
simp = simpson13_int(func3, a, b, 4)
gval4 = gauss4_int(func3, a, b, 1)
true_val = true_integral3(a, b)
print('Trapezoidal, relerr : ', trap,  relerr(trap, true_val))
print('Simpson13  , relerr : ', simp,  relerr(simp, true_val))
print('gauss4     , relerr : ', gval4, relerr(gval4, true_val))