# Implementation of Jacobi method not yielding acurate results

**URL:** <https://discourse.julialang.org/t/implementation-of-jacobi-method-not-yielding-acurate-results/70511>\
**Category:** Numerics\
**Tags:** linearalgebra\
**Created:** [October 27, 2021, 8:07pm UTC](https://discourse.julialang.org/t/implementation-of-jacobi-method-not-yielding-acurate-results/70511 "2021-10-27T20:07:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scaredFish](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@scaredFish](https://discourse.julialang.org/u/scaredFish)\
**Post date:** [October 27, 2021, 8:07pm UTC](https://discourse.julialang.org/t/implementation-of-jacobi-method-not-yielding-acurate-results/70511/1 "2021-10-27T20:07:54Z")

</div>

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).

```julia
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

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 28, 2021, 8:53am UTC](https://discourse.julialang.org/t/implementation-of-jacobi-method-not-yielding-acurate-results/70511/2 "2021-10-28T08:53:56Z")

</div>

[Julia is column-major](https://en.wikipedia.org/wiki/Row-_and_column-major_order) 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**
>
> ```julia
> 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
> 
> ```

---

<div class="post-metadata">

**Author:** ![scaredFish](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@scaredFish](https://discourse.julialang.org/u/scaredFish)\
**Post date:** [October 28, 2021, 9:17am UTC](https://discourse.julialang.org/t/implementation-of-jacobi-method-not-yielding-acurate-results/70511/3 "2021-10-28T09:17:37Z")

</div>

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
