float k,b,x,y;
专注于为中小企业提供网站建设、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业垣曲免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
printf("请输入k,b:");
scanf("%f%f",k,b);
printf("请输入x:");
scanf("%f",x);
y=k*x十b;
printf("y=%f\n",y);
#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=0;iMAXNUM;i++)
{
cinx[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;
}
#include "stdlib.h"
#include "math.h"
#include "stdio.h"
int rgauss(n,a,b)
int n;
double a[],b[];
{ int *js,l,k,i,j,is,p,q;
double d,t;
js=malloc(n*sizeof(int));
l=1;
for (k=0;k=n-2;k++)
{ d=0.0;
for (i=k;i=n-1;i++)
for (j=k;j=n-1;j++)
{ t=fabs(a[i*n+j]);
if (td) { d=t; js[k]=j; is=i;}
}
if (d+1.0==1.0) l=0;
else
{ if (js[k]!=k)
for (i=0;i=n-1;i++)
{ p=i*n+k; q=i*n+js[k];
t=a[p]; a[p]=a[q]; a[q]=t;
}
if (is!=k)
{ for (j=k;j=n-1;j++)
{ p=k*n+j; q=is*n+j;
t=a[p]; a[p]=a[q]; a[q]=t;
}
t=b[k]; b[k]=b[is]; b[is]=t;
}
}
if (l==0)
{ free(js); printf("fail\n");
return(0);
}
d=a[k*n+k];
for (j=k+1;j=n-1;j++)
{ p=k*n+j; a[p]=a[p]/d;}
b[k]=b[k]/d;
for (i=k+1;i=n-1;i++)
{ for (j=k+1;j=n-1;j++)
{ p=i*n+j;
a[p]=a[p]-a[i*n+k]*a[k*n+j];
}
b[i]=b[i]-a[i*n+k]*b[k];
}
}
d=a[(n-1)*n+n-1];
if (fabs(d)+1.0==1.0)
{ free(js); printf("fail\n");
return(0);
}
b[n-1]=b[n-1]/d;
for (i=n-2;i=0;i--)
{ t=0.0;
for (j=i+1;j=n-1;j++)
t=t+a[i*n+j]*b[j];
b[i]=b[i]-t;
}
js[n-1]=n-1;
for (k=n-1;k=0;k--)
if (js[k]!=k)
{ t=b[k]; b[k]=b[js[k]]; b[js[k]]=t;}
free(js);
return(1);
}
徐世良老师的.原地工作全选主元的高斯消元.用的符号有点诡异,而且将矩阵以线性表输入的.
我也写过一个,不过估计你不太喜欢用我的,这个用来解b也是矩阵的.
#include stdio.h
#include stdlib.h
#include math.h
void swap(double *a,double *b){
double temp;
temp=*a;
*a=*b;
*b=temp;
}
void printMatrix(int n,int m,double **a){
int i,j;
for (i=0;in;i++){
for (j=0;jm;j++) printf("%e\t",a[i][j]);
printf("\n");
}
}
int rgauss(int n,int m,double **a,double **b){//,double *det){
int *colex,rowex,s,i,j,k;
double d,t;
// *det=1;
s=0;
colex=malloc(n*sizeof(int));
for (k=0;kn;k++){
d=0.0;
for (i=k;in;i++){
for (j=k;jn;j++){
t=fabs(a[i][j]);
if (td){
d=t;
colex[k]=j;
rowex=i;
}
}
}
if (d+1.0==1.0){
free(colex);
printf("Singular Matrix!\n");
return 0;
}
else{
if (colex[k]!=k){
for (i=0;in;i++)
swap(a[i]+colex[k],a[i]+k);
s=s+colex[k]-k;
}
if (rowex!=k){
for (j=0;jn;j++)
swap(a[rowex]+j,a[k]+j);
for (j=0;jm;j++)
swap(b[rowex]+j,b[k]+j);
s=s+rowex-k;
}
}
d=a[k][k];
// *det=*det*d;
for (j=k+1;jn;j++) a[k][j]/=d;
for (j=0;jm;j++) b[k][j]/=d;
for (i=k+1;in;i++){
for (j=k+1;jn;j++)
a[i][j]-=a[i][k]*a[k][j];
for (j=0;jm;j++)
b[i][j]-=a[i][k]*b[k][j];
}
}
for (i=n-2;i-1;i--)
for (j=i+1;jn;j++)
for (k=0;km;k++)
b[i][k]-=a[i][j]*b[j][k];
colex[n-1]=n-1;
for (i=n-1;i-1;i--)
if (colex[i]!=i)
for (j=0;jm;j++) swap(b[colex[i]]+j,b[i]+j);
// if (s1) *det=-*det;
return 1;
}
void main(){
#define N 2
#define M 1
double input_a[]={1,1,1,2};
double input_b[]={1,2};
double *a[N],*b[N];
int i;
for (i=0;iN;i++){
a[i]=input_a+N*i;
b[i]=input_b+M*i;
}
rgauss(N,M,a,b);
printf("方程解为:\n");
printMatrix(N,M,b);
printf("\n");
}