Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Differentiation in Python

Reply
Thread Tools

Differentiation in Python

 
 
zingbagbamark@gmail.com
Guest
Posts: n/a
 
      03-28-2013
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

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-28-2013
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
 
Reply With Quote
 
 
 
 
David Robinow
Guest
Posts: n/a
 
      03-28-2013
On Thu, Mar 28, 2013 at 8:17 AM, <(E-Mail Removed)> 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[i]*(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))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ANN: OpenOpt 0.25 - free Python-written numerical optimizationframework with automatic differentiation dmitrey Python 0 09-15-2009 01:45 PM
How to implement differentiation in C++ komminenibalaji@gmail.com C++ 2 10-20-2008 07:47 PM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Martin v. Lwis Python 0 03-01-2008 10:51 PM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Paul Moore Python 0 03-01-2008 10:39 PM
Searching comp.lang.python/python-list@python.org (was: UTF-8) skip@pobox.com Python 0 03-10-2007 02:50 PM



Advertisments