Amol_Vaidya

Untitled

Apr 4th, 2022
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. '''
  2.  
  3. Tried to create gaussian elimination as python code
  4.  
  5. Eventually foudn a script on a site that would do it out.
  6.  
  7. The website promptly stopped working around 4pm today, but it was from the umbc math department
  8.  
  9. '''
  10.  
  11. def gausselim1(themat):
  12.     for i in range(len(themat)):
  13.         for j in range(i+1, len(themat)):
  14.             m = themat[j][i]/themat[i][1]
  15.             themat[j] = [themat[j][k] - m*themat[i][k] for k in range(len(themat[0]))]
  16.     return themat
  17.  
  18. # wanted ot check it
  19. check_matrix = [
  20.     [2,3,4],
  21.     [6,7,8],
  22.     [11,6,9]]
  23. gausselim1(check_matrix)
  24.  
  25. '''
  26. I tested it against a 2 by 2 matrix - worked fine.  
  27.  
  28. check1 = [
  29.     [2,4],
  30.     [5,2]]
  31. gausselim1(check1)
  32. >>[[1.0, 2.0], [0.0, -8.0]]
  33.  
  34. Which is correct.
  35.  
  36. Then against a 3 by 3 (see the object check_matrix in the code above).  No luck.
  37.  
  38. The result was -
  39.  
  40. >>
  41. [  2.0000  3.0000  4.0000]
  42. [  2.0000  1.0000  0.0000]
  43. [ 13.6667  0.0000 -5.6667]
  44.  
  45. Yes, I'm using a formatting script to change it into something that's neater,
  46. as opposed to the list of lists that it would give.  No, it's not the formatting
  47. script that has the problem - *that* works fine.
  48.  
  49. I did the gaussian elimination by hand - here are the steps I got
  50. R1 (for Row 1), etc -
  51.  
  52. R1/2 ->
  53. [1 3/2 2]
  54. [6 7 8]
  55. [11 6 9]
  56.  
  57. R2 - 6R1 ->
  58. [1 3/2 2]
  59. [0 -2 -4]
  60. [11 6 9]
  61.  
  62. R3 - 11R1 ->
  63. [1 3/2 2]
  64. [0 -2 -4]
  65. [0 -21/2 -13]
  66.  
  67. R2/-2 ->
  68. [1 3/2 2]
  69. [0 1 2]
  70. [0 -21/2 -13]
  71.  
  72. R3 + (21/2)R2 ->
  73. [1 3/2 2]
  74. [0 1 2]
  75. [0 0 8]
  76.  
  77. R3/8 ->
  78. [1 3/2 2]
  79. [0 1 2]
  80. [0 0 1]
  81.  
  82. I need someone to -
  83.  
  84. a) check that the steps I did out by hand are correct -
  85. I've gone over them, but it's late and
  86. it's possible my eyes are playing tricks on me -
  87.  
  88. if I made an error it would be on the third step (R3-11R1).  I've checked and double checked,
  89. but there was a moment where I ran out of space on the scrap of paper I was working on,
  90. and...well, it would be there if it was anywhere.
  91.  
  92. b) check the code to tell me where it's going wrong
  93.  
  94. '''
  95.  
  96.  
Advertisement
Add Comment
Please, Sign In to add comment