Now Solve Puzzle Using Linear Algebra
This seventh article in the mathematical journey through open source solves puzzles using linear algebra in Octave.
Matrix maths is formally called linear algebra. We have covered its basics in an earlier article. Now, let’s apply what we learnt by solving puzzles. Solving ‘purchase puzzles’ Shrishti purchased 24 pencils and N2 erasers for ` 96. Divya purchased 2M pencils and N5 erasers for ` NMM. What are the prices of the pencil and the eraser?
Assuming that ‘p’ is the price for pencils and ‘e’ is the price for erasers, we have the following two equations: 24 * p + N2 * e = 96 2M * p + N5 * e = NMM Hence, we could get the values of ‘ p’ and ‘ e’ by solving these equations. Converting them into linear algebra form, they can be re- written using matrix multiplication as follows: 24 12 p 96 20 15 e = NMM which is Ax = b, ‘x’ being the vector with variables ‘p’ and ‘e’. Hence, we need to find the value of ‘x’, which is
given by: x = A- Nb. Using Octave: $ octave -qf octave:1> A = [ > 24 12 > 20 15 > ]; octave:2> b = [ > 96 > 100 > ]; octave:3> x = inv(A) * b x= 2.0000 4.0000
Hence, p = ` 2 and e = ` 4, i.e., each pencil costs ` 2 and an eraser costs ` 4. You may check by substituting these