最小二乘法拟合直线 c++程序
2009-05-21 20:53
399 查看
//point.h
class Point //Point类的声明
{
public: //外部接口
Point(float xx=0, float yy=0) {X=xx;Y=yy;}
float GetX() {return X;}
float GetY() {return Y;}
friend float linefit(Point l_point[], int n_point); //友元函数
//int型变量为点数
private: //私有数据成员
float X,Y;
};
//End of point.h
//main.cpp
#include<iostream>
#include<cmath>
#include "point.h"
using namespace std;
float linefit(Point l_point[], int n_point) //友元函数体
{
float av_x,av_y; //声明变量
float L_xx,L_yy,L_xy;
//变量初始化
av_x=0; //X的平均值
av_y=0; //Y的平均值
L_xx=0; //Lxx
L_yy=0; //Lyy
L_xy=0; //Lxy
for(int i=0;i<n_point;i++) //计算X、Y的平均值
{
av_x+=l_point[i].X/n_point;
av_y+=l_point[i].Y/n_point;
}
for(i=0;i<n_point;i++) //计算Lxx、Lyy和Lxy
{
L_xx+=(l_point[i].X-av_x)*(l_point[i].X-av_x);
L_yy+=(l_point[i].Y-av_y)*(l_point[i].Y-av_y);
L_xy+=(l_point[i].X-av_x)*(l_point[i].Y-av_y);
}
cout<<"This line can be fitted by y=ax+b."<<endl;
cout<<"a="<<L_xy/L_xx; //输出回归系数a
cout<<" b="<<av_y-L_xy*av_x/L_xx<<endl; //输出回归系数b
return float(L_xy/sqrt(L_xx*L_yy)); //返回相关系数r
}
int main()
{
Point l_p[10]={Point(6,10),Point(14,20),Point(26,30),
Point(33,40),Point(46,50),Point(54,60),Point(67,70),
Point(75,80),Point(84,90),Point(100,100)}; //初始化数据点
float r=linefit(l_p,10); //进行线性回归计算
cout<<"Line coefficient r="<<r<<endl; //输出相关系数
}
class Point //Point类的声明
{
public: //外部接口
Point(float xx=0, float yy=0) {X=xx;Y=yy;}
float GetX() {return X;}
float GetY() {return Y;}
friend float linefit(Point l_point[], int n_point); //友元函数
//int型变量为点数
private: //私有数据成员
float X,Y;
};
//End of point.h
//main.cpp
#include<iostream>
#include<cmath>
#include "point.h"
using namespace std;
float linefit(Point l_point[], int n_point) //友元函数体
{
float av_x,av_y; //声明变量
float L_xx,L_yy,L_xy;
//变量初始化
av_x=0; //X的平均值
av_y=0; //Y的平均值
L_xx=0; //Lxx
L_yy=0; //Lyy
L_xy=0; //Lxy
for(int i=0;i<n_point;i++) //计算X、Y的平均值
{
av_x+=l_point[i].X/n_point;
av_y+=l_point[i].Y/n_point;
}
for(i=0;i<n_point;i++) //计算Lxx、Lyy和Lxy
{
L_xx+=(l_point[i].X-av_x)*(l_point[i].X-av_x);
L_yy+=(l_point[i].Y-av_y)*(l_point[i].Y-av_y);
L_xy+=(l_point[i].X-av_x)*(l_point[i].Y-av_y);
}
cout<<"This line can be fitted by y=ax+b."<<endl;
cout<<"a="<<L_xy/L_xx; //输出回归系数a
cout<<" b="<<av_y-L_xy*av_x/L_xx<<endl; //输出回归系数b
return float(L_xy/sqrt(L_xx*L_yy)); //返回相关系数r
}
int main()
{
Point l_p[10]={Point(6,10),Point(14,20),Point(26,30),
Point(33,40),Point(46,50),Point(54,60),Point(67,70),
Point(75,80),Point(84,90),Point(100,100)}; //初始化数据点
float r=linefit(l_p,10); //进行线性回归计算
cout<<"Line coefficient r="<<r<<endl; //输出相关系数
}
相关文章推荐
- 最小二乘法用于直线,多项式,圆,椭圆的拟合及程序实现
- 最小二乘法用于直线,多项式,圆,椭圆的拟合及程序实现(资料整理的非常全)
- 最小二乘法拟合直线-C++实现
- 最小二乘法用于直线,多项式,圆,椭圆的拟合及程序实现
- 最小二乘法直线拟合(C++)
- 最小二乘法拟合直线c++代码
- 曲线拟合的最小二乘法(基于OpenCV实现)的,拟合图像中离散点的拟合直线
- 速求“最小二乘法”的“visual c”程序——注意不是要“C++”的
- 最小二乘法直线拟合
- 最小二乘法直线拟合及其Matlab实现
- 最小二乘法直线拟合
- 最小二乘法拟合直线公式推导及vc实现[转]
- 最小二乘法直线拟合
- 算法+OpenCV】基于opencv的直线和曲线拟合与绘制(最小二乘法)
- 【算法研究与实现】最小二乘法直线拟合
- 利用MATLAB进行直线拟合的程序
- 【算法研究与实现】最小二乘法直线拟合
- C++最小二乘法拟合-(线性拟合和多项式拟合)
- 用vb编写一个直线拟合程序
- 最小二乘法拟合直线