Write a C program to implement the polynomial regression algorithm.

Algorithm:

Step 1:  Start
Step 2:  Read n
Step 3:  Initialize sumx = 0, sumxsq = 0, sumy = 0, sumxy = 0, sumx3 = 0, sumx4 = 0, sumxsq = 0
Step 4:  Initialize 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: Initialize i = 0
Step 11: Repeat steps 11 to 15 until i < 3
Step 12: Initialize 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: Initialize i = 0
Step 20: Repeat step 20 to 26 until i <= 2
Step 21: If I not equal to k
Step 22: Assign u = a[i][k] / a[k][k]
Step 23: Initialize 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

Flowchart:

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