• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA jacobi method and convergence criteria help

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
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
 
Blackie

Firstly, welcome to the Chandoo.org forums

Can you please post a sample file including some data and expected output if possible
 
Hi ,

I think there is a slight change you need to make in your code ; the following line needs the change as shown in BOLD :

u(i) = (b(i) - rowsum) / A(i, i)

Narayan
 
what would that do?

i have attached an excel spreadsheet containing a sample file.

i have added a cell showing the error tolerance (user input for convergence criteria)
and another cell that i would want excel to fill in (amount of iterations until the convergence criteria is met)

the answer from x1----x6 is as follows

x1=2
x2=1.5
x3=-4
x4=2
x5=-3
x6=2
 

Attachments

  • excel_problem.xlsm
    23.9 KB · Views: 59
awesome!!

now i tried applying your logic to do the same thing to the Gauss sub but am getting funking answers converging... how would you apply the same thing to the gauss sub?
 
still having some trouble getting the gauss macro also working with iteration # and error tolerance. i have tried applying your logic *narayank991* but was unable to get it to work. any help is greatly appreciated
 
Back
Top