How do I tell if a solution to a set of equations is valid in Math.NET Numerics?

Viewed 90

Trying to solve a system of linear equations using Math.NET Numerics but wont know in advance whether they will have a valid solution.

For example, the equations x + y = 10, x = 3, y = 7, have an obvious solution.

In Math.NET, we programmed this as (making it a square matrix)

var A1 = Matrix<double>.Build.SparseOfArray(new double[,]
{
    { 1, 1, 0 },
    { 1, 0, 0 },
    { 0, 1, 0 },
});
var b1 = Vector<double>.Build.Dense(new double[] { 10, 3, 7 });

We tried to solve it like this

var x1 = A1.Solve(b1);

But that returns NaN for x and y.

Following the advice online we tried the solve it like this

var p1 = A1.PseudoInverse();
var x1 = p1 * b1;

Which returned the correct solutions x = 3 and y = 7.

We then tried an inconsistent set of equations x + y = 10, x = 3, y = 6, and to our surprise it produced a solution x = 3.333333, y = 6.333333 with no indication this is not a valid solution.

How do we get Math.NET to solve a set of equations, which may be inconsistent or may have some redundancy, and get some indication that the solution is valid?

0 Answers
Related