lemmingapex / trilateration

Solves a formulation of n-D space trilateration problem using a nonlinear least squares optimizer
MIT License
398 stars 106 forks source link

SingularMatrixException on Error Calculation #2

Closed bleckers closed 8 years ago

bleckers commented 8 years ago

Hi Scott,

It seems the following two lines both give a SingularMatrixException when run:

// error and geometry information RealVector standardDeviation = optimum.getSigma(0); RealMatrix covarianceMatrix = optimum.getCovariances(0);

I cannot get it to work with any input data. For example, here are some positions that throw the error (these are a Mercator projection of GPS coordinates): Positions[0]: 0 = -4565919.193832541 1 = 1.6141672528603023E7 2 = 2200.0

Positions[1]: 0 = -4565920.313515583 1 = 1.6141679993156526E7 2 = 2200.0

Positions[2]: 0 = -4565927.778069112 1 = 1.6141678948119089E7 2 = 2200.0

Positions[3]: 0 = -4565926.658386086 1 = 1.6141671334274463E7 2 = 2200.0

Distances: 0 = 1.6 1 = 1.6 2 = 1.6 3 = 1.6

Would you have any idea as to why this might be the case? This is running on Android with the same (recently updated) Apache commons Math3 library you are using. It also doesn't work with the previous version of the library.

Calculating the point estimate works perfectly fine.

Regards,

Blair

bleckers commented 8 years ago

Aha!

It seems to only happen when using a third position (altitude) in the Positions array. When you remove this it calculates both of these just fine.

Creating a separate function to calculate the error with only a two element position (lat/lng) allows you to calculate this just fine.

lemmingapex commented 8 years ago

Hi Blair,

I have seen this exception before when requesting the error information. It likely means that this information is meaningless.

When you request sigma, the covariance of the Jacobian matrix is determined in a QRDecomposition. The double threshold argument you provide to getSigma is a way for the caller to specify that the result of this computation should be considered meaningless, and thus trigger an exception. In your above example, change optimum.getSigma(0); and optimum.getCovariances(0); to optimum.getSigma(-1); and optimum.getCovariances(-1); respectively. This will likely cause you to obtain NaN entries in your results. Meaningless, right?

Do you think if the above information was better documented, this would avoid confusion in the future? What do you think?

Thanks, Scott

bleckers commented 8 years ago

I think a brief explanation in the readme on the threshold value and an example on how to handle a meaningless result/exception should suffice. It just threw me because the optimum was calculated just fine.

lemmingapex commented 8 years ago

@bleckers, I put a short comment in the README. Take a look, let me know what you think. Can I close this issue?

bleckers commented 8 years ago

Maybe something about what input variables might constitute a SingularMatrixException? Most people will be using this library without going into detail about the maths involved and it would be useful to know when they might experience the error (like for example above when using a 3D coordinate space).

lemmingapex commented 8 years ago

Everything related to SingularMatrixException is well documented in apache's docs: https://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/fitting/leastsquares/LeastSquaresProblem.Evaluation.html#getSigma(double)

Users looking to find out more about how these methods should look there.