日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

求解非线性最小二乘法 Eigen

發布時間:2024/1/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 求解非线性最小二乘法 Eigen 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// 利用Eigen 求解非線性最小二乘; // 示例:<span style="font-family: Arial, Helvetica, sans-serif;">y = 10*(x0+3)^2 + (x1-5)^2</span><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;">#include "math.h"</span> #include "iostream" #include "vector" #include "list"using namespace std;#include "Eigen/Dense" #include "Eigen/Core" #include <unsupported/Eigen/NonLinearOptimization> #include <unsupported/Eigen/NumericalDiff>using namespace Eigen;


// Generic functor template<typename _Scalar, int NX = Eigen::Dynamic, int NY = Eigen::Dynamic> struct Functor {typedef _Scalar Scalar;enum {InputsAtCompileTime = NX,ValuesAtCompileTime = NY};typedef Eigen::Matrix<Scalar,InputsAtCompileTime,1> InputType;typedef Eigen::Matrix<Scalar,ValuesAtCompileTime,1> ValueType;typedef Eigen::Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime> JacobianType;int m_inputs, m_values;Functor() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}Functor(int inputs, int values) : m_inputs(inputs), m_values(values) {}int inputs() const { return m_inputs; }int values() const { return m_values; }};


struct my_functor : Functor<double> {// 輸出個數必須大于輸入個數, 故用2不用1;my_functor(void): Functor<double>(2, 2) {}int operator()(const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const{// Implement y = 10*(x0+3)^2 + (x1-5)^2fvec(0) = 10.0*pow(x(0)+3.0,2) + pow(x(1)-5.0,2);fvec(1) = 0;return 0;} };
int main(int argc, char *argv[]) {Eigen::VectorXd x(2);x(0) = 1.0;x(1) = 3.0;my_functor functor;Eigen::NumericalDiff<my_functor> numDiff(functor);Eigen::LevenbergMarquardt<Eigen::NumericalDiff<my_functor>,double> lm(numDiff);Eigen::VectorXd y(2);functor.operator()(x, y);std::cout << "x first input: \n" << x << std::endl;std::cout<<"y first outpout: \n" << y << std::endl;lm.parameters.maxfev = 1000;lm.parameters.xtol = 1.0e-8;int iRet = lm.minimize(x);std::cout << "迭代次數:\n"<<lm.iter << std::endl;std::cout << "計算標志:\n" << iRet << std::endl;std::cout << "x finnal: \n" << x << std::endl;functor.operator()(x, y);std::cout<<"y outpout((minimized): \n" << y << std::endl;return 0; }

// 最終看到輸出了x = [-3.0, 5.0]. 使得目標最小!


總結

以上是生活随笔為你收集整理的求解非线性最小二乘法 Eigen的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。