Least Square Regression (Order 2)

/*......++++++ Least Squre Regression ++++++......
......++++++ Order 2(Graphics)++++++......
......++++++ AUTHOR: SUDIPTA KUMAR PAIK ++++++......
......++++++ Numerical Method __3721 ++++++......*/

#include graphics.h>
#include stdlib.h>
#include stdio.h>
#include conio.h>
#include math.h>

# define eqn 50

main()
{
int i,xx,yy,qw,q,n,k,j,h,f,d,N=3,xmax, ymax,xlow,ylow;
int gdriver = DETECT, gmode, errorcode;
float xy[eqn+1][3],r[4][5],a[4][5],hh[1000][3];
float sumxi,sumxi2,sumyi,sumxi3,sumxiyi,sumxi4,sumxi2yi,ar[4],x;
char msg[80];

sumxi=sumxi2=sumyi=sumxi3=sumxiyi=sumxi4=sumxi2yi=0;

/* xmax = 639; ymax = 479; */

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

do
{ sumxi=sumxi2=sumyi=sumxi3=sumxiyi=sumxi4=sumxi2yi=0;
xmax = 639;
ymax = 479;
xlow=169;
ylow=7;

clrscr();
setbkcolor(9);

printf("\n\t\tEnter [ 0 ] for EXIT \n");
printf("\n\t\tMaximum[ %d ] \n",eqn);

printf("\n Enter No. of Observation= ");
scanf("%d",&n);

if(n==0)
{ printf("\n\n\n\t\t\t<<<<===Thank you for use this program===>>>>\n");
printf("\n\t\t\t\t.............Prepared By SUDIPTA.............. ");
getch();
//delay(1200);
exit(1);
}

def1:
setbkcolor(i+1);
printf("\n\n Excute ONE........\n");
printf("\n [1]-INPUT MANUALY");
printf("\n [2]-INPUT RANDOMLY");
printf("\n\n CHOICE= ");
scanf("%d",&q);
printf("\n");
switch(q)
{ case 1:
for(i=1; i<=n; i++)
{ printf("\n Observation [ %d ]\n",i);
printf("\n x%d=",i);
scanf("%f",&xy[i][1]);
printf("\ty%d=", i);
scanf("%f",&xy[i][2]);
}

break;
case 2:
setbkcolor(i+4);
for(i=1; i<=n; i++)
{
xy[i][1]=(rand() % 24);
xy[i][2]=(rand() % 24);
}
break;
default:
setbkcolor(i+5);
printf("\n WRONG SELECTION");
printf("\n Press Enter");
getch();
goto def1;
}



for(i=1; i<=n; i++)
{ sumxi=sumxi+xy[i][1];
sumyi=sumyi+xy[i][2];
sumxiyi=sumxiyi+xy[i][1]*xy[i][2];
sumxi2yi=sumxi2yi+xy[i][1]*xy[i][1]*xy[i][2];
sumxi2=sumxi2+xy[i][1]*xy[i][1];
sumxi3=sumxi3+xy[i][1]*xy[i][1]*xy[i][1];
sumxi4=sumxi4+xy[i][1]*xy[i][1]*xy[i][1]*xy[i][1];
}

r[1][1]=n; r[1][2]=sumxi; r[1][3]=sumxi2; r[1][4]=sumyi;
r[2][1]=sumxi; r[2][2]=sumxi2; r[2][3]=sumxi3; r[2][4]=sumxiyi;
r[3][1]=sumxi2; r[3][2]=sumxi3; r[3][3]=sumxi4; r[3][4]=sumxi2yi;


for(k=1,j=1; j<=N; j++,k++)
{
for(f=1; f<=N; f++)
{ for(h=1; h<=N+1; h++)
a[f][h]=r[f][h];
}

for(i=1; i<=N+1; i++)
r[j][i]=r[j][i]/a[k][k];

for(f=1; f<=N; f++)
{ for(h=1; h<=N+1; h++)
a[f][h]=r[f][h];
}

for(i=1; i<=N; i++)
{ if(i!=j)
{ for(d=1; d<=N+1; d++)
r[i][d]=r[i][d]-a[i][j]*r[j][d];
}
}

}

ar[0]=r[1][4];
ar[1]=r[2][4];
ar[2]=r[3][4];

clrscr();
for(i=1; i<=n; i++)
{ printf(" ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n Observation [ %2d ]\n",i);
printf(" x%2d=%4.2f ±",i,xy[i][1]);
printf(" y%2d=%4.2f\n",i,xy[i][2]);
}

if(r[2][4]<0 && r[3][4]<0)
{
sprintf(msg," y=%f%fx%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]<0 && r[3][4]>0)
{
sprintf(msg," y=%f%fx+%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]>0 && r[3][4]<0)
{
sprintf(msg," y=%f+%fx%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]>0 && r[3][4]>0)
sprintf(msg," y=%f+%fx+%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);


setbkcolor(6);
setcolor(3);
line(xlow, ymax/2, xmax, ymax/2);
line(xlow-1, ymax/2+1, xmax+1, ymax/2+1);
line(xmax/2+xlow/2, ylow, xmax/2+xlow/2, ymax);
line(xmax/2+xlow/2+1,ylow-1, xmax/2+xlow/2+1, ymax+1);
setcolor(GREEN);
rectangle(xlow,ylow, xmax, ymax);
rectangle(xlow-1,ylow-1, xmax-1, ymax-1);
rectangle(xlow-2,ylow-2, xmax-2, ymax-2);


for(i=1; i<=n; i++)
{ setcolor(10);
xx = ceil(xy[i][1])+235;
yy = ceil(xy[i][2])+235;
circle(xx+xlow,yy,2);
delay(24);
}


x=-125.0;

for(qw=1; qw<=1000 ; qw++)
{
hh[qw][1]=x;
hh[qw][2]=ar[0]+ar[1]*x+ar[2]*x*x;

xx = ceil(hh[qw][1])+235;
yy = ceil(hh[qw][2])+235;

putpixel(xx+xlow,yy,14);

delay(11);

x=x+.25;
}

getch();

}while(1);

}

No comments:

Post a Comment