julia - 有什么方法可以限制 NLsolve 在 Julia 中搜索的区域?

标签 julia nonlinear-optimization equation-solving

我试图找到非线性(大致四次)方程的根之一。
该方程总是有四个根,其中一对接近于零,一个大的正根和一个大的负根。我想确定任何一个接近零的根,但是 nlsolve ,即使初始猜测非常接近这些根,似乎总是收敛于大的正根或负根。
函数图基本上看起来像一个恒定的负值,在零附近有一个(非常窄的)偶数阶极点,并逐渐上升到在大的正根和负根处越过零。
有什么办法可以限制nlsolve 搜索的区域吗? ,或者做点什么让它对我的函数中这个极点的存在更加敏感?
编辑:
这是一些重现问题的示例代码:

using NLsolve

function f!(F,x)
    x = x[1]
    F[1] = -15000 + x^4 / (x+1e-5)^2
end
# nlsolve will find the root at -122
nlsolve(f!,[0.0])
作为输出,我得到:
Results of Nonlinear Solver Algorithm
 * Algorithm: Trust-region with dogleg and autoscaling
 * Starting Point: [0.0]
 * Zero: [-122.47447713915808]
 * Inf-norm of residuals: 0.000000
 * Iterations: 15
 * Convergence: true
   * |x - x'| < 0.0e+00: false
   * |f(x)| < 1.0e-08: true
 * Function Calls (f): 16
 * Jacobian Calls (df/dx): 6
在这种情况下,我们可以通过将目标函数转换为多项式来找到确切的根:
using PolynomialRoots
roots([-1.5e-6,-0.3,-15000,0,1])
生产
4-element Array{Complex{Float64},1}:
     122.47449713915809 - 0.0im
    -122.47447713915808 + 0.0im
 -1.0000000813048448e-5 + 0.0im
  -9.999999186951818e-6 + 0.0im
我希望有一种方法可以在不知道目标函数的确切形式的情况下识别 x = -1e-5 处极点周围的一对根。
编辑2:
试用 Roots.jl :
using Roots
f(x) = -15000 + x^4 / (x+1e-5)^2

find_zero(f,0.0) # finds +122... root
find_zero(f,(-1e-4,0.0)) # error, not a bracketing interval
find_zeros(f,-1e-4,0.0) # finds 0-element Array{Float64,1}
find_zeros(f,-1e-4,0.0,no_pts=6) # finds root slightly less than -1e-5
find_zeros(f,-1e-4,0.0,no_pts=10) # finds 0-element Array{Float64,1}, sensitive to value of no_pts
我可以得到find_zeros可以工作,但是对no_pts非常敏感参数和我选择的端点的确切值。循环 no_pts并采取第一个非空结果可能会起作用,但更确定性的收敛会更好。
编辑3:
这是应用 Bogumił 建议的 tanh 变换
using NLsolve
function f_tanh!(F,x)
    x = x[1]
    x = -1e-4 * (tanh(x)+1) / 2
    F[1] = -15000 + x^4 / (x+1e-5)^2
end

nlsolve(f_tanh!,[100.0]) # doesn't converge
nlsolve(f_tanh!,[1e5]) # doesn't converge

using Roots
function f_tanh(x)
    x = -1e-4 * (tanh(x)+1) / 2
    return -15000 + x^4 / (x+1e-5)^2
end

find_zeros(f_tanh,-1e10,1e10) # 0-element Array
find_zeros(f_tanh,-1e3,1e3,no_pts=100) # 0-element Array
find_zero(f_tanh,0.0) # convergence failed
find_zero(f_tanh,0.0,max_evals=1_000_000,maxfnevals=1_000_000) # convergence failed
EDIT4:这种技术组合在大约 95% 的时间里至少可以识别一个根,这对我来说已经足够好了。
using Peaks
using Primes
using Roots

# randomize pole location
a = 1e-4*rand()
f(x) = -15000 + x^4 / (x+a)^2

# do an initial sample to find the pole location
l = 1000
minval = -1e-4
maxval = 0
m = []
sample_r = []
while l < 1e6
    sample_r = range(minval,maxval,length=l)
    rough_sample = f.(sample_r)
    m = maxima(rough_sample)
    if length(m) > 0
        break
    else
        l *= 10
    end
end
guess = sample_r[m[1]]

# functions to compress the range around the estimated pole
cube(x) = (x-guess)^3 + guess 
uncube(x) = cbrt(x-guess) + guess
f_cube(x) = f(cube(x))

shift = l ÷ 1000
low = sample_r[m[1]-shift]
high = sample_r[m[1]+shift]
# search only over prime no_pts, so no samplings divide into each other
# possibly not necessary?
for i in primes(500)
    z = find_zeros(f_cube,uncube(low),uncube(high),no_pts=i)
    if length(z)>0
        println(i)
        println(cube.(z))
        break
    end
end

最佳答案

如果您提供有关您的问题的更多信息,可以给出更多评论。
但总的来说:

  • 看来您的问题是单变量的,在这种情况下,您可以使用 Roots.jl where find_zerofind_zeros提供您要求的界面(即允许指定搜索区域)
  • 如果问题是多变量的,您可以在 nlsolve 的问题规范中选择如何处理。 (因为默认情况下不允许指定边界框 AFAICT)。最简单的是使用变量变换。例如。您可以申请 ai * tanh(xi) + bi转换选择 aibi对于每个变量,使其限制在所需的区间

  • 您在定义中遇到的第一个问题是您定义 f 的方式。它永远不会越过0在您正在寻找的两个根附近,因为 Float641e-5 时精度不够.您需要使用更高的计算精度:
    julia> using Roots
    
    julia> f(x) = -15000 + x^4 / (x+1/big(10.0^5))^2
    f (generic function with 1 method)
    
    julia> find_zeros(f,big(-2*10^-5), big(-8*10^-6), no_pts=100)
    2-element Array{BigFloat,1}:
     -1.000000081649671426108658262468117284940444265467160592853348997523986352593615e-05
     -9.999999183503552405580084054429938261707450678661727461293670518591720605751116e-06
    
    并设置no_pts足够大以找到包含根的区间。

    关于julia - 有什么方法可以限制 NLsolve 在 Julia 中搜索的区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63022701/

    相关文章:

    r - 使用 nls() 进行非线性拟合在初始参数估计时给了我奇异的梯度矩阵。为什么?

    algorithm - 有趣的字节混合方程及其逆

    function - Julia:多种类型的相同功能?

    julia - 在 Julia 中使用 Immutable 时出现错误 "extra token "School“表达式结束后”

    julia - 在 Julia : Is there a way to test for equality like R's all. equal() 中?

    julia - 为什么 Julia 对一些大数字返回非零值?

    r - 使用 R nloptr 包进行最小化 - 多重等式约束

    r - 带抽样权重的非线性回归(包调查)

    c++ - 我将 ASCII 单词转换为数字,但无法解码它们。如何转换 1=a、2=b、28=ab 等? (伪代码好吧)

    Maxima:从方程中消除变量