繁体   English   中英

本征C ++中线性代数方程Ax = By的最小二乘解

[英]Least Squares Solution of Linear Algerbraic Equation Ax = By in Eigen C++

我有一组矩阵形式的线性代数方程Ax = By 其中A36x20矩阵, x是大小为20的向量, B36x13y13x1 等级(A)= 20 因为系统是超定的(方程组的数量多于变量),所以最小二乘解是可能的,即 x =(A ^ TA)^-1A ^ TBy 我想要解决方案,以便应将残留误差e = Ax-By最小化。

使用C ++的Eigen/Dense库,我已经制定了所有矩阵等。我尝试了本页Eigen Tutorial中描述的方法!

我猜本页描述的方法仅适用于平方矩阵。 因为当它尝试运行时会给出错误。

 x = A.jacobiSvd( ComputeThinU | ComputeThinV ).solve(B*y);

错误

 /usr/include/eigen3/Eigen/src/SVD/JacobiSVD.h: In member function 'const    
 Eigen::internal::solve_retval<Eigen::JacobiSVD<MatrixType, QRPreconditioner>, Rhs> 
 Eigen::JacobiSVD<MatrixType, QRPreconditioner>::solve(const 
 Eigen::MatrixBase<OtherDerived>&) const [with Rhs = 
 Eigen::GeneralProduct<Eigen::Matrix<float, 36, 13>, Eigen::Matrix<double, -1, 1>, 4>; 
 _MatrixType = Eigen::Matrix<float, 36, 20>; int QRPreconditioner = 2]':
 /usr/include/eigen3/Eigen/src/SVD/JacobiSVD.h:658:5: warning: control reaches end of  
 non-void function [-Wreturn-type]
 make[2]: *** [src/CMakeFiles/spacebot_actuationKinematics.dir 
 /ActuationKinematics.cpp.o] Error 1
 make[1]: *** [src/CMakeFiles/spacebot_actuationKinematics.dir/all] Error 2
 make: *** [all] Error 2

看来您的solve(B * y)中的矩阵乘法有问题; 部分。 尝试单独执行B * y并使用solve(result); 代替。

Eigen::GeneralProduct<Eigen::Matrix<float, 36, 13>, Eigen::Matrix<double, -1, 1>, 4>

那条线给了我这种怀疑。 它说y变量的大小为-1x1,因此您的程序无论如何都不会运行,因为它无法与矩阵相乘。

另外,本教程说...

A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << endl;

我不知道Eigen是如何工作的,但这似乎是问题所在。

如文档中所述, ComputeThin*选项仅适用于Dynamic大小的矩阵。 对于固定大小,必须使用ComputeFull* 不过,在您的情况下,最好使用Dynamic大小矩阵,即MatrixXf 固定大小的矩阵仅对非常小的矩阵有意义。

最后,对于最小二乘求解,ColPivHouseholderQR可能是更好的选择。 SVD有点矫kill过正。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM