Differentiation in Python

Z

zingbagbamark

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
 
S

Steven D'Aprano

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.
 
D

David Robinow

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
for free.
"""
import numpy as np
p = np.poly1d(coeffs)
deriv_np = np.polyder(p)
print(deriv_np)
# or
print(list(deriv_np))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top