2

I am attempting to implement the multivariate Newton's method in Julia, but have run into a "no metehod matching" error. Below is my implementation and the code I use to call it.

function newton(f::Vector, J::Matrix, x::Vector)
   h = Inf64
   tolerance = 10^(-10)
   while (norm(h) > tolerance)
      h = J(x)\f(x)
      x = x - h
   end
   return x
end

Invokation Attempt 1

f(x::Vector) = [(93-x[1])^2 + (63-x[2])^2 - 55.1^2, 
         (6-x[1])^2 + (16-x[2])^2 - 46.2^2]
J(x::Vector) = [-2*(93-x[1]) -2*(63-x[2]); -2*(6-x[1]) -2*(16-x[2])]
x = [35, 50]
newton(f, J, x)

When running the above code the following error is thrown:

ERROR: LoadError: MethodError: no method matching newton(::typeof(f), ::typeof(J), ::Array{Int64,1})
Closest candidates are:
  newton(::Array{T,1} where T, ::Array{T,2} where T, ::Array{Int64,1})
  newton(::Array{T,1} where T, ::Array{T,2} where T, ::Array{T,1} where T)
  newton(::Array{T,1} where T, ::Array{T,2} where T, ::Array)

Invokation Attempt 2

f(x::Vector) = [(93-x[1])^2 + (63-x[2])^2 - 55.1^2, 
         (6-x[1])^2 + (16-x[2])^2 - 46.2^2]
J(x::Vector) = [-2*(93-x[1]) -2*(63-x[2]); -2*(6-x[1]) -2*(16-x[2])]
x = [35, 50]
newton(f(x), J(x), x) # passing x into f and J

When trying to invoke the method as in attempt 2 I encounter no error, but the process never terminates. For reference, the corresponding implementation of multivariate Newton's method that I have written in MATLB solves the system of equations from the examples in about 10 seconds.

How can I properly implement and invoke multivariate Newton's method in Julia?

K. Claesson
  • 447
  • 4
  • 11

1 Answers1

5

Although they may return a Vector or a Matrix, both f and J are functions. Changing newtons signature to

function newton(f::Function, J::Function, x)

will make your implementation work.

As a side note, you might want to avoid specifying types unless it is necessary, and use the power of dynamic typing and Julia's type system. The code should be as generic as possible. For example, your newton function will not work with x as an SArray from StaticArrays or other array types from other packages since their types will not be <: Vector. But if you omit the type, your function will work with other types. Note that you will not lose anything in performance once the function is compiled.

See the relevant discussion in Julia documentation Style Guide.

hckr
  • 4,853
  • 1
  • 16
  • 27