#!/usr/bin/env python # coding: utf-8 # # Matrix solution for a set of linear equations # It can be tedious to solve a set of three or more linear equations. However, the equations can be rewritten in matrix form and solved computationally. Suppose that you want to solve the equations # $$ # 50 I_1 +180 I_2 = 25, \\ # 10 I_1 + 100 I_2 - 90 I_3 = 0, \\ # 40 I_1 + 200 I_3 = 25. # $$ # These equations can be rewritten as # $$ # \left[ \begin{array}{ccc} # 50 & 180 & 0 \\ # 10 & 100 & -90 \\ # 40 & 0 & 200 \end{array} \right] # \left[ \begin{array}{c} # I_1 \\ # I_2 \\ # I_3 \end{array} \right] = # \left[ \begin{array}{c} # 25 \\ # 0 \\ # 25 \end{array} \right]. # $$ # Define the matrices # $$ # a = # \left[ {\begin{array}{ccc} # 50 & 180 & 0 \\ # 10 & 100 & -90 \\ # 40 & 0 & 200\\ # \end{array} } \right], # $$ # $$ # x = # \left[ {\begin{array}{cc} # I_1 \\ # I_2 \\ # I_3 \\ # \end{array} } \right], # $$ # and # $$ # b = # \left[ {\begin{array}{cc} # 25 \\ # 0 \\ # 25 \\ # \end{array} } \right]. # $$ # We must solve the following matrix equation # $$ ax = b$$ # for $x$. If the multiplicative inverse of the matrix $a$ is $a^{-1}$, then the solution is # $$x = a^{-1}b.$$ # It is easy to find the solution with numpy as shown below. If the two objects being multiplied are matrices, the multiplication operator (*) performs matrix multiplication. Documentation of the matrix class is available at http://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html. # # In[1]: from numpy import * a = matrix([[50, 180, 0], [10, 100, -90], [40, 0, 200]]) b = matrix([[25], [0], [25]]) # a.I is the multiplicative inverse of the matrix a x = (a.I)*b print(x) # This means that # $$ # I_1 = -11.875, \\ # I_2 = 3.4375, \\ # I_3 = 2.5. # $$ # If the equations to be solved involve physical quantities, you must keep track of units. In that case, it is best to convert all quantities to standard units so that the results will be in standard units. # If there is no solution to the equations, the matrix inversion will result in an error.