|
| 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