numpy.polyder() in Python with Examples
Hello Learners, today we are going to learn about numpy.polyder method in Python with examples. Before diving into code let’s give you a brief overview of NumPy and polynomials.
NumPy and Polynomials
It is a Python library that has various high-level mathematical functions to deal with the mathematical operations in Python. polyder is one of those methods, It is used to find the derivatives of polynomials. But Another question arises here is how to create a polynomial in Python. For that, we have another method from numpy that is poly1d.
import numpy as np poly1 = np.poly1d([2,5,8]) poly2 = np.poly1d([6,1]) poly3 = np.poly1d([9,8,3,0,7]) print ("Polynomial 1 --> \n", poly1) print ("\nPolynomial 2 --> \n", poly2) print ("\nPolynomial 3 --> \n", poly3)
Polynomial 1 --> 2 2 x + 5 x + 8 Polynomial 2 --> 6 x + 1 Polynomial 3 --> 4 3 2 9 x + 8 x + 3 x + 7
The poly1d method takes a list of arguments in the descending order of coefficients of x. Suppose you have passed 3 arguments in the list. So the first number is the coefficient of x to the power raised to 2. The next number will be the coefficient of x to the power raised to 1 and the last number represents the x coefficient to the power raised to 0.
Now according to the differentiation rules, to differentiate these polynomials we use polyder method. If you don’t know the basic maths rule for differentiation I would suggest you learn the basic rules of differentiation. It would hardly take up to one to two hours.
This method takes two arguments as follows:
- The first argument is any polynomial.
- The second argument is the degree of differentiation or the order of derivative.
print("\nDerivative of Polynomial1 =", np.polyder(poly1,1))) print("Derivative of Polynomial2 =", np.polyder(poly2,1)) print("Derivative of Polynomial3 =", np.polyder(poly3,2))
Derivative of Polynomial1 = 4 x + 5 Derivative of Polynomial2 = 6 Derivative of Polynomial3 = 2 108 x + 48 x + 6
Let’s examine the above output. See the polynomial1, we have passed the order as 1 or 2 here, which means:
dy/dx of polynomial 1 dy/dx of polynomial 2 d2y/dx2 of polynomial 3
You saw how the magic of NumPy makes your calculus easy. Now you can try it on your own and see how the code works, play with the method, it’s fun!