Hi,
I have wrote a program to do multiple linear regression (form the matrix ) and solve the relevant matrix using Gaussian Elimination,Unfortunately it is not running and once I get it to run it is gives me the error stating det=0.
Please have a look and let me know how to fix it.
// Multiple Linear Regression
//#include<stdio.h>
//#include<stdlib.h>
//#include<math.h>
//#define limit 10
//#define Max 20
//void main()
//{
int i; //Loop counter
int N; //Number of data points
double X1[Max]; //X1 values presented by matrix elements
double X2[Max]; //X2 values presented by matrix elements
double Y[Max]; //Y values presented by matrix elements
double SumX1; //sum Value of X1
double SumX2; //Sum Value of X2
double SumX1X1; //Sum Value of X1*X1
double SumX1X2; //Sum Value of X1*X2
double SumX2X2; //Sum Value of X2*X2
double SumY; //sum Value Y
double SumX1Y; //Sum Value of Y*X1
double SumX2Y; //Sum Value of Y*X2
int K, P, C, J; //Loop Counters
double M[3][4]; //Gaussian Input
int Row[Max]; //Pointer Vector
double a[Max]; //Unknown Vector
double SUM, Z;
int Pivot;
// printf("Enter Number of Data points,It should be less than %d :\n",Max);
// scanf_s("%d",&N);
// for(i = 1; i <= N; i++)
//{
printf("For Data Point Number %d ,Enter values of X1,X2 and Y seperated by comma.\n",i);
scanf_s("%lf,%1f,%1f", &X1[i-1],&X2[i-1],&Y[i-1]);
}
//Calculating Sum X1
// SumX1 = 0.0; //Setting the initial value
//for(i = 1; i <= N; i++) SumX1 = SumX1 + (X1[i-1]);
//Calculating Sum X2
//SumX2 = 0.0; //Setting the initial value
//for(i = 1; i <= N; i++) SumX2 = SumX2 + (X2[i-1]) ;
//Calculating SumY
//SumY = 0.0; // Setting the initial Value
//for(i = 1; i <= N; i++) SumY = SumY + (Y[i-1]);
//Calculating SumX1^2
//SumX1X1 = 0.0; //Setting the initial Value
// for(i = 1; i <= N; i++) SumX1X1 = SumX1X1 +( (X1[i-1] ) * (X1[i-1] ) );
//Calculating SumX2^2
//SumX2X2 = 0.0; //Setting the initial Value
//for(i = 1; i <= N; i++) SumX2X2 = SumX2X2 +( (X2[i-1] ) * (X2[i-1] ) );
//Calculating SumX1.X2
//SumX1X2 = 0.0; //Setting the initial Value
//for(i = 1; i <= N; i++) SumX1X2 = SumX1X2 +( (X1[i-1] ) * (X2[i-1] ) );
//Calculating SumX2.Y
//SumX1Y = 0.0; //Setting the initial Value
// for(i = 1; i <= N; i++) SumX1Y = SumX1Y +( (X1[i-1] ) * (Y[i-1] ) );
//Calculating SumX1.Y
//SumX2Y = 0.0; //Setting the initial Value
// for(i = 1; i <= N; i++) SumX2Y = SumX2Y +( (X2[i-1] ) * (Y[i-1] ) );
//Forming the Gaussian Input Matrix
//M[0][0] = N;
//M[0][1] = SumX1;
//M[0][2] = SumX2;
//M[0][3] = SumY;
//M[1][0] = SumX1;
//M[1][1] = SumX1X1;
//M[1][2] = SumX1X2;
//M[1][3] = SumX1Y;
//M[2][0] = SumX2;
//M[2][1] = SumX1X2;
//M[2][2] = SumX2X2;
//M[2][3] = SumX2Y;
//Forming Pointer Vector
//for (J = 1; J<= 3; J++) Row[J-1] = J - 1;
//Upper Triangular Matrix
//for (P = 1; P <= 2; P++)
{
//Pivoting
//for (K = P + 1; K <= 3; K++)
{
if ( fabs(M[Row[K-1]][P-1]) > fabs(M[Row[P-1]][P-1]) )
{
Pivot = Row[P-1];
Row[P-1] = Row[K-1];
Row[K-1] = Pivot;
}
}
//for (K = P + 1; K <= 3; K++)
{
Z = M[Row[K-1]][P-1] / M[Row[P-1]][P-1];
//for (C = P + 1; C <= 4; C++)
{
M[Row[K-1]][C-1] -= Z * M[Row[P-1]][C-1];
}
}
}
// if( M[Row[2]][2] == 0)
{
// printf("Matrix determinant is Zero,Failure to apply Gaussian Elimination Method !\n");
exit(1);
}
// Back substitution
//a[2] = M[Row[2]][3] / M[Row[2]][2];
// for (K = 2; K >= 1; K--)
{
SUM = 0;
for (C = K + 1; C <= 3; C++)
{
SUM += M[Row[K-1]][C-1] * a[C-1];
}
a[K-1] = ( M[Row[K-1]][3] - SUM) / M[Row[K-1]][K-1];
} // End of back substitution
//for( K = 1; K <= 3; K++) printf("Final answer for a[%d] = %lf\n", K, a[K-1]);
}