Example
> f:=x->ln(cos(x));
> df:=D(f):ddf:=D(df);
> plot(ddf,-1..1);
So the second derivative is bounded by 3.5. Let us now apply the forward-difference formula to estimate the derivative at
:
> for i from 1 to 5 do h:=10.^(-i): df1:=(f(0.4+h)-f(0.4))/h: maxerr:=h*3.5/2: acterr:=df1-df(0.4): print(h,df1,maxerr,acterr); od:
A good estimate is obtained for sufficiently small values of
. But the trend does not continue much longer:
> for i from 5 to 10 do h:=10.^(-i): df1:=(f(0.4+h)-f(0.4))/h: maxerr:=h*3.5/2: acterr:=df1-df(0.4): print(h,df1,maxerr,acterr); od:
This is caused by round-off error. Generally, there will be some optimal value for
.