The gaussian elimination and back-substitution codes were running fine, but I can't get it to work all together yet. If anybody has useful advice, tips or tricks, I would be so grateful.

Note: This is not an assignment for school. I'm just trying to learn coding. It's my first coding language and I don't really know a ton about computers. I didn't write this code either, I found it in a book that was written by my department head. The pivoting code block was taken from another book. I'm going through this as an exercise. Trying to learn!!! Thanks in advance.

Ok, here it is:

import numpy def bobcatpivotLU(A,f): n = f.size #Begin by check for compatible matrix and rhs sizes #A can only be a square matrix...? #Note: == and != do opposite things. if (A.shape[0] != n or A.shape[1] != n): print "Error! Incompatible array and vector sizes." return f #Loop through the columns of the matrix. #Eliminate lower triangular part. for i in range(0,n-1): #Loop through the rows below the diagonal for each column. for j in range(i+1,n): if A[i,i] == 0: #PIVOTING CODE BLOCK STARTS HERE: #Used to simply give a message saying "Zero on the diagonal! Must pivot!" def pivot(A): # Basic row pivoting m = len(A); n = len(A[0]) for j in range(min(n,m)): # for each column on the main diag if(A[j][j]==0): # Find a non-zero pivot and swap rows thecolumn = [A[k][j] for k in range(j,m)] ipivot = thecolumn.index(max(thecolumn)) temp = A[j]; A[j] = A[ipivot]; A[ipivot] = temp for i in range(j+1,m): c = A[i][j]/A[j][j] # Ratio of (i,j) elt by (j,j) (diagonal) elt A[i] = [A[i][k]-c*A[j][k] for k in range(n)] return A #END PIVOTING PORTION OF CODE, THAT I TRIED UNSUCCESSFULLY TO INSERT INTO MY WORKING CODE. return f m = A[j,i]/A[i,i] A[j,:] = A[j,:] - m*A[i,:] f[j] = f[j] - m*f[i] return A,f def bobcatpivotBS(A,f): n = f.size #Begin by check for compatible matrix and rhs sizes if (A.shape[0] != n or A.shape[1] != n): print "Error! Incompatible array and vector sizes." return f #initialize the solution vector, x, to zero. x = numpy.zeros((n,1)) #Solve for last entry first. x[n-1] = f[n-1]/A[n-1,n-1] #loop from the end to the beginning for i in range(n-2,-1,-1): sum = 0 # for all known x values, sum and move to rhs for j in range(i+1,n): sum = sum + A[i,j]*x[j] x[i] = (f[i] - sum)/A[i,i] return x

Please go easy on me I'm a beginner.

I have a very beginner-ey question too actually: What does the return function do? When it says "return A" or "return f" what does it do?

Please forgive my ignorance.

THANK YOU!!!

This post has been edited by **andrewsw**: 22 January 2015 - 04:54 PM

Reason for edit:: Removed the word Python from topic title