Stefan Ram <
r...@zedat.fu-berlin.de> schrieb:
> Thomas Koenig <
tko...@netcologne.de> writes:
>>With IFs, it is really hard - what should the derivative of
>> if (x > 0.1) then
>> foo = 1+x
>> else
>> foo = -1-x
>> end if
>>be?
>
> There is /numeric/ and /symbolic/ differentiation.
>
> Numerically, one can easily get an approximation for anything
> as a difference quotient for a small difference, but needs
> to take care, because some results might not be correct
> (when the function changes too fast or is not differentiable
> at that point).
>
> Symbolically, one can also differentiate anything by taking
> the steps a mathematician would take for manual symbolic
> differentiation. Since FORTRAN was designed for numerical
> mathematics, writing symbolic differentiation in FORTRAN
> might be a tad more difficult than in a language like LISP
> that was made for list processing. What is more difficult
> actually is /simplifying/ the raw result of the symbolic
> differentiation to some reasonable and human-readable
> expression (in the case of large and complex expressions).
>
> Now, to your question: My first idea would be
>
> if (x > 0.1) then
> result = 1
> else
> result = -1
> end if
>
> as a first coarse approximation. (Some thoughts about
> the behavior at exactly x=0.1 might have to be added.)
There is no "exactly x=0.1" in binary floating point (and I'm not
sure that even a single Fortran compiler supports decimal floating
point, despite radix being present in selected_real_kind), which
is why I chose that particular number, to add a bit of difficulty.
However, when you differentiate, there are usually certain
assumptions, which are violated in this case - people sort of expect
that integrating what you differentiated gets the same result
(plus a constant). Since there is no way to specify the Dirac
delta function (ok, so it's a distribution) in floating point,
that's not given.
Or people my want to use it for something useful like evaluating
sensitivies, or for root-finding, or... all of that is likely
to fall down in the face of discontinuity.