#include <iostream>
using namespace std
const int MAXNUM = 3
int main()
{
double x[MAXNUM],y[MAXNUM]
double sumx=0,sumy=0,sumxy=0,sumxx=0
double a,b
int i
cout<<"输入"<<MAXNUM<<"组样本:"
for (i=0i<MAXNUMi++)
{
cin>>x[i]>>y[i]
sumx += x[i]
sumy += y[i]
sumxy = x[i]*y[i]
sumxx = x[i]*x[i]
}
double averagex,averagey
averagex = sumx/MAXNUM
averagey = sumy/MAXNUM
b = (sumxy - MAXNUM*averagex*averagey)/(sumxx - MAXNUM*averagex*averagex)
a = averagey - b*averagex
cout<<"线性拟合曲线为:y="<<a<<"+"<<b<<"*y\n"
return 0
}
可以直接在线进行拟合,下面是地址(已验证)http://3.14159.biz/math/Fit.html
建议你直接对数据用Matlab的cftool拟合(可以自由的自定义拟合函数形式)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)