線形補間を使用して三角関数および逆三角関数を簡略化する

def linear_interpolation(x, x0, y0, x1, y1):
    """
    线性插值函数
    """
    return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

次の例では、最初に補間範囲 (0 ~ π/2) を定義し、次に精度要件に従って補間ステップ サイズを計算します。次に、入力値に最も近い補間点を見つけ、線形補間式を使用して sin 関数の値を近似します。

精度要件 (つまり、内挿点の数) が高くなるほど、近似は指定された入力値での sin 関数の正確な値に近づくことに注意してください。ただし、線形補間を使用しているため、近似の精度が制限される場合があります。より高精度の近似結果が必要な場合は、二次補間や三次補間などの高次の補間方法が考慮されます。

import math

def linear_interpolation(x, x0, y0, x1, y1):
    """
    线性插值函数
    """
    return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

def approximate_sin(x, precision):
    """
    使用线性插值逼近sin函数
    """
    # 定义插值范围
    x0 = 0.0
    y0 = math.sin(x0)
    x1 = math.pi / 2
    y1 = math.sin(x1)

    # 计算插值步长
    step = (x1 - x0) / precision

    # 找到插值点
    interpolation_point = x0
    while interpolation_point < x:
        interpolation_point += step

    # 进行线性插值
    approximation = linear_interpolation(x, interpolation_point - step, math.sin(interpolation_point - step),
                                         interpolation_point, math.sin(interpolation_point))
    return approximation

# 示例使用
x = 1.0  # 输入值
precision = 1000  # 精度要求,即插值点的个数
approximation = approximate_sin(x, precision)
print(approximation)

コス

上の例では、同様の方法で補間範囲と精度の要件を定義し、入力値に最も近い補間点を見つけ、線形補間公式を使用して cos 関数の値を近似します。

同様に、近似の精度は精度要件 (補間点の数) によって異なります。より高精度の近似結果が必要な場合は、二次補間や三次補間などの高次の補間方法が考慮されます。

import math

def linear_interpolation(x, x0, y0, x1, y1):
    """
    线性插值函数
    """
    return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

def approximate_cos(x, precision):
    """
    使用线性插值逼近cos函数
    """
    # 定义插值范围
    x0 = 0.0
    y0 = math.cos(x0)
    x1 = math.pi / 2
    y1 = math.cos(x1)

    # 计算插值步长
    step = (x1 - x0) / precision

    # 找到插值点
    interpolation_point = x0
    while interpolation_point < x:
        interpolation_point += step

    # 进行线性插值
    approximation = linear_interpolation(x, interpolation_point - step, math.cos(interpolation_point - step),
                                         interpolation_point, math.cos(interpolation_point))
    return approximation

# 示例使用
x = 1.0  # 输入值
precision = 1000  # 精度要求,即插值点的个数
approximation = approximate_cos(x, precision)
print(approximation)

黄褐色

 Tan 関数の線形補間近似の場合、tan 関数はいくつかの点 (π/2 の倍数など) で発散するため、補間範囲はこれらの点を除外する必要があることに注意してください。これは、tan 関数に対する線形補間近似の例のコードです。

import math

def linear_interpolation(x, x0, y0, x1, y1):
    """
    线性插值函数
    """
    return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

def approximate_tan(x, precision):
    """
    使用线性插值逼近tan函数
    """
    # 定义插值范围(排除tan函数发散的点)
    x0 = -math.pi / 2 + 0.01
    y0 = math.tan(x0)
    x1 = math.pi / 2 - 0.01
    y1 = math.tan(x1)

    # 计算插值步长
    step = (x1 - x0) / precision

    # 找到插值点
    interpolation_point = x0
    while interpolation_point < x:
        interpolation_point += step

    # 进行线性插值
    approximation = linear_interpolation(x, interpolation_point - step, math.tan(interpolation_point - step),
                                         interpolation_point, math.tan(interpolation_point))
    return approximation

# 示例使用
x = 1.0  # 输入值
precision = 1000  # 精度要求,即插值点的个数
approximation = approximate_tan(x, precision)
print(approximation)

逆正弦

import math

def linear_interpolation(x, x0, y0, x1, y1):
    """
    线性插值函数
    """
    return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

def approximate_arcsin(x, precision):
    """
    使用线性插值逼近arcsin函数
    """
    # 定义插值范围
    x0 = -1.0
    y0 = math.asin(x0)
    x1 = 1.0
    y1 = math.asin(x1)

    # 计算插值步长
    step = (x1 - x0) / precision

    # 找到插值点
    interpolation_point = x0
    while interpolation_point < x:
        interpolation_point += step

    # 进行线性插值
    approximation = linear_interpolation(x, interpolation_point - step, math.asin(interpolation_point - step),
                                         interpolation_point, math.asin(interpolation_point))
    return approximation

# 示例使用
x = 0.5  # 输入值
precision = 1000  # 精度要求,即插值点的个数
approximation = approximate_arcsin(x, precision)
print(approximation)

 

上の例では、補間範囲 (-1 から 1) を定義し、補間ステップ サイズを計算しました。次に、入力値に最も近い補間点を見つけ、線形補間式を使用して arcsin 関数の値を近似します。

arcsin 関数の性質により、いくつかの点で近似の精度が低くなる可能性があるため、適切な内挿範囲と精度要件を選択する必要があることに注意してください。より高精度の近似結果が必要な場合は、他の高次の補間方法または他の近似技術を考慮することができます。

アークコス

import math

def linear_interpolation(x, x0, y0, x1, y1):
    """
    线性插值函数
    """
    return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

def approximate_arccos(x, precision):
    """
    使用线性插值逼近arccos函数
    """
    # 定义插值范围
    x0 = -1.0
    y0 = math.acos(x0)
    x1 = 1.0
    y1 = math.acos(x1)

    # 计算插值步长
    step = (x1 - x0) / precision

    # 找到插值点
    interpolation_point = x0
    while interpolation_point < x:
        interpolation_point += step

    # 进行线性插值
    approximation = linear_interpolation(x, interpolation_point - step, math.acos(interpolation_point - step),
                                         interpolation_point, math.acos(interpolation_point))
    return approximation

# 示例使用
x = 0.5  # 输入值
precision = 1000  # 精度要求,即插值点的个数
approximation = approximate_arccos(x, precision)
print(approximation)

 

上の例では、補間範囲 (-1 から 1) を定義し、補間ステップ サイズを計算しました。次に、入力値に最も近い内挿点を見つけ、線形内挿公式を使用して arccos 関数の値を近似します。

他の三角関数の近似と同様に、arccos 関数の性質により、いくつかの点で近似の精度が低くなる可能性があります。したがって、より高い精度の要件については、他の高次の補間方法または他の近似技術が考慮される場合があります。

アークタン

import math

def linear_interpolation(x, x0, y0, x1, y1):
    """
    线性插值函数
    """
    return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

def approximate_arctan(x, precision):
    """
    使用线性插值逼近arctan函数
    """
    # 定义插值范围
    x0 = -math.pi / 2
    y0 = math.atan(x0)
    x1 = math.pi / 2
    y1 = math.atan(x1)

    # 计算插值步长
    step = (x1 - x0) / precision

    # 找到插值点
    interpolation_point = x0
    while interpolation_point < x:
        interpolation_point += step

    # 进行线性插值
    approximation = linear_interpolation(x, interpolation_point - step, math.atan(interpolation_point - step),
                                         interpolation_point, math.atan(interpolation_point))
    return approximation

# 示例使用
x = 1.0  # 输入值
precision = 1000  # 精度要求,即插值点的个数
approximation = approximate_arctan(x, precision)
print(approximation)

アークタン2

import math

def linear_interpolation(x, x0, y0, x1, y1):
    """
    线性插值函数
    """
    return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

def approximate_arctan2(y, x, precision):
    """
    使用线性插值逼近arctan2函数
    """
    # 计算arctan值
    arctan_yx = math.atan2(y, x)

    # 定义插值范围(通常为[-pi, pi])
    x0 = -math.pi
    y0 = math.atan2(math.sin(x0), math.cos(x0))
    x1 = math.pi
    y1 = math.atan2(math.sin(x1), math.cos(x1))

    # 计算插值步长
    step = (x1 - x0) / precision

    # 找到插值点
    interpolation_point = x0
    while interpolation_point < arctan_yx:
        interpolation_point += step

    # 进行线性插值
    approximation = linear_interpolation(arctan_yx, interpolation_point - step,
                                         math.atan2(math.sin(interpolation_point - step), math.cos(interpolation_point - step)),
                                         interpolation_point,
                                         math.atan2(math.sin(interpolation_point), math.cos(interpolation_point)))
    return approximation

# 示例使用
y = 1.0  # y坐标
x = 1.0  # x坐标
precision = 1000  # 精度要求,即插值点的个数
approximation = approximate_arctan2(y, x, precision)
print(approximation)

おすすめ

転載: blog.csdn.net/weixin_57399429/article/details/130686291