最小二乘法 直线生成
来源:互联网 发布:淘宝店铺的层级 编辑:程序博客网 时间:2024/06/05 07:20
//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; //输出相关系数
}
- 最小二乘法 直线生成
- 最小二乘法直线拟合
- 最小二乘法直线拟合
- 最小二乘法似合直线
- 最小二乘法直线拟合
- 最小二乘法直线拟合
- 最小二乘法直线拟合
- 最小二乘法 直线拟合
- 最小二乘法拟合直线
- 最小二乘法直线拟合
- 最小二乘法直线拟合
- 最小二乘法拟合直线
- 最小二乘法直线拟合
- 最小二乘法直线拟合(C++)
- 最小二乘法实现直线拟合
- 最小二乘法直线拟合
- 最小二乘法拟合直线
- 最小二乘法直线拟合简介
- ODI 11g和Oracle BAM的集成
- 重写back键
- 自定义Toast
- 程序员基本技能
- HRESULT返回值说明(转) .
- 最小二乘法 直线生成
- 如何在应用中打开系统播放器
- AndroidManifest之Service
- 进入软件测试行业的六年感悟
- ASP.NET 防重复提交.
- uestc 1425 Another LCIS(线段树 最长递增序列)
- 使用ActivityGroup来实现tabhost效果
- 深入分析Android (build/core/*.mk脚本)
- java数组遍历——iterator和for方法