⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nihe.txt

📁 任意输入五个点做线性拟合
💻 TXT
字号:
#include<iostream>
#include<cmath>
#include"nihe1.h"
using namespace std;
float linefit(Point l_point[],int n_point,float *R,float *a1,float *a0)  //通常的函数(非指针型)调用结束后只能返回一个变量或者对象
                          //要想函数结束时把大量数据返回给主调函数,可以使用指针型函数。
{
	float av_x,av_y;
	float L_xx,L_yy,L_xy;
	float a,b;
    av_x=0;
	av_y=0;
	L_xx=0;
	L_yy=0;
	L_xy=0;
	for (int i=0;i<n_point;i++)
	{
      av_x+=l_point[i].X/n_point;
	  av_y+=l_point[i].Y/n_point;
	}
	for(i=0;i<n_point;i++)
	{
      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);
    }
	a=L_xy/L_xx;
	b=av_y-L_xy*av_x/L_xx;
		//cout<<"This line can be fitted by y=ax+b."<<endl;
	//cout<<"a="<<a<<endl;
	//cout<<"b="<<b<<endl;
	*a1=a;
	*a0=b;
	*R=L_xy/sqrt(L_xx*L_yy);//);return float(
return 0;
}

int main()
{
	int i;
	float CX,CY;
	float m,n,p;
   Point l_p[5]={Point(6,10),Point(14,20),Point(26,30),Point(33,40),Point(46,50)};//其实这里也可以不特意进行初始化
		for(i=0;i<5;i++)
	{
		cout<<"Please input the X coordinates of the "<<i+1<<" point:"<<endl;
        cin>>CX;
		l_p[i].setPointCoX(CX);//这里不能写l_p[i].X,因为l_p[i]不能访问类Point的私有成员X
		cout<<"Please input the Y coordinates of the "<<i+1<<"point:"<<endl;
        cin>>CY;
		l_p[i].setPointCoY(CY);
	}
	linefit(l_p,5,&p,&m,&n);	//float r=
	cout<<"So the line can be fitted by:y="<<m<<"x+"<<n<<endl;
	//float r=linefit(l_p,5,&p,&m,&n);
	cout<<"Line coefficient r"<<p<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -