Javascript 求任意多项式函数在x处的正切

Javascript 求任意多项式函数在x处的正切,javascript,python,math,polynomial-math,calculus,Javascript,Python,Math,Polynomial Math,Calculus,问题: function findTangent(x, coefficients) { // Do differential calculus here. return [tangentIntercept, tangentSlope] } function findTangent(x, coefficients) { let slope = 0 let intercept = coefficients[0] for (let i = 1; i < coeff

问题:

function findTangent(x, coefficients) {

  // Do differential calculus here.

  return [tangentIntercept, tangentSlope]

}
function findTangent(x, coefficients) {

  let slope = 0
  let intercept = coefficients[0]

  for (let i = 1; i < coefficients.length; i++) {

    slope += coefficients[i] * i * Math.pow(x, i - 1)
    intercept += coefficients[i] * Math.pow(x, i)

  }

  return [intercept - slope * x, slope]

}
def find_tangent(x, coefficients):

    slope = 0
    intercept = coefficients[0]

    for i, coefficient in enumerate(coefficients):

        if i != 0:

            slope += coefficient * i * pow(x, i - 1)
            intercept += coefficient * pow(x, i)

    return [intercept - slope * x, slope]
我在寻找一个包罗万象的函数,我可以用它来计算任何多项式函数在x的切线。我对所使用的语言漠不关心,尽管JavaScript或Python更受欢迎!我应该能够以a+bx+cx^2+dx^3的格式传入任意x值和系数数组。。。等等

函数格式示例:

function findTangent(x, coefficients) {

  // Do differential calculus here.

  return [tangentIntercept, tangentSlope]

}
function findTangent(x, coefficients) {

  let slope = 0
  let intercept = coefficients[0]

  for (let i = 1; i < coefficients.length; i++) {

    slope += coefficients[i] * i * Math.pow(x, i - 1)
    intercept += coefficients[i] * Math.pow(x, i)

  }

  return [intercept - slope * x, slope]

}
def find_tangent(x, coefficients):

    slope = 0
    intercept = coefficients[0]

    for i, coefficient in enumerate(coefficients):

        if i != 0:

            slope += coefficient * i * pow(x, i - 1)
            intercept += coefficient * pow(x, i)

    return [intercept - slope * x, slope]
功能测试示例:

function findTangent(x, coefficients) {

  // Do differential calculus here.

  return [tangentIntercept, tangentSlope]

}
function findTangent(x, coefficients) {

  let slope = 0
  let intercept = coefficients[0]

  for (let i = 1; i < coefficients.length; i++) {

    slope += coefficients[i] * i * Math.pow(x, i - 1)
    intercept += coefficients[i] * Math.pow(x, i)

  }

  return [intercept - slope * x, slope]

}
def find_tangent(x, coefficients):

    slope = 0
    intercept = coefficients[0]

    for i, coefficient in enumerate(coefficients):

        if i != 0:

            slope += coefficient * i * pow(x, i - 1)
            intercept += coefficient * pow(x, i)

    return [intercept - slope * x, slope]
假设我有一个函数,y=2+7x+5x^2+x^3,我想找到x=-2处的切线。我可以像这样调用这个函数,
findTangent(-2,[2,7,5,1])
并得到这样的返回值,
[-2,-1]
表示切线,y=-2-x

注意事项:

function findTangent(x, coefficients) {

  // Do differential calculus here.

  return [tangentIntercept, tangentSlope]

}
function findTangent(x, coefficients) {

  let slope = 0
  let intercept = coefficients[0]

  for (let i = 1; i < coefficients.length; i++) {

    slope += coefficients[i] * i * Math.pow(x, i - 1)
    intercept += coefficients[i] * Math.pow(x, i)

  }

  return [intercept - slope * x, slope]

}
def find_tangent(x, coefficients):

    slope = 0
    intercept = coefficients[0]

    for i, coefficient in enumerate(coefficients):

        if i != 0:

            slope += coefficient * i * pow(x, i - 1)
            intercept += coefficient * pow(x, i)

    return [intercept - slope * x, slope]

我在和谷歌搜索中寻找答案,但所有结果都是数学语法,而不是代码。我想要一个程序化的解决方案,比起有趣的符号和数学术语,我更喜欢循环和if语句

好吧,经过一天的努力,我想我已经用JavaScript和Python找到了解决方案

function findTangent(x, coefficients) {

  // Do differential calculus here.

  return [tangentIntercept, tangentSlope]

}
function findTangent(x, coefficients) {

  let slope = 0
  let intercept = coefficients[0]

  for (let i = 1; i < coefficients.length; i++) {

    slope += coefficients[i] * i * Math.pow(x, i - 1)
    intercept += coefficients[i] * Math.pow(x, i)

  }

  return [intercept - slope * x, slope]

}
def find_tangent(x, coefficients):

    slope = 0
    intercept = coefficients[0]

    for i, coefficient in enumerate(coefficients):

        if i != 0:

            slope += coefficient * i * pow(x, i - 1)
            intercept += coefficient * pow(x, i)

    return [intercept - slope * x, slope]
我已经针对测试结果进行了测试,结果似乎还可以,但是如果发现任何错误,请告诉我
此外,我希望看到其他语言的结果,因此,如果您有一种此处未提及的首选语言,请毫不犹豫地发布

使用允许符号区分的Python

代码

from sympy import Function, Symbol

def polynomial(coeficents, degrees, x):
    '''
        Evaluate polynomial
        
        Example
            coefficients = [1, 2, 4]
            degrees = [2, 1, 0]
            
            corresponds to polynomial x^2 + 2*x + 4
    '''
    return sum([coeficents[i]*x**degrees[i] for i in range(len(coeficents))])
         
# Using OP polynomial
coefficients = [1, 5, 7, 2]
degrees = [3, 2, 1, 0]
print(polynomial(coefficients, degrees, -2))  # Output: 15

# Create symbolic polynomial in x
# Define symbolic variable x
x = Symbol('x')  # symbolic variable x

# Create polynomial in x for OP polynomial
poly = polynomial(coefficients, degrees, x)
print(poly)                                  # Output: x**3 + 5*x**2 + 7*x + 2
# Evaluate at x = -2
print(poly.subs(x, -2))                      # Output: 7  (i.e. substitute x for 1 in equation)

####################################################
# Symbolic differentiation of polynomial 'poly'
####################################################
diff_poly = poly.diff(x)
print(diff_poly)                             # Output: 3*x**2 + 10*x + 7 (derivative of polynomial)
# Evaluate derivative at x = -2
print(diff_poly.subs(x, -2))                 # Output: -1   (derivate at x = -1)

因为您询问了其他语言,这里有一个C函数来计算多项式在某一点的导数(和值)。我推荐的方法可以用在任何语言中

double  pol_eval_d( double x, int deg, const double* c, double* pdp)
{
double  p = c[deg];
double  dp = 0.0;
    for( int d=deg-1; d>=0; --d)
    {   dp = fma( dp, x, p);
        p = fma( p, x, c[d]);
    }
    *pdp = dp;
    return p;
}
此函数获取多项式的x值、阶数和系数,并返回多项式在x处的值以及其导数在*pdp中的值

系数为c[0](幂0),c[1](幂1)。。和c[deg](功率deg)

它调用(C数学库)函数fma,用于

fma(x,y,z) = x*y+z, except that it is evaluated with extra precision.
如果你没有这样的函数,你可以用上面的表达式替换调用,尽管你会失去一点准确性

使用的方法是霍纳方法。这通常比计算多项式的其他方法更快、更准确

解释它是如何工作的,首先考虑不需要导数的情况。然后就可以写了

double  pol_eval( double x, int deg, const double* c)
{
double  p = c[deg];
    for( int d=deg-1; d>=0; --d)
    {   p = fma( p, x, c[d]);
    }
    return p;
}
如果我们一步一步地通过二次函数,用它们的数学等价物替换fma调用,我们得到

p = c[2]
p = p*x + c[1]
p = p*x + c[0]
这就是我们所评估的

c[0]+x*c[1]+x*x*c[2] 

这是霍纳的方法

为了计算导数,我们对pol__eval中的每个项进行了区分。最初p是一个常数,所以它的导数是0。然后当我们用

p = fma( p, x, c[d]);
或者用数学术语

p = p*x + c[d];
我们用乘积法则来区分,因为c[d]是一个常数

dp = dp*x + p

请注意,我们必须在更新p之前执行此操作。

“我对循环和if语句比有趣的符号和数学术语更熟悉!”-哈哈。可能
Array.prototype.reduce()
,因为这看起来更酷!:-)看起来非常棒-完全符合你的公式。谢谢@RandyCasburn,我确实在某些时候拔出了头发,但我认为这是我能想到的最优雅、最准确的解决方案。好吧,我以前没有用过sympy!谢谢你!太棒了,非常感谢!我个人从来没有写过任何C,但这看起来不错!