CoolProp 7.2.0
An open-source fluid property and humid air property database
finite_diff.h
Go to the documentation of this file.
1#include <Eigen/Dense>
2
3template <typename Callable>
4auto romberg_diff(Callable& func, double x, std::size_t order = 2, double h = 0.1) {
5
6 // Initialize the table
7 auto r = Eigen::ArrayXd(order, order);
8
9 // Compute the first column using the central difference formula
10 for (auto i = 0; i < order; ++i) {
11 r(i, 0) = (func(x + h) - func(x - h)) / (2 * h);
12 h /= 2.0;
13 }
14
15 // Apply Richardson extrapolation
16 for (auto i = 1; i < order; ++i) {
17 for (auto j = i; j < order; ++j) {
18 double fouri = pow(4, i);
19 r(j, i) = (fouri * r(j, i - 1) - r(j - 1, i - 1)) / (fouri - 1);
20 }
21 }
22
23 return r(order - 1, order - 1);
24}