29PolyFit(
const Eigen::MatrixBase<Derived> & x,
const Eigen::MatrixBase<Derived> & y,
const unsigned int polyorder)
31 using Scalar =
typename Derived::PlainObject::Scalar;
32 const int n = x.size();
33 const int m = polyorder + 1;
35 if(x.size() != y.size()) {
36 throw std::invalid_argument(
"PolyFit(): x and y must have the same size.");
40 throw std::invalid_argument(
"PolyFit(): polyorder is too high for the number of points.");
43 Eigen::MatrixX<Scalar> A(n, m);
45 for(
int i = 0; i < n; ++i) {
48 for(
int j = 0; j < m; ++j) {
54 return (A.transpose() * A).ldlt().solve(A.transpose() * y);
const Derived::PlainObject PolyFit(const Eigen::MatrixBase< Derived > &x, const Eigen::MatrixBase< Derived > &y, const unsigned int polyorder)
Fits a polynomial of a specified order to the given data points.