gpt4 book ai didi

julia - 非线性求解器始终产生零残差

转载 作者:行者123 更新时间:2023-12-04 23:38:53 25 4
gpt4 key购买 nike

我正在学习如何求解非线性方程和一般的 Julia (1.3.1),想问一下我们应该如何使用 NLsolve。

作为第一步,我尝试了以下操作:

using NLsolve
uni = 1
z = 3
x = [3,2,4,5,6]
y = z .+ x
print(y)
function g!(F, x)
F[1:uni+4] = y .- z - x
end
nlsolve(g!, [0.5,0.9,1,2,3])

而且,我确认它的工作原理如下:

Results of Nonlinear Solver Algorithm
* Algorithm: Trust-region with dogleg and autoscaling
* Starting Point: [0.5, 0.9, 1.0, 2.0, 3.0]
* Zero: [2.999999999996542, 2.000000000003876, 4.000000000008193, 4.999999999990685, 5.999999999990221]
* Inf-norm of residuals: 0.000000
* Iterations: 2
* Convergence: true
* |x - x'| < 0.0e+00: false
* |f(x)| < 1.0e-08: true
* Function Calls (f): 3
* Jacobian Calls (df/dx): 3

然后,我尝试了一个更复杂的模型,如下所示

using SpecialFunctions, NLsolve, Random
Random.seed!(1234)
S = 2

# Setting parameters
ε = 1.3
β = 0.4
γ = gamma((ε-1)/ε)
T = rand(S)
E = rand(S)
B = rand(S)
w = rand(S)
Q = rand(S)
d = [1 2 ;2 1 ]

# Construct a model

rvector = T.*Q.^(1-β).*B.^ε
svector = E.* w.^ε


Φ_all = (sum(sum(rvector * svector' .* d )))
π = rvector * svector' .* d ./ Φ_all

# These two are outcome the model
πR = (sum(π,dims=1))'
πM = sum(π,dims=2)

# E is now set as unknown and we want to estimate it given the outcome of the model

function f!(Res, Unknown)


rvector = T.*Q.^(1-β).*B.^ε
svector = Unknown[1:S].* w.^ε


Φ_all = (sum(sum(rvector * svector' .* d )))
π = rvector * svector' .* d ./ Φ_all
Res = ones(S+S)
Res[1:S] = πR - (sum(π,dims=1))'
Res[S+1:S+S] = πM - sum(π,dims=2)

end


nlsolve(f!, [0.5,0.6])

这段代码会产生如下奇怪的结果。

Results of Nonlinear Solver Algorithm
* Algorithm: Trust-region with dogleg and autoscaling
* Starting Point: [0.5, 0.6]
* Zero: [0.5, 0.6]
* Inf-norm of residuals: 0.000000
* Iterations: 0
* Convergence: true
* |x - x'| < 0.0e+00: false
* |f(x)| < 1.0e-08: true
* Function Calls (f): 1
* Jacobian Calls (df/dx): 1

因此,从本质上讲,该函数的返回值始终为 0,因此初始输入始终成为解决方案。我不明白为什么这不起作用以及为什么它的行为与第一个示例不同。我可以提出修复建议吗?

最佳答案

您需要就地更新Res。现在你做

Res = ones(S+S)

隐藏输入 Res。直接更新Res即可。

关于julia - 非线性求解器始终产生零残差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60740171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com