how do I calculate the value of second derivative at a point using python?

2 Answers

Relevance
  • 4 weeks ago

    ffffffffffffffffffffffffffffffff

  • 2 months ago

    I assume you want to estimate the derivative numerically.  You can use the three equally spaced values f(x-h), f(x), f(x+h) for some small value of h>0 to get a value that's usually close enough.

    The usual definition of the derivative is the limit:

        f'(a) = lim h-->0  [f(a + h) - f(a)] / h

    Another limit that works, provided that f'(x) exists at and near x=a, is the symmetrical limit:

        f'(a) = lim h-->0 [f(a + h/2) - f(a - h/2)] / h

    Use that to find a formula for f''(x) at x=a:

        f''(a)  ~~  [f'(a + h/2) - f'(a - h/2)] / h

    Use the same formula to find that:

        f'(a + h/2)  ~~  [f((a + h/2) + h/2) - f((a + h/2) - h/2)] / h

                           =  [f(a + h) - f(a)] / h

        f'(a - h/2)  ~~  [f((a - h/2) + h/2) - f((a - h/2) - h/2)] / h

                           =  [f(a) - f(a - h)] / h

    Plug those two into the estimate for f''(a) above to get:

        f''(a) ~~ [[f(a + h) - f(a)]/h - [f(a) - f(a - h)]/h] /h

                  = [f(a + h) - 2*f(a) + f(a - h)] / h²

    That's the math.  Converting that to Python is pretty easy:

        h = 1e-6

        fpp_at_a = (f(a + h) + f(a - h) - 2*f(a)) / h**2 # f''(a) estimate

Still have questions? Get your answers by asking now.