Error when defining an array as a function argument

question
#1

I wrote the following code and an error occurred in the function part.
I think that the number of elements of the array is correct, but I do not know why the error occurs.

function jacobi(x::Matrix{Float64},A::Matrix{Float64},b::Matrix{Float64})
    n=size(x,1)
    x_new=copy(x)  
    sum=zeros(n)
    b_sum=0
    for i in 1:n
        b_sum+=(abs(b[i]^2))^0.5
    end
    count=0
    if count==0
        for i in 1:n
            for j in 1:n
                if i!=j
                    sum[i]+=A[i,j]*x[j]
                end
            end
            x_new[i]=(b[i]-sum[i])/A[i,i]
        end
        
        nrm=zeros(n)
        nrm=x_new-x
        nrm_sum=0
        for i in 1:n
            nrm_sum+=(abs(nrm[i])^2)^0.5
        end
        if nrm_sum<5e-1
            count=1
        end
    end
end

A=[3 1 1; 1 3 1; 1 1 3]
b=[0,4,6]
x=zeros(3)

jacobi(x,A,b)

So, please tell me what is wrong.

ERROR: LoadError: MethodError: no method matching jacobi(::Array{Float64,1}, ::Array{Int64,2}, ::Array{Int64,1})Stacktrace:
 [1] top-level scope at none:0
0 Likes

#2

your matrix is a matrix of integers while your function accepts floating point numbers only. this code is a little bit more generic:

function jacobi(x::Array{<:Real},A::Array{<:Real},b::Array{<:Real})
...

besides, this works fine too:

function jacobi(x,A,b)
...

julia will recognize the types at the moment you feed some numbers to that function

if you only want floating numbers, you can:
a) convert the numbers to Float64 before putting those inside the function:

convert.(Float64,[2,3]) # the point in this case is to broadcast 
#the operation to the entire array

b) write those numbers as floats:

#notice the points
A=[3. 1. 1.; 1. 3. 1.; 1. 1. 3.]
b=[0.,4.,6.] 
x=zeros(3) #this already produces an array of float64
0 Likes