/*************** least square fitting ******************/
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,i,j;
float a,a0,a1,x[10],f[10],sumx=0,sumy=0,sumxy=0,sumx2=0;
clrscr();
cout<<"Enter no of sample points ? ";cin>>n;
cout<<"Enter all sample points: "<<endl;
for(i=0;i<n;i++)
{
cin>>x[i]>>f[i]; // read both (x,f(x))
sumx+=x[i];
sumy+=f[i];
sumxy+=x[i]*f[i];
sumx2+=x[i]*x[i];
}
cout<<"your sample x ? ";
cin>>a;
a0=(sumy*sumx2-sumx*sumxy)/(n*sumx2-sumx*sumx);
a1=(n*sumxy-sumx*sumy)/(n*sumx2-sumx*sumx);
cout<<"The coefficients are : "<<endl<<a0<<endl<<a1;
cout<<endl<<"f("<<a<<"): "<<(a0+a1*a);
getch();
return 0;
}
C C++ CODE : least square fitting regression
Subscribe to:
Post Comments
(
Atom
)
when entering all sample points what will i do all values of x then all values of y, or one after one 1st x 2nd y and so on
ReplyDelete