I would like to use Mathematica to find a non-trivial solution of the linear system $Ax = 0$. When I try running Solve[A.x == 0, x] the output I receive is a general solution, i.e. the entries of $x$ are given as linear combinations of some free variables of $x$. Eg. I receive something like {{x4->-x1,x6->x2+x5/2,x7->x3+(3 x5)/2}}, just much longer.

My question is this: how do I "instantiate" a generic solution like the one above? All I care about is that I have a concrete solution returned (which is not the zero vector), so I would like to pick some non-zero values for the free variables (e.g. above $x_1, x_2,x_5, x_3$, and then use the ruleset returned by Solve to generate a concrete solution vector with numeric entries. How can I do this?

  • 1
    $\begingroup$ Please give us an example of your A and x so that potential answers can work with a system similar to what you want to use the solution on. $\endgroup$ Nov 10 '21 at 15:39
  • 4
    $\begingroup$ Have you tried using FindInstance instead of Solve? $\endgroup$
    – Bob Hanlon
    Nov 10 '21 at 15:50
  • $\begingroup$ @CATrevillian, for some integer $N$, $x$ is just defined as x = Table[ToExpression["x" <> ToString[i]], {i, 1, N}];, and A is just an $2N \times N$ matrix of integer entries. I hope this helps. $\endgroup$
    – gen
    Nov 10 '21 at 16:07
  • $\begingroup$ gen, update your question with this information. It can easily be buried & missed in the comments $\endgroup$ Nov 10 '21 at 16:08
  • $\begingroup$ @BobHanlon yes, I tried FindInstance, but the system is trivially satisfied by the zero vector, and this is exactly what FindInstance returns. As I mentioned, I'd like to find a non-trivial solution. $\endgroup$
    – gen
    Nov 10 '21 at 16:12

Using the optional "number of solutions" argument for FindInstance guarantees that it will find at least one non-trivial solution if there is one. Example:

n = 4;
A = RandomChoice[{-1, 1}, {2 n, n}]
x = Table[ToExpression["x" <> ToString[i]], {i, 1, n}]
FindInstance[A . x == 0, x, Reals, 2]

(* {{x1 -> 0, x2 -> 1/2, x3 -> 0, x4 -> -(1/2)}, 
    {x1 -> 0, x2 -> 3/5, x3 -> 0, x4 -> -(3/5)}} *)

Perhaps somewhat surprisingly, neither of the solutions returned are trivial in this case; but if you replace 2 with 1 in the code above, it returns only the trivial solution. I assume that this is due to the fine details of the implementation of FindInstance.

Alternately, you can just include x != 0 in your list of equations and dispense with the optional arguments:

FindInstance[{A . x == 0, x != 0}, x]

(* {{x1 -> 1, x2 -> -1, x3 -> 0, x4 -> 0}} *)

Finally, note that a random $2n \times n$ matrix is likely to be of full rank, meaning that the nullspace will be trivial. You may need to run this code several times before it picks the "right" sort of matrix for $A$ and produces a non-trivial result.

  • $\begingroup$ Thank you for this answer. I'll now try and figure out why I get that error message and then I'll see if this solves my problem. $\endgroup$
    – gen
    Nov 10 '21 at 16:28
  • 1
    $\begingroup$ Yes, this did fix my problem. I have one more question though. Say I save one of the solutions as a variable v = {x1 -> 0, x2 -> 1/2, x3 -> 0, x4 -> -(1/2)}. How do I obtain an actual $n$-long vector w = {0, 1/2, 0, -1/2} from this? Edit: w = x /. v seems to do the trick. $\endgroup$
    – gen
    Nov 10 '21 at 17:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.