如何使用 python、numpy 和 scipy 积分弧长?

Fai*_*ual 3 python numpy ellipse scipy automatic-ref-counting

在另一个线程上,我看到有人设法使用 mathematica 积分弧长。他们写道:

In[1]:= ArcTan[3.05*Tan[5Pi/18]/2.23]
Out[1]= 1.02051
In[2]:= x=3.05 Cos[t];
In[3]:= y=2.23 Sin[t];
In[4]:= NIntegrate[Sqrt[D[x,t]^2+D[y,t]^2],{t,0,1.02051}]
Out[4]= 2.53143
Run Code Online (Sandbox Code Playgroud)

究竟如何使用 numpy 和 scipy 的导入将其转移到 python 中?特别是,我被他的代码中的第 4 行“NIntegrate”函数困住了。谢谢您的帮助!

另外,如果我已经有了弧长和垂直轴长度,我如何才能反转程序以从已知值中吐出原始参数?谢谢!

yan*_*ros 5

如果您更喜欢纯数值方法,则可以使用以下准系统解决方案。鉴于我有两个输入numpy.ndarrayx并且y没有可用的功能形式,这对我来说效果很好。

import numpy as np

def arclength(x, y, a, b):
    """
    Computes the arclength of the given curve
    defined by (x0, y0), (x1, y1) ... (xn, yn)
    over the provided bounds, `a` and `b`.

    Parameters
    ----------
    x: numpy.ndarray
        The array of x values

    y: numpy.ndarray
        The array of y values corresponding to each value of x

    a: int
        The lower limit to integrate from

    b: int
        The upper limit to integrate to

    Returns
    -------
    numpy.float64
        The arclength of the curve

    """
    bounds = (x >= a) & (y <= b)

    return np.trapz(
        np.sqrt(
            1 + np.gradient(y[bounds], x[bounds])
        ) ** 2),
        x[bounds]
    )
Run Code Online (Sandbox Code Playgroud)

注意:我以这种方式间隔返回变量只是为了使其更具可读性和更清晰地理解正在发生的操作。

顺便说一句,回想一下曲线的弧长由下式给出:

弧长方程