From help-octave-request at bevo dot che dot wisc dot edu Tue Jan 19 17:37:57 1999 Subject: RE: diff(x) question From: George White To: Van den Eynde Gert cc: help-octave at bevo dot che dot wisc dot edu Date: Tue, 19 Jan 1999 19:37:05 -0400 (AST) On Tue, 19 Jan 1999, Van den Eynde Gert wrote: > > Diff computes forward differences. Not the derivative. If you want to > > approximate the derivative by using forward differences you have to > > divide by the step h with which you make your function discrete. > > In this case h = .1 > > > > > > df f(t+h) - f(t) > > -- (t) =~ --------------- + O(h) > > dt h > > > > just want to update my previous answer. > > I suggest to use a symmetrical formula > > (f(t+h) - f(t-h))/(2h) > > this gives an error of O(h^2) AND you can use Richardson extrapolation > (extrapolation to the limit). IMHO, this is a good way to compute a > numerical derivative. > > Gert Van den Eynde Neither of the above is appropriate for machine arithmetic. For example, the first formula above should replace "h" with "h2" as follows: tmp=t;t = t+h;h2 = t-tmp;t = tmp; This is necessary since t+h will not, in general, be representable in machine arithmetic. The assignment of t to tmp is intended to remind the reader that optimizing compilers may simplify away the necessary distinction. What does octave do with: h2=(t+h)-t; and will future version do the same? -- George White Halifax, Nova Scotia