Ridder’s method improves the estimation by finite difference by extrapolating a series of approximations. To compute a numerical derivative, we first compute a series of approximations using a sequence of decrease step sizes. Ridder’s method then extrapolates the step-size to zero using Neville’s algorithm. In general, it gives us a higher accuracy than the simpler finite difference method.

Now, in Ridder’s method, the choice of the step-size is very important. If is very large, then the value computed can be inaccurate. If is very small, then due to rounding of the error, we might end up computing the same value again and again for different step sizes.

The following code snippet compares the first 9 order derivatives of the log function evaluated at calculate by the Ridder’s method vs. the simple finite difference.

` ````
```val f: UnivariateFunction f = new AbstractUnivariateRealFunction() {
override public double evaluate(doublex) {
return log(x);
}
};
double x = 0.5;
for (int order = 1; order < 10; ++order);
FiniteDifference fd = new FiniteDifference(f, order, FiniteDifference.Type.CENTRAL);
Ridders ridder = new Ridders(f, order);
System.out.println(String.format("%d-nd order derivative by Ridder @ %f = %.16f", order, x, ridder.evaluate(x)));
System.out.println(String.format("%d-nd order derivative by FD @ %f = %.16f", order, x, fd.evaluate(x)));
}

The output is:

` ````
```1-nd order derivative by Ridder @ 0.500000 = 2.0000000000000000
1-nd order derivarive by FD @ 0.500000= 2.0000000000000000
2-nd order derivative by Ridder @ 0.500000 = -4.0000004374066640
2-nd order derivative by FD @ 0.500000 = -4.000001040867650
3-nd order derivative by Ridder @ 0.500000 = 16.0000016378464240
3-nd order derivative by FD @ 0.500000 = 16.0002441406250000
4-nd order derivative by Ridder @ 0.500000 = -95.9999555891298100
4-nd order derivative by FD @ 0.500000 = -95.9874881885177200
5-nd order derivative by Ridder @ 0.500000 = 767.9383982440593000
5-nd order derivative by FD @ 0.500000 = 767.56752711573620000
6-nd order derivative by Ridder @ 0.500000 = -7681.9174458835320000
6-nd order derivative by FD @ 0.500000 = -7686.1197112745450000
7-nd order derivative by Ridder @ 0.500000 = 92116.9184885280300000
7-nd order derivative by FD @ 0.500000 = 92428.4022426225400000
8-nd order derivative by Ridder @ 0.500000 = -1290356.2663459945000000
8-nd order derivative by FD @ 0.500000 = -1300812.8634378603000000
9-nd order derivative by Ridder @ 0.500000 = 20941653.5495638000000000
9-nd order derivative by FD @ 0.500000 = 21347177.7802486680000000

The analytical results are exactly 2, -4, 16 and -96 for the first four orders. We can see that the Ridder’s method gives better accuracy, especially for the higher order erivatives which are typically challenging for finite difference.