title: Back-substitution in Python for 3x3 matrix
body: <pre><code>import numpy as np
  
  
  A = np.matrix([[1,1,-1],[0,1,3],[0,0,-6]])
  b = np.array([9,3,8])
  
  
  def back_sub(A,b):
      n = len(A)
      print('n is:', n)
      x = [0]*n
      for i in range(n-1,-1,-1): #this refers to the rows; i goes 2,1,0
          for j in range(i+1,n): 
              b[i] = b[i] - A[i,j]*x[j]
          x[i] = b[i]/A[i,i]
  
      return x
  </code></pre>
  
  <p>I am trying to back substitute an upper-triangular matrix A, and come up with an answer: x1=4/3,x2=7,x3=−4/3
  .</p>
  
  <p>When I run my code, it gives correct answer for x2
   and x3
  , but x1
   comes out as 0, which is confounding. I have tried to debug my code, and it seems to run fine until i=0
  . Why isn't this code giving me the correct value of x1
  ?</p>