Simplify trigonometric and inverse trigonometric functions using linear interpolation

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

sin

In the following example, we first define the interpolation range (from 0 to π/2), and then calculate the interpolation step size according to the accuracy requirement. Then, we find the interpolation point closest to the input value and use the linear interpolation formula to approximate the value of the sin function.

Note that the higher the accuracy requirement (i.e. the number of interpolation points), the closer the approximation will be to the exact value of the sin function at the given input values. However, the approximation may have limited accuracy due to the use of linear interpolation. If higher precision approximation results are required, higher order interpolation methods such as quadratic or cubic interpolation may be considered.

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

In the above example, we define the interpolation range and precision requirements in a similar way, then find the interpolation point closest to the input value, and use the linear interpolation formula to approximate the value of the cos function.

Likewise, the accuracy of the approximation depends on the accuracy requirement (number of interpolated points). If higher precision approximation results are required, higher order interpolation methods such as quadratic or cubic interpolation may be considered.

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

 For the linear interpolation approximation of the tan function, it should be noted that the tan function will diverge at some points (such as multiples of π/2), so the interpolation range needs to exclude these points. Here is the code for an example linear interpolation approximation to the tan function:

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)

arcsin

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)

 

In the above example, we defined the interpolation range (from -1 to 1), and calculated the interpolation step size. Then, find the interpolation point closest to the input value and use the linear interpolation formula to approximate the value of the arcsin function.

Note that due to the nature of the arcsin function, the approximation may be less accurate at some points, so an appropriate interpolation range and accuracy requirement needs to be chosen. If higher accuracy approximation results are required, other higher order interpolation methods or other approximation techniques may be considered.

arccos

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)

 

In the above example, we defined the interpolation range (from -1 to 1), and calculated the interpolation step size. Then, find the interpolation point closest to the input value and use the linear interpolation formula to approximate the value of the arccos function.

Similar to the approximation of other trigonometric functions, due to the nature of the arccos function, the approximation may be less accurate at some points. Therefore, for higher accuracy requirements, other higher order interpolation methods or other approximation techniques may be considered.

arctan

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)

arctan2

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)

Guess you like

Origin blog.csdn.net/weixin_57399429/article/details/130686291