首页 > 编程知识 正文

wps怎么拟合直线,端点直线法求拟合直线

时间:2023-05-05 02:01:35 阅读:143434 作者:3574

最wxddr乘问题

通过最小化误差平方和来寻找数据的最佳函数匹配

直线方程式

y=kx b

求未知数k和b

已知点

(x1,y1 )…) xn,yn ) ) )。

如果点没有误差的话。 也就是说,存在解一般方程式的唯一问题

典型的点云分布如下图所示。 PS:此图为失窃图)

要求k和b使偏差的平方之和最小(最大wxddr次幂) ) )。

偏差=y-kx-b

使用强力ceres

构建代价函数

structlineresidual { line residual (doublex,double y ) :x_(x ) x,y_(y ) { } template typename t bool operator ) ) 返回真; }private: const double x_; const double y_; (; residual[0]=y_ - k[0] * x_ - b[0]也就是偏差PS:可能不太熟悉,但应该这样理解

*residual=y_ - *k * x_ - *b; 其余请参阅ceres官方网站文档http://www.ceres-solver.org/nnls _ tutorial.html # hello-world

解释成本函数的这个线性结果该如何定义呢

数据

手动创建数据

设y=x 7

const int kNumObservations=11; constdoubledata [ ]={ 0,7,1,8,2,9,3,10,4.5,11,5,12,6,13,7,14,8,15,9,16,10,17 }; 这样,就有了(0,7 )…) 10,17 )这样的要点。 注: (4.5,11 )因错误而濒临危险。 现在用ceres和这些点反求k和b

构建优化问题

双k=0; //要解的kdouble b=0; //要解的bProblem problem; //定义优化问题for (inti=0; i kNumObservations; I ) /如果使用自动引导将以前的成本函数结构传递给problem.addresidualblock (/autodiffcostfunction ),则后面的几个1将返回输入/输出维//表示k或b残差residual的维数newautodiffcostfunctionlineresidual的1 ) newlineresidual(data[2*I],data[2 * i 1] ),NULL,k,b }简要说明

因为我们只要有11个点就有11个误差项,所以在一个周期内在被定义的problem中追加误差项

data[2 * i],data[2 * i 1]为了依次取得data中的点,将结构体LineResidual初始化

配置求解

solver :选项选项; //可配置的优化选项options.max_num_iterations=25; //最大重复次数options.linear _ solver _ type=ceres 33603360 dense _ QR; //配置增量方程解法options.minimizer _ progress _ to _ stdout=true; 输出到//cout solver :3360 summary summary; //优化信息solve(options,problem,summary ); 求出打印输出结果

STD : cout ' finalk : ' k ' b : ' b 'n '; 可知通过加上点(4.5,11 )这样误差,结果近似于k=1和b=7

大家可以自己参加噪音测试

以下给出完全代码,这是一般人都喜欢的。直接copy,然后调试分析

我在qt下调试了,大家可以自己删除主窗口等相关二维码

# include ' main window.h ' # include qapplication # include ' ceres/ceres.h ' # include ' glog/logging.h ' using ceres using ceres :探测器; using ceres :服务器; using ceres :解决方案; const int kNumObservations=11; constdoubledata [ ]={ 0,7,1,8,2,9,3,10,4.5,11,5,12,6,13,7,14,8,15,9,16,10,17 }; structlineresidual { line residual (doublex,double y ) :x_(x ) x,y_(y ) { } template typename t bool operator ) ) 返回真; }private: const double x_; const double y_; (; intmain(intargv,char *argv[] ) qapplicationa ) argc,argv ); Google :3360 initgooglelogging (argv [0]; 双k=0; 双b=0; 探测器探测器; for(intI=0; i kNumObservations; I ({ problem.addresidualblock (newautodiffcostfunctionlineresidual,1,1 ) newlineresidual(data((2*I,data ) ) ) options.max_num_iterations=25; options.linear _ solver _ type=ceres 33603360 dense _ QR; options.minimizer _ progress _ to _ stdout=true; solver :摘要摘要; solve(options,problem,summary ); STD :3360 cout summary.brief report (() ) (n ); STD : cout ' initialk : ' 0.0 ' b : ' 0.0 'n '; STD : cout ' finalk : ' k ' b : ' b 'n '; 主窗口w; w.show (; return a.exec (; } .pro文件需要关注的问题

首先,您需要自己安装ceres库。 百度哈,我可能已经写了相关博客,大家都在自己找

在ceres上用了eigen库大家也要自己安装

.在pro的最下面放入以下内容

unix: macx 3360 libs=-l/usr/local/lib/- lcereslibs=-l/usr/lib/- LG log-lcholmod-lcx sparse-LG OMP-LBL as -廉价航空公司las

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。