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