【发布时间】:2019-09-08 13:34:34
【问题描述】:
我正在尝试在 Julia 中实现多元牛顿法,但遇到了“无方法匹配”错误。下面是我的实现和我用来调用它的代码。
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
调用尝试 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)
运行上述代码时,会抛出以下错误:
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)
调用尝试 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
在尝试调用方法 2 时,我没有遇到任何错误,但进程永远不会终止。作为参考,我在 MATLB 中编写的多元牛顿法的相应实现在大约 10 秒内解决了示例中的方程组。
如何在 Julia 中正确实现和调用多元牛顿法?
【问题讨论】:
标签: julia newtons-method