Skip to content

Commit 285bd37

Browse files
authored
Merge pull request #208 from posi2/master
#issue 154
2 parents 0f1c554 + 1fe1328 commit 285bd37

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

EXPONENTIAL REGRESSION

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<bits/stdc++.h>
2+
#include<iomanip>
3+
using namespace std;
4+
#define f float
5+
void exponential(f x[], f y[], int n){
6+
f Y[n],sumx=0,sumy=0,sumxy=0,sumx2=0;
7+
double a,b,A;
8+
for(int i=0;i<=n-1;i++)
9+
{
10+
Y[i]=log(y[i]);
11+
}
12+
for(int i=0;i<=n-1;i++)
13+
{
14+
sumx=sumx +x[i];
15+
sumx2=sumx2 +x[i]*x[i];
16+
sumy=sumy +Y[i];
17+
sumxy=sumxy +x[i]*Y[i];
18+
19+
}
20+
A=((sumx2*sumy -sumx*sumxy)*1.0/(n*sumx2-sumx*sumx)*1.0);
21+
b=((n*sumxy-sumx*sumy)*1.0/(n*sumx2-sumx*sumx)*1.0);
22+
a=exp(A);
23+
printf("\n The curve is Y= %4.3fe^%4.3fX",a,b);
24+
}
25+
26+
int main()
27+
{
28+
int i,n;
29+
cout<<"welcome to linear regression\n";
30+
cout<<"enter number of data you want to enter\n";
31+
cin>>n;
32+
f x[n],y[n];
33+
cout<<"enter x variables\n";
34+
for(i=0;i<n;++i) cin>>x[i];
35+
cout<<"enter y variables\n";
36+
for(i=0;i<n;++i) cin>>y[i];
37+
exponential(x,y,n);
38+
return 0;
39+
}

LINEAR REGRESSION

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<bits/stdc++.h>
2+
#include<iomanip>
3+
using namespace std;
4+
#define f float
5+
void linear(f x[],f y[],int n)
6+
{
7+
f cx=0,cy=0,sumxy=0,sumx2=0;
8+
double a,b;
9+
for(int i=0;i<=n-1;i++)
10+
{
11+
cx=cx+x[i];
12+
sumx2=sumx2 +x[i]*x[i];
13+
cy=cy+y[i];
14+
sumxy=sumxy +x[i]*y[i];
15+
16+
}
17+
a=((sumx2*cy -cx*sumxy)*1.0/(n*sumx2-cx*cx)*1.0);
18+
b=((n*sumxy-cx*cy)*1.0/(n*sumx2-cx*cx)*1.0);
19+
printf("\n\nThe line is Y=%3.3f +%3.3f X",a,b);
20+
}
21+
int main()
22+
{
23+
int i,n;
24+
cout<<"welcome to linear regression\n";
25+
cout<<"enter number of data you want to enter\n";
26+
cin>>n;
27+
f x[n],y[n];
28+
cout<<"enter x variables\n";
29+
for(i=0;i<n;++i) cin>>x[i];
30+
cout<<"enter y variables\n";
31+
for(i=0;i<n;++i) cin>>y[i];
32+
linear(x,y,n);
33+
return 0;
34+
}

polynomial regression

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include<bits/stdc++.h>
2+
#include<iomanip>
3+
using namespace std;
4+
#define f float
5+
void poly(float x[], float y[], int N){
6+
int n;
7+
cout << "\nPlease enter the degress:\n";
8+
cin >> n;
9+
10+
double X[2*n+1]; //Array that will store the values of sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n)
11+
for (int i=0;i<2*n+1;i++)
12+
{
13+
X[i]=0;
14+
for (int j=0;j<N;j++)
15+
X[i]=X[i]+pow(x[j],i); //consecutive positions of the array will store N,sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n)
16+
}
17+
double B[n+1][n+2],a[n+1]; //B is the Normal matrix(augmented) that will store the equations, 'a' is for value of the final coefficients
18+
for (int i=0;i<=n;i++)
19+
for (int j=0;j<=n;j++)
20+
B[i][j]=X[i+j]; //Build the Normal matrix by storing the corresponding coefficients at the right positions except the last column of the matrix
21+
double Y[n+1]; //Array to store the values of sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^n*yi)
22+
for (int i=0;i<n+1;i++)
23+
{
24+
Y[i]=0;
25+
for (int j=0;j<N;j++)
26+
Y[i]=Y[i]+pow(x[j],i)*y[j]; //consecutive positions will store sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^n*yi)
27+
}
28+
for (int i=0;i<=n;i++)
29+
B[i][n+1]=Y[i]; //load the values of Y as the last column of B(Normal Matrix but augmented)
30+
n=n+1; //n is made n+1 because the Gaussian Elimination part below was for n equations, but here n is the degree of polynomial and for n degree we get n+1 equations
31+
32+
for (int i=0;i<n;i++) //From now Gaussian Elimination starts(can be ignored) to solve the set of linear equations (Pivotisation)
33+
for (int k=i+1;k<n;k++)
34+
if (B[i][i]<B[k][i])
35+
for (int j=0;j<=n;j++)
36+
{
37+
double temp=B[i][j];
38+
B[i][j]=B[k][j];
39+
B[k][j]=temp;
40+
}
41+
42+
for (int i=0;i<n-1;i++) //loop to perform the gauss elimination
43+
for (int k=i+1;k<n;k++)
44+
{
45+
double t=B[k][i]/B[i][i];
46+
for (int j=0;j<=n;j++)
47+
B[k][j]=B[k][j]-t*B[i][j]; //make the elements below the pivot elements equal to zero or elimnate the variables
48+
}
49+
for (int i=n-1;i>=0;i--) //back-substitution
50+
{ //x is an array whose values correspond to the values of x,y,z..
51+
a[i]=B[i][n]; //make the variable to be calculated equal to the rhs of the last equation
52+
for (int j=0;j<n;j++)
53+
if (j!=i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated
54+
a[i]=a[i]-B[i][j]*a[j];
55+
a[i]=a[i]/B[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
56+
}
57+
58+
cout<<"\nThe curve is:Y=";
59+
for (int i=0;i<n;i++)
60+
cout<<" + ("<<a[i]<<")"<<"x^"<<i;
61+
cout<<"\n";
62+
63+
}
64+
65+
int main()
66+
{
67+
int i,n;
68+
cout<<"welcome to linear regression\n";
69+
cout<<"enter number of data you want to enter\n";
70+
cin>>n;
71+
f x[n],y[n];
72+
cout<<"enter x variables\n";
73+
for(i=0;i<n;++i) cin>>x[i];
74+
cout<<"enter y variables\n";
75+
for(i=0;i<n;++i) cin>>y[i];
76+
poly(x,y,n);
77+
return 0;
78+
}

0 commit comments

Comments
 (0)