繁体   English   中英

Julia 中的牛顿方法 function 出错(没有方法匹配 ^(::Vector{Float64}, ::Int64)

[英]Error in Newton's Method function in Julia (no method matching ^(::Vector{Float64}, ::Int64)

我正在尝试在 Julia 中编写一个 function 来解决非线性方程 f(x)=0 使用牛顿迭代。 我是 Julia 的初学者,所以请耐心等待。 在作业中,我的老师提供了第一行代码:

newton(f, x_0; maxiterations=10, tolerance=1e-14, epsilon=1e-7)

他还提供了以下声明:“可选的 epsilon 参数提供了用于有限差分逼近 f'(x) ≈ (f(x + ε) - f(x)) / ε 的 ε 值。” 过去,我在 MATLAB 中为牛顿法创建了 function,但我假设 f(x) 的一阶导数必须是函数的输入之一。 在这个作业中,他似乎希望我使用这个近似公式。 无论如何,这是我到目前为止的代码。

function newton(f,x_0; maxiterations=10, tolerance=1e-14, epsilon=1e-7)
    
    x = [x_0] # assign x_0 to x and make x a vector 
    fd = (f(x.+epsilon) - f(x))./epsilon # fd is the first derivative of f(x), calculated from
                                       # the finite-difference approximation
    # create for loop to begin iteration
    for n = 0:maxiterations
        if abs(x[n+1]-x[n]) < tolerance # if the absolute value of the difference of x[n+1] and x[n] 
                                        # is less than the tolerance, then the value of x is returned
            return x                   
        end
        
        if abs(f(x[n])) < tolerance # if the absolute value of f(x[n]) is less than the tolerance,
                                    # then the value of x is returned
            return x
        end
        
        push!(x, x[n] - (f(x[n]))/fd) # push each calculated value to the end of vector x, 
                                      # and continue iterating
    end
    return x # after iteration is complete, return the vector x
end

在执行了这个 function 之后,我定义了应该用来确定 sqrt(13) 的方程,并调用了牛顿 function,初始猜测为 x_0=3。

f(x) = x^2 - 13
newton(f,3)

这是我在调用 newton function 后遇到的错误消息:

MethodError: no method matching ^(::Vector{Float64}, ::Int64)
Closest candidates are:
  ^(::Union{AbstractChar, AbstractString}, ::Integer) at strings/basic.jl:730
  ^(::LinearAlgebra.Hermitian, ::Integer) at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/symmetric.jl:696
  ^(::LinearAlgebra.Hermitian{T, S} where S<:(AbstractMatrix{<:T}), ::Real) where T at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/symmetric.jl:707
  ...

Stacktrace:
 [1] literal_pow
   @ ./intfuncs.jl:340 [inlined]
 [2] f(x::Vector{Float64})
   @ Main ./In[35]:3
 [3] newton(f::typeof(f), x_0::Int64; maxiterations::Int64, tolerance::Float64, epsilon::Float64)
   @ Main ./In[34]:5
 [4] newton(f::Function, x_0::Int64)
   @ Main ./In[34]:2
 [5] top-level scope
   @ In[35]:4
 [6] eval
   @ ./boot.jl:368 [inlined]
 [7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1428

在这个问题中,我应该让我的 function 返回一个逐次逼近的向量并产生一个对数误差 plot ,其中包括一条牛顿迭代的理论误差估计曲线。 我将不胜感激纠正我的代码中的问题的任何指导,以便我可以继续创建错误 plot。

谢谢你。

您可以使用

newton(x->x.^2 .- 13, 3)

操纵向量而不是标量。 但是你的牛顿代码仍然有问题!

您从零索引开始(不好),并且在分配之前使用 x[n+1] 并且它缺少一些数学步骤。 我可以提议你

function newton(f,x_0; maxiterations=10, tolerance=1e-14, epsilon=1e-7)
    x = x_0 
    for n =1:maxiterations
        fd = (f(x.+epsilon) - f(x))./epsilon 
        tmp=x - f(x) / fd
        if (abs(tmp-x) < tolerance || abs(f(x)) < tolerance )
           break
        end
        x = tmp 
    end
    return x 
end

现在结果似乎很好

julia> newton(x->x.^2 .- 13, 3)
3.60555127546399

julia> sqrt(13)
3.605551275463989

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM