Re: numerical derivatives at very high x-values: precision problem
- To: mathgroup at smc.vnet.net
- Subject: [mg64036] Re: [mg64021] numerical derivatives at very high x-values: precision problem
- From: "Carl K. Woll" <carlw at wolfram.com>
- Date: Sat, 28 Jan 2006 02:23:54 -0500 (EST)
- References: <200601271013.FAA19073@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Nadir Kaplan wrote: > Hi, > > When calculating partial numerical derivatives, namely the formula [f(x+h, > y)-f(x-h, y) ] / (2h), at very high x values, let's say x~10^12 , I should > choose h=(10^a)*|x| where x should be <-16 in order to get accurate results. > We know that the MachineEpsilon is approx. 2.2*10^(-16) , but I think I > should somehow use a number less than that for h, otherwise the answers are > not satisfiying. How will I do this in order to get high precision (or > accuracy) ? I added SetPrecision command everywhere, but it seems it didn't > work out well, besides it looks dumb (or not? I'm new at Mathematica ). > > > > Any help will be appreciated... > > > > The values are f.e. (from a set of recursion relations) : > > ---------------------------------------------------------------------------- > > J[40]= 1.12970494019605678505771742460*10^12 > > mu[40]= 3.38911605114868836337349826143*10^12 > > V[40]= -8.47278910237611083984375000000*10^11 > > T1[40]= 5.64852470100879638671875000000*10^11 > > > > The function to be partial-differentiated: > > ------------------------------------------------------------ > > lngamma1[J_, mu_, V_, t1_]:= > > Module[{mu2, U1, U2, Umax}, mu2=SetPrecision[mu/(2*d), 30]; > > U1=SetPrecision[0, 30]; > > U2=SetPrecision[1.5*mu2+func1[0.5*mu2, t1], 30]; > > Umax= Max[U1, U2]; > > > SetPrecision[Umax+Log[Exp[U1-Umax]+2*Exp[U2-Umax]*func2[0.5*mu2, t1]], 30] ^^^^^^^^^^^^^^^^ |||||||||||||||| why not just U1-Umax? > > ] > > > > The derivative formula: > > ---------------------------------------------------------------- > > derivt1[func_, J_, mu_, V_, t1_]:= > > Module[ {h}, h=SetPrecision[10^-15*Abs[t1],30] ; > > SetPrecision[(func[J, mu, V, t1+h]-func[J, mu, V, t1-h])/(2*h), 30] > > ] > > First, eliminate all of the SetPrecision statements, they aren't helpful. Next, you don't tell us what func1 and func2 are. The rest of your lngamma1 function can be symbolically differentiated by Mathematica without problems, so unless func1 and func2 are undifferentiable functions, there is no need to use numerical derivatives. If func1 or func2 is symbolically undifferentiable, then you should consider using the ND standard package to do differentiation. It uses Richardson extrapolation to find derivatives, which is much more reliable than the derivative formula you give above. Finally, instead of writing lngamma1 as lngamma1[J_, mu_, V_, t1_]:=... you should consider writing it as lngamma1[J_, mu_, V_][t1_]:=... Carl Woll Wolfram Research
- References:
- numerical derivatives at very high x-values: precision problem
- From: "Nadir Kaplan" <nadir.kaplan@pclabs.gen.tr>
- numerical derivatives at very high x-values: precision problem