Friday, July 20, 2012

C Program for Multiple Regression





/*Multiple Regression */

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

void main()

{

float f,k,sum=0,a[3][4],*x1,*x2,*y,n,i,j,p[]={0,0,0};

float x_1=0,x_2=0,x12=0,x22=0,x1x2=0,y1=0,x1y1=0,x2y1=0,a1,a0,e;

clrscr();

printf("Multiple Regression");

printf("\nHow many values You are Entering");

scanf("%f",&n);

x1=(float*)malloc(n*4);

x2=(float*)malloc(n*4);

y=(float*)malloc(n*4);

puts("Enter coressponding Elements X1,X2 & Y");

for(i=0;i<n;i++)

{

scanf("%f %f %f",&x1[i],&x2[i],&y[i]);

x_1+=x1[i];

x_2+=x2[i];

x1x2+=x1[i]*x2[i];

x12+=x1[i]*x1[i];

x22+=x2[i]*x2[i];

y1=y1+y[i];

x1y1=x1y1+x1[i]*y[i];

x2y1=x2y1+x2[i]*y[i];

}

a[0][0]=n;

a[0][1]=a[1][0]=x_1;

a[0][2]=a[2][0]=x_2;

a[1][2]=a[2][1]=x1x2;

a[1][1]=x12;

a[2][2]=x22;

a[0][3]=y1;

a[1][3]=x1y1;

a[2][3]=x2y1;

puts("\nMatrix");

for(i=0;i<3;i++)

{

for(j=0;j<4;j++)

{

printf("\t%6.6f",a[i][j]);

}

printf("\n");

}

/* Gauss Elimination */

for(k=0;k<2;k++)

{

for(i=k+1;i<3;i++)




{

for(i=k+1;i<3;i++)

{

f=a[i][k]/a[k][k];

for(j=0;j<4;j++)

}

}

for(i=2;i>=0;i--)

{

sum=a[i][3];

for(j=0;j<3;j++)

{

sum=sum-a[i][j]*p[j];

}

p[i]=sum/a[i][i];

}

e=y[0]-p[0]-p[1]*x1[0]-p[2]*x2[0];

p[0]+=e;

puts("Required Equation");

printf("Y=%f+%fX1+%fX2",p[0],p[1],p[2]);

}

Output of Program:-

Multiple Regression

How many values You are Entering

6

Enter coressponding Elements X1,X2 & Y

0 0 5

2 1 10

2.5 2 9

1 3 0

4 6 3

7 2 27

Matrix

6.000000 16.500000 14.000000 54.000000

16.500000 76.250000 48.000000 243.500000

14.000000 48.000000 54.000000 100.000000

Required Equation

Y=5.000000+4.000000X1+-3.000000X2





No comments:

Post a Comment