blackie9900
New Member
Hello,
below is my excel macro that reads the matrix data, reads an initial guess for the x values, and reads the maximum number of iterations from a cell and then calculates the Jacobi method to solve for each of the x values for the systems of equations.
I got it to successfully calculate the Jacobi method using a 6x6 matrix [A], and read my initial x value guesses from the 1x6 matrix [x0] and iterate a set number of times defined by the value in a cell.
however I'm unsure how to code the next part.
im looking to stop the solution early if x is converged based on a set criteria. to accomplish this I was going to put in a tolerance value into a cell (say 0.001) and then upon iterating I was going to subtract the solved x values. the i+1 iteration from the i (all x values in the 1x6 matrix) and if that difference is less than or equal my tolerance value the program would stop iterating and would say which iteration number the solution converged on.
Attached is my code that solves the matrix. any help is greatly appreciated.
thank you
below is my excel macro that reads the matrix data, reads an initial guess for the x values, and reads the maximum number of iterations from a cell and then calculates the Jacobi method to solve for each of the x values for the systems of equations.
I got it to successfully calculate the Jacobi method using a 6x6 matrix [A], and read my initial x value guesses from the 1x6 matrix [x0] and iterate a set number of times defined by the value in a cell.
however I'm unsure how to code the next part.
im looking to stop the solution early if x is converged based on a set criteria. to accomplish this I was going to put in a tolerance value into a cell (say 0.001) and then upon iterating I was going to subtract the solved x values. the i+1 iteration from the i (all x values in the 1x6 matrix) and if that difference is less than or equal my tolerance value the program would stop iterating and would say which iteration number the solution converged on.
Attached is my code that solves the matrix. any help is greatly appreciated.
thank you
Code:
Sub Jacobi()
Dim A(1 To 36, 1 To 6) 'my [A] matrix
Dim b(1 To 6)
Dim x(1 To 6)
Dim u(1 To 6)
'read in my [A] matrix
For RC = 1 To 6
For CC = 1 To 6
A(RC, CC) = Worksheets("MatrixProblem").Cells(11 + RC, 1 + CC).Value
Next CC
b(RC) = Worksheets("MatrixProblem").Cells(11 + RC, 13).Value
x(RC) = Worksheets("MatrixProblem").Cells(11 + RC, 11).Value
Next RC
Maxiter = Worksheets("MatrixProblem").Cells(6, 17).Value
'calculate jacobi algorithm
For iter = 1 To Maxiter
For i = 1 To 6
rowsum = 0
For j = 1 To 6
If j <> i Then
rowsum = rowsum + A(i, j) * x(j)
End If
Next j
u(i) = b(i) - rowsum
Next i
'reassign u vector to x
For m = 1 To 6
x(m) = u(m)
Next m
Next iter
'write out data
For RC = 1 To 6
For CC = 1 To 6
Worksheets("MatrixProblem").Cells(27 + RC, 1 + CC).Value = A(RC, CC)
Worksheets("MatrixProblem").Cells(27 + RC, 13).Value = b(RC)
Worksheets("MatrixProblem").Cells(27 + RC, 9).Value = x(RC)
Next CC
Next RC
End Sub