Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to compute the polynomial regression algorithm
Polynomial regression is a form of regression analysis used to model the relationship between an independent variable x and dependent variable y as an nth degree polynomial. This technique extends linear regression to capture non-linear relationships in data.
Syntax
y = a0 + a1*x + a2*x² + a3*x³ + ... + an*x?
Where a0, a1, a2, ..., an are coefficients to be determined using least squares method.
Algorithm
The polynomial regression algorithm works by −
- Setting up a system of normal equations using least squares method
- Creating coefficient matrix from input data points
- Solving the system using Gaussian elimination
- Obtaining polynomial coefficients
Example
Following is the C program to compute polynomial regression coefficients −
#include <stdio.h>
#include <math.h>
int main() {
int i, j, k, m, n;
float x[20], y[20], u, a[10], c[20][20], power, r;
printf("Enter degree of polynomial (m) and number of data points (n): ");
scanf("%d %d", &m, &n);
printf("Enter x and y values:<br>");
for(i = 1; i <= n; i++) {
printf("x[%d], y[%d]: ", i, i);
scanf("%f %f", &x[i], &y[i]);
}
/* Form coefficient matrix */
for(j = 1; j <= m + 1; j++) {
for(k = 1; k <= m + 1; k++) {
c[j][k] = 0;
for(i = 1; i <= n; i++) {
power = pow(x[i], j + k - 2);
c[j][k] = c[j][k] + power;
}
}
}
/* Form constant terms */
for(j = 1; j <= m + 1; j++) {
c[j][m + 2] = 0;
for(i = 1; i <= n; i++) {
r = pow(x[i], j - 1);
c[j][m + 2] = c[j][m + 2] + y[i] * r;
}
}
printf("\nAugmented matrix:<br>");
for(i = 1; i <= m + 1; i++) {
for(j = 1; j <= m + 2; j++) {
printf("%.2f\t", c[i][j]);
}
printf("<br>");
}
/* Gaussian elimination */
for(k = 1; k <= m + 1; k++) {
for(i = 1; i <= m + 1; i++) {
if(i != k) {
u = c[i][k] / c[k][k];
for(j = k; j <= m + 2; j++) {
c[i][j] = c[i][j] - u * c[k][j];
}
}
}
}
printf("\nPolynomial coefficients:<br>");
for(i = 1; i <= m + 1; i++) {
a[i] = c[i][m + 2] / c[i][i];
printf("a[%d] = %.6f<br>", i - 1, a[i]);
}
return 0;
}
Output
Enter degree of polynomial (m) and number of data points (n): 2 3 Enter x and y values: x[1], y[1]: 1 1 x[2], y[2]: 2 4 x[3], y[3]: 3 9 Augmented matrix: 3.00 6.00 14.00 14.00 6.00 14.00 36.00 36.00 14.00 36.00 98.00 98.00 Polynomial coefficients: a[0] = 0.000000 a[1] = 0.000000 a[2] = 1.000000
How It Works
The algorithm creates normal equations by minimizing the sum of squared errors. For a polynomial of degree m with n data points, it solves the matrix equation to find coefficients that best fit the given data points.
Key Points
- The degree of polynomial should be less than the number of data points
- Higher degree polynomials may lead to overfitting
- The algorithm uses Gaussian elimination to solve the linear system
Conclusion
Polynomial regression extends linear regression to model non-linear relationships. The C implementation uses matrix operations and Gaussian elimination to compute polynomial coefficients that minimize squared error.
