Author: Not specified Language: cpp
Description: Not specified Timestamp: 2018-07-06 08:20:56 +0000
View raw paste Reply
  1. void polyfit(const std::vector<double> &xv, const std::vector<double> &yv, std::vector<double> &coeff, int order)
  2. {
  3.     Eigen::MatrixXd A(xv.size(), order+1);
  4.     Eigen::VectorXd yv_mapped = Eigen::VectorXd::Map(&yv.front(), yv.size());
  5.     Eigen::VectorXd result;
  6.  
  7.     assert(xv.size() == yv.size());
  8.     assert(xv.size() >= order+1);
  9.  
  10.  
  11.     for (size_t i = 0; i < xv.size(); i++)
  12.         for (size_t j = 0; j < order+1; j++)
  13.             A(i, j) = pow(xv.at(i), j);
  14.  
  15.  
  16.     result = A.householderQr().solve(yv_mapped);
  17.  
  18.     coeff.resize(order+1);
  19.     for (size_t i = 0; i < order+1; i++)
  20.         coeff[i] = result[i];
  21. }
View raw paste Reply