Re: Mixed Numerical Derivatives
- To: mathgroup at smc.vnet.net
- Subject: [mg87164] Re: Mixed Numerical Derivatives
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Thu, 3 Apr 2008 05:13:08 -0500 (EST)
- Organization: University of Bergen
- References: <fsveco$sp7$1@smc.vnet.net> <fsvosj$5at$1@smc.vnet.net>
[note: message sent to comp.soft-sys.math.mathematica]
dh wrote:
> Hi,
>
> I have version 6 and can not find ND.
It is in the NumericalCalculus package.
>
> Nevertheless, you could try an iterative approach. E.g.:
>
> ND[ ND[fun[x,y],{x,2},1] ,{y,2}, 1]
This will appear to work for simple functions, but I would not recommend
it because the inner ND is evaluated /before/ the outer ND, and produces
a "semi-symbolic" result, i.e. something containing both 'y' and
floating point numbers. This might not work at all for complicated
functions that /must/ take numerical arguments, and even for simple
functions ND cannot check for convergence, etc., so it is problematic.
A similar solution is to define one's own "double-derivative" function
which avoids evaluation of the "inner" ND explicitly, e.g.
ND2[expr_, x_, y_, x0_, y0_] := Module[{fun, a},
fun[a_?NumericQ] := ND[expr /. y -> a, x, x0];
ND[fun[a], a, y0]
]
ND2[Sin[x y], x, y, 1, 1]
... but I am still not comfortable with this approach ...
>
> hope this helps, Daniel
>
>
>
> jwmerrill at gmail.com wrote:
>
>> How can I come up with the Hessian of a function, at a particular
>
>> point, which can only be evaluated numerically? If I had a symbolic
>
>> function, I could do something like
>
>
>> In[123]:=
>
>> D[x^3 + z*y^-1 + z^(1/2), {{x, y, z}, 2}] /. {x -> 3, y -> 5,
>
>> z -> 12}
>
>
>> Out[123]= {{18, 0, 0}, {0, 24/
>
>> 125, -1/25}, {0, -1/25, -1/(96 Sqrt[3])}}
>
>
>> The function I'm interested in, though, can only be calculated
>
>> numerically. Using ND, I can find the diagonal elements of the
>
>> Hessian:
>
>
>> In[92]:= rules = Last[
>
>> FindMaximum[{Total[logPr[vdt, ddt, var, #] & /@ testData], ddt > 0,
>
>> var > 0}, {{vdt, .9}, {ddt, 120}, {var, 90}}]]
>
>
>> Out[92]= {vdt -> 0.95945, ddt -> 151.097, var -> 103.255}
>
>
>> In[111]:= Needs["NumericalCalculus`"]
>
>
>> In[124]:= ND[
>
>> Total[logPr[vdtp, ddt, var, #] & /@ testData] /. rules, {vdtp, 2},
>
>> Evaluate[vdt /. rules]]
>
>
>> Out[124]= -64.4011
>
>
>> But what about the off diagonal elements?