📜  如何使用Numpy计算导数?

📅  最后修改于: 2022-05-13 01:54:22.368000             🧑  作者: Mango

如何使用Numpy计算导数?

在本文中,我们将学习如何使用 NumPy 计算导数。通常,NumPy 不提供任何稳健的函数来计算不同多项式的导数。但是,NumPy 可以使用函数 numpy.poly1d() 和 deriv() 计算一维多项式的特殊情况。

使用的功能:

  • poly1d():有助于定义多项式表达式或函数。
  • deriv():计算并给出导数表达式

方法:

  • 首先,我们需要定义使用numpy.poly1d()函数的多项式函数。
  • 然后我们需要使用derive()函数来导出导数表达式。
  • 最后,我们可以将所需的值赋给 x 以数值计算导数。

下面是我们使用 NumPy 计算某些表达式的导数的一些示例。在这里,我们采用变量 'var' 中的表达式,并根据 'x' 对其进行区分。

示例 1:

Python3
import numpy as np
  
# defining polynomial function
var = np.poly1d([1, 0, 1])
print("Polynomial function, f(x):\n", var)
  
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=", derivative)
  
# calculates the derivative of after 
# given value of x
print("When x=5  f(x)'=", derivative(5))


Python3
import numpy as np
  
# defining polynomial function
var = np.poly1d([4, 9, 5, 1, 6])
print("Polynomial function, f(x):\n", var)
  
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=\n", derivative)
  
# calculates the derivative of after 
# given value of x
print("When x=3  f(x)'=", derivative(3))


Python3
import numpy as np
  
# defining polynomial function
var = np.poly1d([5, 4, 9, 5, 1, 6])
print("Polynomial function:\n", var)
  
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=\n", derivative)
  
# calculates the derivative of after 
# given value of x
print("When x=2  f(x)'=", derivative(0.2))


Python3
import numpy as np
  
# defining polynomial function
var = np.poly1d([3, 5, 4, 9, 5, 1, 6])
print("Polynomial function:\n", var)
  
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=\n", derivative)
  
# calculates the derivative of after 
# given value of x
print("When x=1  f(x)'=", derivative(1))
derivative1 = derivative.deriv()
  
print("\n\nDerivative, f(x)''=\n", derivative1)
print("When x=1  f(x)'=", derivative1(1))


输出:



示例 2:

蟒蛇3

import numpy as np
  
# defining polynomial function
var = np.poly1d([4, 9, 5, 1, 6])
print("Polynomial function, f(x):\n", var)
  
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=\n", derivative)
  
# calculates the derivative of after 
# given value of x
print("When x=3  f(x)'=", derivative(3))

输出:

示例 3:

蟒蛇3

import numpy as np
  
# defining polynomial function
var = np.poly1d([5, 4, 9, 5, 1, 6])
print("Polynomial function:\n", var)
  
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=\n", derivative)
  
# calculates the derivative of after 
# given value of x
print("When x=2  f(x)'=", derivative(0.2))

输出:



要计算双导数,我们可以简单地使用 deriv()函数两次。

示例 4:

蟒蛇3

import numpy as np
  
# defining polynomial function
var = np.poly1d([3, 5, 4, 9, 5, 1, 6])
print("Polynomial function:\n", var)
  
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=\n", derivative)
  
# calculates the derivative of after 
# given value of x
print("When x=1  f(x)'=", derivative(1))
derivative1 = derivative.deriv()
  
print("\n\nDerivative, f(x)''=\n", derivative1)
print("When x=1  f(x)'=", derivative1(1))

输出: