How to solve this problem in matrix way

60 Views Asked by At

Suppose there're 3 kind of goods, A, B, C. And 5 dollar (or whatever currency) for 1 A, 3 for 1 B, and 1 for 3 Cs.

Find all possible combinations such that spend 100 (exactly) and get 100 goods in total (regardless of the kind).

Let x,y,z denote the amount for A, B, C respectively.

It's easy to get the relationship of x,y,z.

\begin{equation}\begin{aligned} x &= -100 + 4z\\ y &= 200 - 7z\end{aligned} \end{equation}

And there're four possible combinations:

0, 25, 25
4, 18, 26
8, 11, 27
12, 4, 28

Now, let $\vec{A}=(5,1),\vec{B}=(3,1),\vec{C}=(1,3), \vec{G}=(100,100)$

The problem becomes find all $\vec{V}=(x,y,z)$ such that $x\vec{A}+y\vec{B}+z\vec{C}=\vec{G}$

i.e

\begin{equation} \vec{V}\left(\begin{array}{cc}\vec{A}\\ \vec{B}\\ \vec{C}\end{array}\right)=\vec{G} \\ \left[\begin{array}{cc} x & y & z \end{array}\right] \left[\begin{array}{cc} 5 & 1\\ 3 & 1\\ 1 & 3 \end{array}\right] = \left[\begin{array}{cc} 100 & 100 \end{array}\right] \end{equation}

I'm really confused at solving this $\vec{V}$

1

There are 1 best solutions below

1
On BEST ANSWER

The goal in doing this with matrices is to be able to quickly get the equations for the variables by taking advantage of matrix inversion and multiplication.
Since we have three variables and only two equations, there's a degree of freedom we want to eliminate. Let's 'forget' about $x$ temporarily, by moving it to the other side of the equation, and then we'll be left with a $2 \times 2$ matrix that we can usefully invert.

$$ \begin{bmatrix} y & z \end{bmatrix} \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix} = \begin{bmatrix} 100 - 5x & 100 - x \end{bmatrix} $$

Indeed now the $2 \times 2$ matrix on the left is invertible, $$ \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}^{-1} = \frac{1}{8}\begin{bmatrix} 3 & -1 \\ -1 & 3 \end{bmatrix} $$ So we get $$ \begin{bmatrix} y & z \end{bmatrix} = \frac{1}{8}\begin{bmatrix} 100 - 5x & 100 - x \end{bmatrix} \begin{bmatrix} 3 & -1 \\ -1 & 3 \end{bmatrix} = \frac{1}{8}\begin{bmatrix} 200 + 14x & 200 - 2x \end{bmatrix} $$

In other words $$ y = 25 - 7x/4$$ and $$z = 25 + x/4$$

Since you're only interested in integer solutions, you know that $x$ is divisible by $4$. Since you're only interested in solutions for which $x,y,z$ positive, the equation for $y$ says that $25 - 7x/4 \geq 0$ so $x < 16$.

This gives the solutions

$(0, 25, 25), (4, 18, 26), (8, 11, 27), (12, 4, 28)$, as you said.