Menu Close

Moments Method C++

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x[10],y[10];
int i,n,yt=0,xtyt=0;
float a,b,l1,l2,c1,c2,c3,d,d1,d2,m1,m2,h;


cout<<"enter the no. of observations "<<endl;
cin>>n;

cout<<"Enter the different values of x "<<endl;

for(i=1;i<=n;i++)
{
cin>>x[i];
}

cout<<"\nenter corresponding values of y "<<endl;

for(i=1;i<=n;i++)
{
cin>>y[i];
}

h=x[2]-x[1];

for(i=1;i<=n;i++)
{
yt += y[i];
xtyt += x[i]*y[i];
}

m1=h*yt;
m2=h*xtyt;
l1=(-(h/2)+x[1]);
l2=((h/2)+x[n]);
c1=(l2-l1);
c2=((l2*l2)-(l1*l1))/2;
c3=((l2*l2*l2)-(l1*l1,l1))/3;

cout<<"The observed equations are "<<endl;
cout<<c1<<"a+"<<c2<<"b"<<endl<<"&"<<c2<<"a+"<<c3;
cout<<"b";

d=c2/c1;
d1=d*c1;
d2=d*c2;
m1=d*m1;
b=(m2-m1)/(c3-d2);
a=(m1-(d2*b))/d1;

cout<<"\n On solving equations we get a+";
cout<<setw(5)<<setprecision(2)<<a<<"&b=";
cout<<setw(5)<<setprecision(2)<<b<<endl;

cout<<"Hence the required equation is y=";
cout<<setw(5)<<setprecision(2)<<a<<"+"<<setw(5);
cout<<setprecision(2)<<b<<"x";

return 0;
}

Output

enter the no. of observations
4
Enter the different values of x
1 2 3 4
enter corresponding values of y
16 19 23 28
The observed equations are
4a+10b
&10a+30.2083b
On solving equations we get a+ 12&b= 3.8
Hence the required equation is y= 12+ 3.8x

More Related Stuff