# Differentiation in Python

Discussion in 'Python' started by zingbagbamark@gmail.com, Mar 28, 2013.

1. ### Guest

How do I differentiate(first order and 2nd order) the following equations in python. I want to differentiate C wrt Q.

C = (Q**3)-15*(Q**2)+ 93*Q + 100

, Mar 28, 2013

2. ### Steven D'ApranoGuest

On Thu, 28 Mar 2013 05:17:36 -0700, zingbagbamark wrote:

> How do I differentiate(first order and 2nd order) the following
> equations in python. I want to differentiate C wrt Q.
>
> C = (Q**3)-15*(Q**2)+ 93*Q + 100

For such a simple case, you don't do it with Python, you do it with
maths, and get an exact result.

dC/dQ = 3*Q**2 - 30*Q + 93

dÂ²C/dQÂ² = 6*Q - 30

The rule for differentiating polynomials of the form

y = k*x**n

is:

dy/dx = (k*n)*x**(n-1)

Consult a good calculus text book or on-line site for further details.

--
Steven

Steven D'Aprano, Mar 28, 2013

3. ### David RobinowGuest

On Thu, Mar 28, 2013 at 8:17 AM, <> wrote:
> How do I differentiate(first order and 2nd order) the following equations in python. I want to differentiate C wrt Q.
>
> C = (Q**3)-15*(Q**2)+ 93*Q + 100

"""
Years ago, when I actually worked for a living, I would have
done something like this:
"""
coeffs = [1, -15, 93, 100]
num_coeffs = len(coeffs)-1
deriv = [coeffs*(num_coeffs-i) for i in range(num_coeffs)]
print(deriv)

"""
The above is somewhat obscure and requires one to add
some documentation. Who wants to do that?

Below is a version using numpy. You get the numpy docs
"""
import numpy as np
p = np.poly1d(coeffs)
deriv_np = np.polyder(p)
print(deriv_np)
# or
print(list(deriv_np))

David Robinow, Mar 28, 2013