十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
最小二乘法常用于根据实测数据求线性方程的最近似解。根据如图(图片引用于百度百科)的描述,利用C语言求,使用最小二乘法算法求线性方程的解,程序如下:
创新互联公司专业为企业提供汝南网站建设、汝南做网站、汝南网站设计、汝南网站制作等企业网站建设、网页设计与制作、汝南企业网站模板建站服务,十载汝南做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
#include stdio.h
#define N 4 //共有4个记录,根据需要增加记录
typedef struct Data{ //定义实验记录结构
int w; //实验次数
double x;
double y;
}DATA;
//根据d中的n个DATA记录,计算出线性方程的a,b两值
void getcs(DATA *d,int n,double a,double b){
double fi11=0,fi12=0,fi21=0,fi22=0,f1=0,f2=0;
int i;
for(i=0;in;i++){
fi11+=d[i].w;
fi12+=d[i].w*d[i].x;
fi21=fi12;
fi22+=d[i].w*d[i].x*d[i].x;
f1+=d[i].w*d[i].y;
f2+=d[i].w*d[i].x*d[i].y;
}
//解一元一次方程
b=(f2*fi11/fi21-f1)/(fi22*fi11/fi21-fi12);
a=(f2*fi12/fi22-f1)/(fi21*fi12/fi22-fi11);
}
int main(){
DATA d[N]={ //定义时赋初值,共4个记录
{2,0.1,1.1},
{1,0.2,1.9},
{1,0.3,3.1},
{1,0.4,3.9}
};
double a,b;
getcs(d,N,a,b); //计算线性方程参数a,b
printf("线性方程是:Y=%.4lf+%.4lfX\n",a,b);
}
#include stdio.h
#include math.h
#define epsilon 1e-6
void nihe1(int n,int m,float sum_x,float sum_y,float sum_xy,float x2);
void nihe2(int n,int m,float sum_x,float sum_y,float sum_xy,float x2,float x2y,float x3,float x4);
int main(){
float x[100]={0.0};
float y[100]={0.0};
int n,i,flag=1;
float sum_y=0.0,sum_x=0,x2=0,sum_xy=0.0,x3=0,x4=0,x2y=0.0;
printf("请你输入需要测试的数据(先输入x[],后输入y[])的个数:");
scanf("%d",n);
for(i = 0; i n; i++){
scanf("%f",x[i]);}
for(i = 0; i n; i++){
scanf("%f",y[i]);}
for(i = 0; i n; i++){
sum_x += x[i];
sum_y += y[i];
sum_xy += x[i]*y[i];
x2 += x[i]*x[i];
x2y += x[i]*x[i]*y[i];
x3 += x[i]*x[i]*x[i];
x4 += x[i]*x[i]*x[i]*x[i];}
printf("---------------请你输入的要拟合的函数------------------\n");
printf(" 1、拟合一次函数\n");
printf(" 2、拟合二次函数\n");
scanf("%d",flag);
switch(flag){
case 1:
nihe1(n,flag+1,sum_x,sum_y,sum_xy,x2); break;
case 2:
nihe2(n,flag+1,sum_x,sum_y,sum_xy,x2,x2y,x3,x4); break;
default:
printf("ERROR\n");}
return 0;}
void nihe1(int n,int m,float sum_x,float sum_y,float sum_xy,float x2){
int i,k,j;
float t,s=0;
float a[2][3] = {{(float)n,sum_x,sum_y},{sum_x,x2,sum_xy}};
n=m;
//if(m == 3)
// a[3][4] = {{n,sum_x,sum_y},{sum_x,x2,x3,sum_xy},{x2,x3,x4,x2y}};
for(k=0;kn-1;k++) {
for(i=k+1;in;i++)
if( abs((int)a[i][k]) abs((int)a[k][k]) )
for(j=k;jn+1;j++) {
t=a[k][j];
a[k][j]=a[i][j];
a[i][j]=t; }
if( abs((int)a[k][k]) epsilon) {
printf("\nError,主元消去法 cann't be durable,break at %d!\n",k+1);
return; }
for(i=k+1;in;i++){
a[i][k]=a[i][k] / a[k][k];
for(j=k+1;jn+1;j++)
a[i][j]=a[i][j]-a[i][k] * a[k][j]; }}
a[n-1][n]=a[n-1][n] / a[n-1][n-1];
for(k=n-2;k=0;k--) {
s=0;
for(j=k+1;jn;j++)
s+=a[k][j]*a[j][n];
a[k][n]=( a[k][n]-s ) / a[k][k]; }
printf("\n*****The Result*****\n");
for(i=0;in;i++)
printf(" x[%d]=%.4f\n",i+1,a[i][n]);
printf("函数为:p(x) = %.4f + (%.4f)*x\n",a[0][n],a[1][n]);
getchar();}
void nihe2(int n,int m,float sum_x,float sum_y,float sum_xy,float x2,float x2y,float x3,float x4){
int i,k,j;
float t,s=0;
float a[3][4]=
{{(float)n,sum_x,x2,sum_y},{sum_x,x2,x3,sum_xy},{x2,x3,x4,x2y}};
n=m;
for(k=0;kn-1;k++) {
for(i=k+1;in;i++)
if( abs((int)a[i][k]) abs((int)a[k][k]) )
for(j=k;jn+1;j++) {
t=a[k][j];
a[k][j]=a[i][j];
a[i][j]=t; }
if( abs((int)a[k][k]) epsilon) {
printf("\nError,主元消去法 cann't be durable,break at %d!\n",k+1);
return; }
for(i=k+1;in;i++){
a[i][k]=a[i][k] / a[k][k];
for(j=k+1;jn+1;j++)
a[i][j]=a[i][j]-a[i][k] * a[k][j]; } }
a[n-1][n]=a[n-1][n] / a[n-1][n-1];
for(k=n-2;k=0;k--) {
s=0;
for(j=k+1;jn;j++)
s+=a[k][j]*a[j][n];
a[k][n]=( a[k][n]-s ) / a[k][k]; }
printf("\n*****The Result*****\n");
for(i=0;in;i++)
printf(" x[%d]=%.4f\n",i+1,a[i][n]);
printf("函数为:p(x) = %.4f + (%.4f)*x + (%.4f)*x*x\n",a[0][n],a[1][n],a[2][n]);
getchar();}
#include stdio.h
void main ()
{
int num,i;
float x,y,l,m,n,p,a,b;
i=1;
l=0.0;
m=0.0;
n=0.0;
p=0.0;
printf ("请输入你想计算的x,y的个数:");
scanf("%d",num);
if (num=1)
{
while (i=num);
{
printf("请输入x的值");
scanf ("%lf",x);
printf("请输入y的值");
scanf ("%lf",y);
l+=x;
m+=y;
n+=x*y;
p+=x*x;
i++;
}
a=(num*n-l*m)/(num*p-l*l);
b=(p*m-n*l)/(num*p-l*l);
printf("最小二乘法所算得的斜率和截距分别为%f和%f\n",a,b);
}
else printf("mun"输入有误!);
}