Advertisement

多项式拟合即最小二乘法估计系数参数以预测结果值

阅读量:

一、单参数多项式拟合函数

利用多项式拟合函数,确定系数coefficient。points中存储了x与y的对应数据,degree代表拟合的阶数,将其转化为矩阵形式以求解最小二乘问题。

复制代码
 // 多项式拟合函数

    
 VectorXd polyFit(const QVector<QPair<double, double>> &points, int degree) {
    
     int n = points.size();
    
     MatrixXd A(n, degree + 1);
    
     VectorXd b(n);
    
  
    
     for (int i = 0; i < n; ++i) {
    
     double x = points[i].first;
    
     double y = points[i].second;
    
     for (int j = 0; j <= degree; ++j) {
    
         A(i, j) = std::pow(x, j);
    
     }
    
     b(i) = y;
    
     }
    
  
    
     // 打印设计矩阵 A 和目标向量 b
    
     st

全部评论 (0)

还没有任何评论哟~