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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    for free.
    """
    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
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,442
    eman hanoun
    Oct 20, 2008
  2. Replies:
    0
    Views:
    724
  3. Paul Moore
    Replies:
    0
    Views:
    597
    Paul Moore
    Mar 1, 2008
  4. Martin v. Löwis
    Replies:
    0
    Views:
    637
    Martin v. Löwis
    Mar 1, 2008
  5. Senthil Kumaran
    Replies:
    0
    Views:
    558
    Senthil Kumaran
    Jan 17, 2011
Loading...

Share This Page