This post expands on the previous post about Linear and Multiple Linear Regression in C# and gives examples of linear regression of polynomial coefficients.
In the previous post, I used the .NET 4.0 Matrix class with parallel processing, however, this should also work by adding the Regress function to the .NET 3.5 Matrix class. I will avoid using the parallel processing classes in this example so that it will work with .NET 3.5.
For this example, I will use the data shown below. I intentionally picked data that is difficult to regress a polynomial to because it would be kind of boring to regress a second order or higher polynomial to data that is pretty much linear.
2.3601

133.32

2.3942

666.61

2.4098

1333.22

2.4268

2666.45

2.4443

5332.90

2.4552

7999.35

2.4689

13332.25

2.4885

26664.50

2.5093

53329.00

2.5287

101325.09

For a first order polynomial (a line), the equation is:
Y = A + BX
For this equation, z_{0} is 1 and z_{1} is X, and y is Y.
This is done in the code shown below.
double[] dX=new double[]{2.3601, 2.3942, 2.4098, 2.4268, 2.4443, 2.4552, 2.4689, 2.4885, 2.5093, 2.5287};
double[] dY = new double[] { 133.322, 666.612, 1333.22, 2666.45, 5332.9, 7999.35, 13332.2, 26664.5, 53329, 101325 };
int nPolyOrder = 1;
double[,] dZ = new double[dY.Length, nPolyOrder+1];
for (int i = 0; i < dY.Length; i++)
{
dZ[i, 0] = 1.0;
dZ[i, 1] = dX[i];
}
double[] dCoefs = MatrixNumeric.Regress(dZ, dY);
This gives A = 1212289, B = 503788.92 and the data below.
X

Y

Fitted

2.3601

133.32

23286.98

2.3942

666.61

6123.37

2.4098

1333.22

1765.81

2.4268

2666.45

10283.15

2.4443

5332.90

19111.49

2.4552

7999.35

24626.26

2.4689

13332.25

31497.13

2.4885

26664.50

41379.54

2.5093

53329.00

51853.08

2.5287

101325.09

61653.87

For a second order polynomial, the equation is:
Y = A + BX + CX^{2}
For this equation, z_{0} is 1, z_{1} is X, z_{2} is X^{2}, and y is Y.
This is done in the code shown below.
double[] dX=new double[]{2.3601, 2.3942, 2.4098, 2.4268, 2.4443, 2.4552, 2.4689, 2.4885, 2.5093, 2.5287};
double[] dY = new double[] { 133.322, 666.612, 1333.22, 2666.45, 5332.9, 7999.35, 13332.2, 26664.5, 53329, 101325 };
int nPolyOrder = 2;
double[,] dZ = new double[dY.Length, nPolyOrder+1];
for (int i = 0; i < dY.Length; i++)
{
dZ[i, 0] = 1.0;
dZ[i, 1] = dX[i];
dZ[i, 2] = dX[i] * dX[i];
}
double[] dCoefs = MatrixNumeric.Regress(dZ, dY);
This gives A = 36759078.32, B = 30552595.42, C = 6347521.4 and the data below.
X

Y

Fitted

2.3601

133.32

8037.42

2.3942

666.61

4722.08

2.4098

1333.22

5643.88

2.4268

2666.45

3144.23

2.4443

5332.90

3276.49

2.4552

7999.35

9265.54

2.4689

13332.25

18855.64

2.4885

26664.50

36789.79

2.5093

53329.00

61128.71

2.5287

101325.09

88873.81

For a third order polynomial, the equation is:
Y = A + BX + CX^{2} + DX^{3}
For this equation, z_{0} is 1, z_{1} is X, z_{2} is X^{2}, z_{3} is X^{3}, and y is Y.
This is done in the code shown below.
double[] dX=new double[]{2.3601, 2.3942, 2.4098, 2.4268, 2.4443, 2.4552, 2.4689, 2.4885, 2.5093, 2.5287};
double[] dY = new double[] { 133.322, 666.612, 1333.22, 2666.45, 5332.9, 7999.35, 13332.2, 26664.5, 53329, 101325 };
int nPolyOrder = 3;
double[,] dZ = new double[dY.Length, nPolyOrder+1];
for (int i = 0; i < dY.Length; i++)
{
dZ[i, 0] = 1.0;
dZ[i, 1] = dX[i];
dZ[i, 2] = dX[i] * dX[i];
dZ[i, 3] = dX[i] * dX[i] * dX[i];
}
double[] dCoefs = MatrixNumeric.Regress(dZ, dY);
This gives A = 830167848.92, B = 1034157557.04, C = 429397609.64, D = 59427310.55, and the data below.
X

Y

Fitted

2.3601

133.32

1114.68

2.3942

666.61

3407.84

2.4098

1333.22

2643.43

2.4268

2666.45

1988.25

2.4443

5332.90

3291.59

2.4552

7999.35

5970.15

2.4689

13332.25

12152.31

2.4885

26664.50

28292.46

2.5093

53329.00

57429.76

2.5287

101325.09

98694.28

For a fourth order polynomial, the equation is:
Y = A + BX + CX^{2} + DX^{3} + EX^{4}
For this equation, z_{0} is 1, z_{1} is X, z_{2} is X^{2}, z_{3} is X^{3}, z_{4} is X^{4}, and y is Y.
This is done in the code shown below.
double[] dX=new double[]{2.3601, 2.3942, 2.4098, 2.4268, 2.4443, 2.4552, 2.4689, 2.4885, 2.5093, 2.5287};
double[] dY = new double[] { 133.322, 666.612, 1333.22, 2666.45, 5332.9, 7999.35, 13332.2, 26664.5, 53329, 101325 };
int nPolyOrder = 4;
double[,] dZ = new double[dY.Length, nPolyOrder+1];
for (int i = 0; i < dY.Length; i++)
{
dZ[i, 0] = 1.0;
dZ[i, 1] = dX[i];
dZ[i, 2] = dX[i] * dX[i];
dZ[i, 3] = dX[i] * dX[i] * dX[i];
dZ[i, 4] = dX[i] * dX[i] * dX[i] * dX[i];
}
double[] dCoefs = MatrixNumeric.Regress(dZ, dY);
This gives A = 9470307327.9, B = 15832920218.37, C = 9925973640.82, D = 2765589751.22, E = 288948184.43, and the data below.
X

Y

Fitted

2.3601

133.32

255.30

2.3942

666.61

1289.77

2.4098

1333.22

1932.21

2.4268

2666.45

2786.55

2.4443

5332.90

4755.05

2.4552

7999.35

7217.83

2.4689

13332.25

12526.31

2.4885

26664.50

26899.26

2.5093

53329.00

55407.42

2.5287

101325.09

100200.45
