Re: Simple Question on Derivatives



In article <ef100ad.-1@xxxxxxxxxxxxxxxx>, "J Dagg"
<james.dagg@xxxxxxxxxxxx> wrote:

> Can I safely compute the second derivative of a function by using
> diff(y,2)./diff(x,2)? If not, what's the simplest way to do it?
-------------------
No James, all this gives you, at best, is the ratio between two second
derivatives, and moreover you haven't specified what variable you are
differentiating with respect to. If x and y are symbolic variables, and
you are differentiating with respect to x, you will be dividing by zero.
What you need is just the numerator with y being expressed explicitly in
terms of x.

If, on the other hand, x and y are discrete numerical arrays, it still
doesn't give you what you want. If the x values are evenly spaced, you
will again be dividing by zero. If x and y are both closely spaced in
accordance with some, possibly hypothetical, third variable which is
itself evenly spaced, then the formula you should use for the second
derivative of y with respect to x is:

(diff(x).*diff(y,2) - diff(y).*diff(x,2))./diff(x).^3

This comes from the famous chain rule in calculus. Bear in mind that even
this is only an approximation depending on how close the spacing is, not
to mention on the accuracy of the x and y values. The requirement that
the third variable be evenly spaced is essential in this case. Otherwise
second differences do not correspond to any kind of second derivative with
respect to anything.

This is one level of differentiation beyond the formula for a first
derivative of y with respect to x:

diff(y)./diff(x)

again if both x and y are spaced according to some closely spaced third
variable. In this case the requirement of even spacing for the third
variable can be relaxed to merely close spacing.

The point is that 'diff(x)' applied to a discrete array of numbers does
not by itself give a derivative for the numbers. It's only a first
difference. Similarly 'diff((),2)' is only a second difference and is
possibly even farther from being anything like a second derivative.

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
.



Relevant Pages

  • Re: integration + Levenberg-Marquardt (Numerical Recipes)
    ... I am having problems getting my fitting function>derivatives correct ... The integration is correct when I test the resulting>data but I don't ... This follows by differentiating the equation ...
    (sci.math.num-analysis)
  • Re: Linear operators 3
    ... Why are you differentiating? ... You are given TWO FUNCTIONS f and g, with domain, and codomain ... you are going to use derivatives; to check that your proposed h also ... would recommend you quit giving me a hard time and quit your position ...
    (sci.math)
  • Re: Linear operators 3
    ... Do you know any way by which, given a CONTINUOUS function f, you can ... Why are you differentiating? ... You are given TWO FUNCTIONS f and g, with domain, and codomain ... you are going to use derivatives; to check that your proposed h also ...
    (sci.math)
  • Re: Evaulate an arithmetic expression
    ... Your derivative code isn't evaluating ... In other words, differentiating ... derivative expression that we need concern ourselves with what X ... in your derivatives. ...
    (comp.lang.lisp)

Loading