# Implementation of Jacobi method not yielding acurate results

Hello,

I am fairly new to the language and i have tried to implement the Jacobi method. However, this has not given me the expected results. The solution should be (1,2,-1,1) and the returned values at convergence are (0.95,1.78,-0.96,1.26).

``````A = [10 -1 2 0
-1 11 -1 3
2 -1 10 -1
0 3 -1 8]
B = [6 25 -11 15]

initial = Float64[0, 0, 0, 0]
maxIterations = 1000
tolerance = 1*10^-3

dimension = length(initial)

coefficentMatrix = zeros(Float64,length(initial),length(initial) + 1)
for index in CartesianIndices(coefficentMatrix)
if index[1] == index[2]
coefficentMatrix[index[1],length(initial)+1] = B[index[2]] / A[index[2],index[2]]
elseif index[2] != length(initial) + 1
coefficentMatrix[index[1],index[2]] = -A[index[1],index[2]] / A[index[2],index[2]]
end
end
iteration = 1
while iteration < maxIterations
aproximation = Float64[]
for row in eachrow(coefficentMatrix)
push!(aproximation,sum(row[1:dimension] .* initial[:,iteration]) + row[dimension+1])
end
global iteration += 1
global initial = hcat(initial,aproximation)
end
``````

Any help is appreciated.

Thanks,

Fish

Julia is column-major while your implementation seems to be for a row-major case. The code works as expected if we swap the indexing of the matrices:

Code revised
``````A = [10. -1 2 0
-1 11 -1 3
2 -1 10 -1
0 3 -1 8]
B = [6., 25, -11, 15]

initial = [0., 0, 0, 0]
maxIterations = 1000
tolerance = 1*10^-6

dimension = length(initial)

coefficentMatrix = zeros(Float64,length(initial),length(initial) + 1)
for index in CartesianIndices(coefficentMatrix)
if index[1] == index[2]
coefficentMatrix[index[2],length(initial)+1] = B[index[2]] / A[index[2],index[2]]
elseif index[2] != (length(initial) + 1)
coefficentMatrix[index[2],index[1]] = -A[index[2],index[1]] / A[index[2],index[2]]
end
end
iteration = 1
while iteration < maxIterations
aproximation = Float64[]
for row in eachrow(coefficentMatrix)
push!(aproximation,sum(row[1:dimension] .* initial[:,iteration]) + row[dimension+1])
end
iteration += 1
initial = hcat(initial,aproximation)
end
initial
``````

Thanks that did the trick. i just canâ€™t get to the column major system. I came over from python and I am having a rough time.

Thanks,

Fish