Saturday 15 September 2012

Implement in ‘C’ the linear regression and polynomial regression algorithms


 Linear regression

Algorithm:

Step 1. Read n

Step 2. Sumx=0

Step 3. Sumxsq=0

Step 4. Sumy=0

Step 5. Sumxy=0

Step 6. fori=1 to n do

Step 7. Read x,y

Step 8. Sumx=sumx+x

Step 9.Sumxsq=Sumxsq+x2

Step 10.Sumy=Sumy+y

Step 11.Sumxy=sumxy+x x y end for

Step 12. denom=n x sumxsq – sumx x sumx

Step 13. a0=(sumy x  sumxsq – sumx x sumxy) / denom

Step 14. a1=(n x sumxy-sumx x sumy)/ denonm

Step 15. Write a1,a0

Step 16. STOP
 Program:

            #include<stdio.h>
            #include<math.h>
            Main()
            {
                        Int n,I;
                        Float sumx, sumxsq, sumy, sumxy, x, y, a0, a1, denom;
                        Printf(“enter the n value”);
                        Scanf(“%d”,&n);
                        Sumx=0;
                        Sumsq=0;
                        Sumy=0;
                        Sumxy=0;
                        For(i=0;i<n;i++)
                        {
                                    Scanf(“%f %f”,&x, &y);
                                    Sumx +=x;
                                    Sumsq += pow(x, 2);
                                    Sumy +=y;
                                    Sumxy +=x * y;
                        }
                        Denom = n * sumxsq – pow(sumx, 2);
                        A0 = (sumy * sumxsq –sumx *sumxy)/denom;
                        A1 = (n * sumxy –sumx *sumy)/denom;
                        Printf(“y= %fx + %f”,a1,a0);
                   }
Input/Output:
Enter the n value 7
1                2
2                5
4                7
5                10
6                12
8                15
9                19
Y = 1.980769x + 0.096154
Conclusion: The program is error free
VIVA QUESATIONS
1) What is the use of goto statement ?
Ans: The goto statement is used to alter the normal sequence of the program execution by unconditionally transferring control to some other part of the program.
 2) What is the use of continue statement ?
Ans: The continue statement is used to bypass the remainder of the current pass through a loop

Polynomial regression

Algorithm:

Sep 1: Strart

Step 2: Read n

Step 3: Initialize sumx = 0, sumxsq = 0, sumy = 0, sumxy = 0, sumx3 = 0, sumx4 = 0, sumxsq =0

Step 4: Intialize i=0

Step 5: Repeat steps 5 to 7 until i<n

Step 6: Read x,y

Step 7: Sumx = sumx + x
            Sumxsq =sumxsq + pow(x,2)
            Sumx3 = sumx3 + pow(x,3)
            Sumx4 = sumx4 + pow(x,4)
            Sumy = sumy + y
            Sumxy = Sumxy + x*y
            Sumxsqy = Sumxsqy + pow(x,2) *y

Step 8: Increment  I by 1

Step 9: Assign
         a[0][0] = n
         a[0][1] = n
a[0][2] = n
a[0][3] = n
a[1][0] = n
a[1][1] = n
a[1][2] = n
a[1][3] = n
a[2][0] = n
a[2][1] = n
a[2][2] = n
a[2][3] = n
                          
                           Step 10: Intialize i=0

Step 11: Repeat steps 11 to 15 until i<3

Step 12: Intialize j=0

Step 13: Repeat step 13 to 14 until j<=3

Step 14: Write a[i][j]

Step 15: Increment j by 1

Step 16: Increment I by 1

Step 17: Initialize k =0

Step 18: Repeat steps  18 to 27 until k<=2

Step 19: Intialize i=0

Step 20: Repeat step 20 to 26 until i<=2

Step 21: If  I not equal to k

Step 22: Asign u=a[i][k]/a[k][k]

Step 23: Intialize j=k

Step 24: Repeat steps 24 and 25 until j<=3

Step 25: Assign a[i][j] = a[i][j] – u *a[k][j]

Step 26: Increment j by 1

Step 27: Increment i by 1

Step 28: Increment k by 1

Step 29: Initialize I =0

Step 30: Repeat steps 31 to 33  until i<3

Step 31: Assign b[i] = a[i][3]/a[i][i]

Step 32: Write I, b[i]

Step 33: Increment I by 1

Step 34: Write b[2],b[i],b[0]
Step 35: Stop
Program:
           

            #include<stdio.h>
            #include<math.h>
main()
{
               Int n, I, j, k;
               Float sumx, sumxsq, sumy, sumxy, x, y;
               Float sumx3, sumx4, sumxsqy, a[20][20], u=0.0, b[20];
               Printf(“\n Enter the n value”);
               Scanf(“%d”, &n);
               Sumx = 0;
               Sumxsq = 0;
               Sumy = 0;
               Sumxy = 0;
               Sumx3 = 0;
               Sumx4 = 0;
               Sumxsqy = 0;
               For(i=0;  i<n; i++)
               {
                        Scanf(“%f %f”, &x, &y);
                        Sumx +=x;
                        Sumxsq += pow(x,2);
                        Sumx3 += pow(x,3);
                        Sumx4 += pow(x,4);
                        Sumy +=y;
                        Sumxy += x * y;
                        Sumxsqy += pow(x,2) *y;
               }
               A[0][0] = n;
               A[0][1] = sumx;
               A[0][2] = sumxsq;
               A[0][3] = sumy;
               A[1][0] = sumx;
               A[1][1] = sumxsq;
               A[1][2] = sumx3;
               A[1][3] = sumxy;
               A[2][0] = sumxsq;
               A[2][1] = sumx3;
               A[2][2] = sumx4;
               A[2][3] = sumxsqy;
for(i=0;  i<3; i++)
  for(j=0;  j<=3; j++)
               Printf(“%10.2f”,a[i][j]);
               Printf(“\n”);
            }
          For(k=0;  k<=2; k++)
          {
               For(i=0;i<=2;i++)
               {
                        If(i!=k)
                           U=a[i][k]/a[k][k];
                        For(j = k; j<=3; j++)
                                    A[i][j]=a[i][j] – u * a[k][j];
                }
        }

     For(i=0;i<3;i++)
     {
            B[i] = a[i][3]/a[i][i];
            Printf(“\nx[%d] = %f”, I, b[i]);
     }
  Printf(“\n”);
 Printf(“y= %10.4fx +10.4 fx +%10.4f”,b[2],b[i],b[0]);
}


Input/Output:

Enter the n value 10

-4                     21
-3                     12
-2                     4
-1                     1
 0                     2
 1                     7
 2                     15
 3                     30
 4                     45
 5                     67
               10.00            5.00                 85.00                  204.00
                 5.00            85.00               125.00                513.00
               85.00            125.00             1333.00              3193.00
X[0] = 2.030303
X[1] = 2.996970
X[2] = 1.984848
Y =         1.9848xsq +  2.9979x  + 2.0303
Conclusion: The program is error free

VIVA QUESATIONS
1)  Define insertion sort ?
Ans: Insertion sort is similar to playing cards. To sort the cards in yourhand you extrat a card shift the remaining cards and then insert the extracted card in its correct place.

2) Efficiency of the insertion sort ?
Ans: The efficiency of insertion sort is O(n2).





1 comment:

  1. Thank you for the code. It worked fine. I had to re-type parts of it because there was some conversion of the " and - signs to an improper form when direct copying. Also the upper case initials in the code caused some problems. I understand that those are beginner's problems but that is what I am. If you could re-post that it would be much appreciated and it could help us beginners. Regards.

    ReplyDelete