Jonny_92
Jonny_92

Reputation: 159

How can I estimate the first derivative of an evaluated univariate function in C++?

I have a two vectors:

and I want to compute an estimate of the first derivative of f at these coordinates.

The function is a descriptor of a wavefunction and x is the dihedral angle.

Because the result vector must have the same length as the two existing vectors, I cannot use a manual implementation based on Newton's difference quotient.

In Python, I can obtain such an estimate using the scipy library:

spline = UnivariateSpline(X, Y, k=4, s=0)
sd = spline.derivative(n=1)

Perhaps I can do something similar in C++?

Upvotes: 3

Views: 447

Answers (1)

user14717
user14717

Reputation: 5161

I'd start with pchip.

Example:

#include <boost/math/interpolators/pchip.hpp>
// ...
using boost::math::interpolators::pchip;
auto f = pchip(std::move(x), std::move(y));
double t = 3.2;
std::cout << "f(" << t << " = " << f(t) << ", f'(" << t << ") = " << f.prime(t) << "\n";

If you don't like the "character" of pchip, then you have many other options.

Upvotes: 2

Related Questions