# How do I solve these simultaneous equations with restrictions?

Let's say I have

$an_1+bn_2+cn_3=n_T$

$ap_1+bp_2+cp_3=p_T$

$ak_1+bk_2+ck_3=k_T$

where $a,b,c \geqslant 0$

What's the best way to find solutions for a, b and c so that the results of the sums are as close as possible to the terms on the right-hand side? My method has been a brute-force search but maybe there's a better way. I tagged this with 'matrices' because I didn't know what else to tag it with.

I've also thought about trying to find the variance of the variances to determine a best fit.

-
So the $n_i$, $p_i$ and $k_i$ are given? What do you mean by "the results of the sums are as close as possible to the terms on the right-hand sides"? –  Johannes Kloos Mar 9 '12 at 16:07

First, rewrite the system as $Ax = d$: $$\begin{pmatrix} n_1 & n_2 & n_3 \\ p_1 & p_2 & p_3 \\ k_1 & k_2 & k_3 \end{pmatrix} \begin{pmatrix} a \\ b \\ c \end{pmatrix} = \begin{pmatrix} n_T \\ p_T \\ k_T \end{pmatrix}$$ If $A$ is non-singular, i.e. $\operatorname{det}(A) \neq 0$, and $d \neq 0$, then the system has one solution, namely $$x = A^{-1} d,$$ which might violate the condition $a,b,c \geqslant 0.$
However, if $\operatorname{det}(A) = 0$, then you are looking for a vector $x$ in the nullspace of $A$. You will have to find the nullspace basis of $A$: $\{ B_1, \ldots, B_{3-r} \}$, where $r = {\rm rank}(A)$. and form a vector $x = \sum B_i$ s.t. $x$ satisfies the conditions.
I'm assuming you're given $n_i$'s, $p_i$'s, $k_i$'s and $n_T, p_T, k_T$. Then you will form to linear system above and solve by $x = A{-1} d$ which will give you a vector of three values $x=(a,b,c)$. Now, in some cases, it happens that ${\rm det}(A) = 0$ and more than one solution exists. What you do in this case, is to find what is called the homogeneous solution. –  user2468 Mar 12 '12 at 5:04
... A simple way to do so, is to move the RHS vector to LHS and have: $$\begin{pmatrix} n_1 & n_2 & n_3 & n_T \\ p_1 & p_2 & p_3 & p_T\\ k_1 & k_2 & k_3 & k_T \end{pmatrix} \begin{pmatrix} a \\ b \\ c \end{pmatrix} =\begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix}$$ The vector $x = (a,b,c)$ you're looking for is a nullspace vector of the matrix above. Think of the matrix as function that maps vectors to vectors. It really scrambles the entries of the vectors. In this case, it scrambles $x$ and produces the zero vector $(0,0,0)$. Finding a nullspace.. –  user2468 Mar 12 '12 at 5:05
...basis is a standard operation in linear algebra. The basis is a general parametric formula for all such $x$'s. What you need to do in this case, is to search for parameters such that $x$ satisfies the conditions $a,b,c \ge 0$. –  user2468 Mar 12 '12 at 5:06