# Write a C program to implement the `polynomial regression algorithm`.

#### Algorithm:

```Step 1:  Start
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 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 = n
a = n
a = n
a = n
a = n
a = n
a = n
a = n
a = n
a = n
a = n
a = 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] / a[i][i]
Step 32: Write I, b[i]
Step 33: Increment I by 1
Step 34: Write b, b[i], b
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, u = 0.0, b;
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 = n;
a = sumx;
a = sumxsq;
a = sumy;
a = sumx;
a = sumxsq;
a = sumx3;
a = sumxy;
a = sumxsq;
a = sumx3;
a = sumx4;
a = 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]/a[i][i];
printf(“\nx[%d] = %f”, I, b[i]);
}
printf(“\n”);
printf(“y = %10.4fx + 10.4 fx + %10.4f”, b, b[i], b);
}```

#### 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 = 2.030303
X = 2.996970
X = 1.984848
Y = 1.9848xsq + 2.9979x + 2.0303```
• Created
Oct 11, 2014
• Updated
Oct 21, 2014
• Views
8,539
You May Like