From: Arne Vajhøj on
On 11-06-2010 03:39, Andreas Bauer wrote:
> Hello Arne,
>>> --Simple linear regression, or?
>>
>> Yes.
>>
>> Simple linear regression.
>>
>> http://www.mathdotnet.com/
>>
>> can do it.
>>
>> Some old code from the shelf:
>>
>> private static void Solve(double[] y, out double[] b, double[] x)
>> {
>> Vector one = Vector.Ones(x.Length);
>> Vector x2 = Vector.Create(x);
>> Matrix x3 = Matrix.CreateFromColumns(new List<Vector> { one, x2 });
>> Vector y2 = Vector.Create(y);
>> Matrix y3 = Matrix.CreateFromColumns(new List<Vector> { y2 });
>> Matrix b3 = x3.Solve(y3);
>> Vector b2 = b3.GetColumnVector(0);
>> b = b2.CopyToArray();
>> }
> it is not clear.
> On my Excel sheet, you see may problem
>
> Two axis
>
> x
> y
>
> Customer insert the values in mm, axis controller needs steps.
> First I should calibrate the system.
> http://www.fileuploadx.de/49586
>
> How?
> How I get a,b and the offset c?

If you have 3 values for each of fx and fy, then it is
simple linear regression.

Arne
From: Andreas Bauer on
Hello Arne,

>
> If you have 3 values for each of fx and fy, then it is
> simple linear regression.


sorry.
http://www.fileuploadx.de/492401

First I have a axis x
The user want to insert the values in mm.
But I should send in C++ or in C# the steps,units to the x controller
(Step motor)

In C# I found a code, but I'm not sure is working or not.
[Linear regression of polynomial coefficients]
http://www.trentfguidry.net/post/2009/08/01/Linear-regression-polynomial-coefficients.aspx

[Linear and multiple linear regression]
http://www.trentfguidry.net/post/2009/07/19/Linear-multiple-regression.aspx

I should also to understand the algorithm by hand. That is my problem.
Where is a good instruction.
Maybe you can help me.

Thanks a lot.

Regards Andreas

For a first order polynomial (a line), the equation is:
Y = A + BX
For this equation, z0 is 1 and z1 is X, and y is Y.
This is done in the code shown below.
double[] x = new double[] { 2.3601, 2.3942, 2.4098, 2.4268,
2.4443, 2.4552, 2.4689, 2.4885, 2.5093, 2.5287 };
double[] y = new double[] { 133.322, 666.612, 1333.22,
2666.45, 5332.9, 7999.35, 13332.2, 26664.5, 53329, 101325 };
int nPolyOrder = 1;
double[,] z = new double[y.Length, nPolyOrder + 1];
for (int i = 0; i < y.Length; i++)
{
z[i, 0] = 1.0;
z[i, 1] = x[i];
}
double[] coefs = Polynomial.Regress(z, y);


public static double[] Regress(double[,] z, double[] y)
{
//y=a0 z1 + a1 z1 +a2 z2 + a3 z3 +...
//Z is the functional values.
//Z index 0 is a row, the variables go across index 1.
//Y is the summed value.
//returns the coefficients.
Debug.Assert(z != null && y != null);
Debug.Assert(z.GetLength(0) == y.GetLength(0));

Matrix zMatrix = z;
Matrix zTransposeMatrix = zMatrix.Transpose(); KENNZEICHENooooo
Matrix leftHandSide = zTransposeMatrix * zMatrix;
Matrix rightHandSide = zTransposeMatrix * y;
Matrix coefsMatrix = leftHandSide.SolveFor(rightHandSide);
KENNZEICHENooooo

return coefsMatrix;
}
From: Andreas Bauer on
http://www1.minpic.de/bild_anzeigen.php?id=114609&key=55273016&ende

Hello,
the next problem is, I dimension x and y.
The new x, ist depend from y.

Measure
mm x mm y Units X Units Y
173,000 12,500 130,816 266,000
275,500 28,500 196,860 256,000
139,500 153,000 113,987 175,000


Greeting Andreas
From: Arne Vajhøj on
On 12-06-2010 15:00, Andreas Bauer wrote:
> >
> > If you have 3 values for each of fx and fy, then it is
> > simple linear regression.
>
>
> sorry.
> http://www.fileuploadx.de/492401

Ah. Units x is fx.

Then it is a very simple linear regression.

using System;
using System.Collections.Generic;

using MathNet.Numerics.LinearAlgebra;

namespace E
{
public class Program
{
private static void Solve(double[] f, out double[] b, double[]
x, double[] y)
{
Vector one = Vector.Ones(x.Length);
Vector x2 = Vector.Create(x);
Vector y2 = Vector.Create(y);
Matrix xy3 = Matrix.CreateFromColumns(new List<Vector> {
x2, y2, one });
Vector f2 = Vector.Create(f);
Matrix f3 = Matrix.CreateFromColumns(new List<Vector> { f2 });
Matrix b3 = xy3.Solve(f3);
Vector b2 = b3.GetColumnVector(0);
b = b2.CopyToArray();
}
public static void Main(string[] args)
{
double[] fx = { 130.816, 196.860, 113.987 };
double[] fy = { 266.000, 256.000, 175.000 };
double[] x = { 173.0, 275.5, 139.5 };
double[] y = { 12.5, 28.5, 153-0 };
double[] b;
Solve(fx, out b, x, y);
Console.WriteLine("fx = " + b[0] + "*x + " + b[1] + "*y + "
+ b[2]);
Solve(fy, out b, x, y);
Console.WriteLine("fy = " + b[0] + "*x + " + b[1] + "*y + "
+ b[2]);
Console.ReadKey();
}
}
}

outputs:

fx = 0,639237208990945*x + 0,0326366299017556*y + 19,8200049707945
fy = 0,00341428308423573*x + -0,646872751008385*y + 273,495238414032

which looks as the results you have found in Excel.

Arne
From: Andreas Bauer on
Hello Arne,
>
> Then it is a very simple linear regression.
>
> using System;
> using System.Collections.Generic;
>
> using MathNet.Numerics.LinearAlgebra;
>
> namespace E
> {
> public class Program
> {
> private static void Solve(double[] f, out double[] b, double[]
> x, double[] y)
> {
> Vector one = Vector.Ones(x.Length);
> Vector x2 = Vector.Create(x);
> Vector y2 = Vector.Create(y);
> Matrix xy3 = Matrix.CreateFromColumns(new List<Vector> { x2,
> y2, one });
> Vector f2 = Vector.Create(f);
> Matrix f3 = Matrix.CreateFromColumns(new List<Vector> { f2 });
> Matrix b3 = xy3.Solve(f3);
> Vector b2 = b3.GetColumnVector(0);
> b = b2.CopyToArray();
> }
> public static void Main(string[] args)
> {
> double[] fx = { 130.816, 196.860, 113.987 };
> double[] fy = { 266.000, 256.000, 175.000 };
> double[] x = { 173.0, 275.5, 139.5 };
> double[] y = { 12.5, 28.5, 153-0 };
> double[] b;
> Solve(fx, out b, x, y);
> Console.WriteLine("fx = " + b[0] + "*x + " + b[1] + "*y + "
> + b[2]);
> Solve(fy, out b, x, y);
> Console.WriteLine("fy = " + b[0] + "*x + " + b[1] + "*y + "
> + b[2]);
> Console.ReadKey();
> }
> }
> }
>
> outputs:
>
> fx = 0,639237208990945*x + 0,0326366299017556*y + 19,8200049707945
> fy = 0,00341428308423573*x + -0,646872751008385*y + 273,495238414032
>
> which looks as the results you have found in Excel.

fine, thank you very much.

Two additional question. I hope is ok.
What is exactly the theoretical math. background.
Linear regression? I have a relation from x to y.
Can you show me, if I must calculate by hand, I need the correct way,
the correct math. formular.

My problem.
I have a new task.
I should find a solution.
If I know not the correct headline, is not possible to search via
www.google.com or via www.bing.com

Greeting Andreas