C1W2: Help with Programming Assignment back_substitution step

Hello! Really excited about this course!

I’d like some help debugging/understand the back_substitution step of the Gaussian Elimination Algorithm programming assignment.

First, I got an error that one of my integers was not subscriptable in python. After googling this exception, I think I fixed it by getting “value” as an index of M. Is this correct?

Also, I ran into an exception with the back_substitution function. It’s not passing the check_null_matrix test, and is outputting [6 7 7] instead of [5 6 7], and I see the exception below.
“An exception was thrown while running your function: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices.”
Any advice? How can I go about this?

Just as in the case of Row Echelon Form, they give you an example in the instructions and then walk through the steps carefully to show you what your algorithm needs to do. In order to debug this, my suggestion would be to instrument your code with print statements so that you can see what it is doing. Then compare that to the example to figure out where you are going off the rails.

I did that with my code and then created a separate test cell and ran it. Here’s the test cell:

test_M = np.array([[1, -1, 0.5, 0.5], [0, 1, 2, -1], [0, 0, 1, -1]])
test_Out = back_substitution(test_M)
print(f"test_Out {test_Out}")

Then with all the prints in my back_substitution code, here’s the output of running that test:

M initial =
[[ 1.  -1.   0.5  0.5]
 [ 0.   1.   2.  -1. ]
 [ 0.   0.   1.  -1. ]]
row 2
sub_row = [ 0.  0.  1. -1.]
j 0
row_to_reduce [ 1.  -1.   0.5  0.5]
M [[ 1. -1.  0.  1.]
 [ 0.  1.  2. -1.]
 [ 0.  0.  1. -1.]]
j 1
row_to_reduce [ 0.  1.  2. -1.]
M [[ 1. -1.  0.  1.]
 [ 0.  1.  0.  1.]
 [ 0.  0.  1. -1.]]
row 1
sub_row = [0. 1. 0. 1.]
j 0
row_to_reduce [ 1. -1.  0.  1.]
M [[ 1.  0.  0.  2.]
 [ 0.  1.  0.  1.]
 [ 0.  0.  1. -1.]]
row 0
sub_row = [1. 0. 0. 2.]
M final =
[[ 1.  0.  0.  2.]
 [ 0.  1.  0.  1.]
 [ 0.  0.  1. -1.]]
solution = [ 2.  1. -1.]
test_Out [ 2.  1. -1.]
2 Likes