Least squares solution to simultaneous equations

In summary, the conversation discusses trying to fit a transformation from one set of coordinates to another using the formula x' = R + Px + Qy and y' = S - Qx + Py, where P, Q, R, and S are constants. One person is having trouble finding an error estimate for the fit and asks for an example, tutorial, or code sample. Someone suggests minimizing the sum over the distances of (x',y') to their nearest neighbors (x,y) and provides an equation for this. Another person offers an alternative approach using equations and solving for the unknowns. The conversation then shifts to discussing how to calculate the residual and errors in the parameters. Finally, someone mentions that the best approach may be to
  • #1
mgb_phys
Science Advisor
Homework Helper
7,902
15
I am trying to fit a transformation from one set of coordiantes to another.
x' = R + Px + Qy
y' = S - Qx + Py

Where P,Q,R,S are constants, P = scale*cos(rotation). Q=scale*sin(rotation)

There is a well known 'by hand' formula for fitting P,Q,R,S to a set of corresponding points.
But I need to have an error estimate on the fit - so I need a least squares solution.

I'm having trouble working out how to do this for first principles for data sets with x and y in them.
Can anyone point to an example/tutorial/code sample of how to do this ?
 
Mathematics news on Phys.org
  • #2
Why don't you try to minimize the sum over the distances of (x',y') to their nearest neighbors (x,y) (there are algorithms for finding those)
 
  • #3
I was thinking along similar lines. If y' = f(x,y,R,P,Q) and x' = g(x,y,S,P,Q) then one could minimize:

1/2 sum [f - y']^2 + 1/2 sum [g - x']^2
 
  • #4
hotvette said:
I was thinking along similar lines. If y' = f(x,y,R,P,Q) and x' = g(x,y,S,P,Q) then one could minimize:

1/2 sum [f - y']^2 + 1/2 sum [g - x']^2

Seems like the right approach to me. This could be done in Excel, using the Solver add-in to minimize the sum.

The 1/2 factors aren't necessary.
 
  • #5
I found http://mathworld.wolfram.com/LeastSquaresFitting.htm
Interstingly the 100year old 'by hand' instructions I had from an old Army surveying manual is almost exactly the same algorithm
 
Last edited by a moderator:
  • #7
Redbelly98 said:
This could be done in Excel, using the Solver add-in to minimize the sum

Maybe Excel Solver could do it, but it would be much more fun to write out the equations and solve them. :smile:
 
  • #8
hotvette said:
Maybe Excel Solver could do it, but it would be much more fun to write out the equations and solve them. :smile:

If one really wants to do it that way, start with hotvette's equation:

hotvette said:
I was thinking along similar lines. If y' = f(x,y,R,P,Q) and x' = g(x,y,S,P,Q) then one could minimize:

1/2 sum [f - y']^2 + 1/2 sum [g - x']^2

To minimize the sum, take partial derivitives w.r.t. P, Q, R, S and set each expression equal to zero. That gives 4 linear equations in 4 unknowns to be solved.

Writing out the sum in full:

χ² = (1/2)∑[(Px + Qy + R - x')2 + (-Qx + Py + S - y')2]

Next, set ∂χ² / ∂P = 0:

χ² / ∂P = ∑[(Px + Qy + R - x')x + (-Qx + Py + S - y')y]
= ∑[(Px² + Qxy + Rx - x'x) + (-Qxy + Py² + Sy - y'y)]

= P[∑x² + ] + Rx + Sy - x'x - y'y = 0

And similarly for ∂χ²/∂Q, ∂χ²/∂R, and ∂χ²/∂S.
 
  • #9
Not sure how to get r2, or errors in the parameters though.
 
  • #10
Continuing the partial differentiation, I get the following:

[tex]
\begin{bmatrix}
\sum(x^2+y^2) & 0 & \sum x & \sum y \\
0 & \sum(x^2+y^2) & \sum y & -\sum x \\
\sum x & \sum y & m & 0 \\
\sum y & -\sum x & 0 & m
\end{bmatrix}
\begin{bmatrix}P \\ Q \\ R \\ S \end{bmatrix}
= \begin{bmatrix} \sum (y'y + x'x) \\ \sum (x'y - y'x) \\ \sum x' \\ \sum y' \end{bmatrix}
[/tex]

Re the residual, I think it is just [itex]r^2 = \sum (S-Qx+Py-y')^2 + \sum (R + Px + Qy - x')^2[/itex] which is equivalent to:

[tex] r^2 = (Az-y')^T (Az-y') + (Bz-x')^T (Bz-x') [/tex]

where:

[tex]
A = \begin{bmatrix}
y_1 & -x_1 & 0 & 1 \\ y_2 & -x_2 & 0 & 1 \\ \vdots & \vdots & \vdots & \vdots \\ y_m & -x_m & 0 & 1
\end{bmatrix}
\quad
B=\begin{bmatrix}
x_1 & y_1 & 1 & 0 \\ x_2 & y_2 & 1 & 0 \\ \vdots & \vdots & \vdots & \vdots \\ x_m & y_m & 1 & 0
\end{bmatrix}
\quad
z = \begin{bmatrix}P \\ Q \\ R \\ S \end{bmatrix}
\quad
y' = \begin{bmatrix} y_1' \\ y_2' \\ \vdots \\ y_m' \end{bmatrix}
\quad
x' = \begin{bmatrix} x_1' \\ x_2' \\ \vdots \\ x_m' \end{bmatrix}
[/tex]
 
  • #11
hotvette said:
Re the residual, I think it is just [itex]r^2 = \sum (S-Qx+Py-y')^2 + \sum (R + Px + Qy - x')^2[/itex] which is equivalent to:

[tex] r^2 = (Az-y')^T (Az-y') + (Bz-x')^T (Bz-x') [/tex]

I was thinking of the correlation coefficient r, not the residual sum of squares (often denoted by RSS).
 
  • #12
I've been looking at it and the problem with getting errors directly from the equations is that the coefficients are so orthogonal. A small error in the sin./cos terms is much more significant than in the origin.

What I did was find the fit and then work out the mismatch for each of the known set of points and then use the statistics of that.
 
  • #13
mgb_phys said:
What I did was find the fit and then work out the mismatch for each of the known set of points and then use the statistics of that.

Makes sense. Does the mismatch look reasonably Gaussian?
 
  • #14
Redbelly98 said:
Makes sense. Does the mismatch look reasonably Gaussian?
Too few points to tell.
In reality the error is likely to be due to an outlier where one match is just completely wrong.
Best approach is some iterative removal of outliers - but the specs call for a statistical measure of accuracy.
 

Related to Least squares solution to simultaneous equations

1. What is the least squares solution to simultaneous equations?

The least squares solution to simultaneous equations is a method used to find the best fitting line or curve that represents a set of data points. It is often used in statistical analysis and regression to minimize the sum of the squared differences between the observed data and the predicted values.

2. How is the least squares solution calculated?

The least squares solution is calculated by finding the values of the coefficients in the linear equation that minimize the sum of the squared residuals (differences between the observed data and the predicted values). This is done by using a system of equations and solving for the coefficients using matrix operations.

3. Can the least squares solution be used for non-linear data?

Yes, the least squares solution can be used for non-linear data by transforming the data into a linear form. This can be done by taking the logarithm or square root of the data, or by using polynomial regression techniques. Once the data is transformed, the least squares solution can be applied to find the best fitting line or curve.

4. What is the difference between the least squares solution and the normal equations method?

The least squares solution and the normal equations method are both used to find the best fitting line or curve for a set of data points. However, the normal equations method involves solving a system of equations directly, while the least squares solution involves finding the solution through matrix operations. In some cases, the normal equations method may be faster, but the least squares solution is more flexible and can be used for non-linear data.

5. When is the least squares solution not appropriate to use?

The least squares solution is not appropriate to use when there are outliers or influential points in the data that significantly affect the results. In these cases, the least squares solution may not accurately represent the overall trend of the data. It is important to identify and address outliers before using the least squares solution to avoid misleading results.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
1K
Replies
3
Views
2K
Replies
13
Views
3K
  • Advanced Physics Homework Help
Replies
0
Views
288
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
532
  • Programming and Computer Science
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
994
  • General Math
Replies
2
Views
1K
  • General Math
Replies
19
Views
5K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
Back
Top