0

I want to solve the below system of NL equations using NLSolve.

using NLsolve
function f!(x, fvec; α=0.21,γ=0.4,β=0.99,μ=0.20,δ=0.14,θ=0.25)
i=α-1
b=α-2
c=1/γ
fvec[1]=x[3]-x[3]*β*(1-δ+(α/μ)*x[1]^i*float(x[2])^-i)
fvec[2]=θ*x[3]*x[2]^c-(1-α)*x[1]^α*float(x[2])^-α*(1/μ)
fvec[3]=x[3]+δ*x[1]-x[1]^α*float(x[2])^-i
fvec[4]=x[2]-0.33
fvec[5]=-1.05^(1/4)+1-δ+(α/μ)*x[1]^i*float(x[2])^-i
end
function g!(x, fjac;α=0.21,γ=0.4,β=0.99,μ=0.2,δ=0.14,θ=0.25)
a=-α-1
i=α-1
b=α-2
fjac[1,1]=-x[3]β*(α/μ)*(α-1)*(x[1]^b)*float(x[2])^-i
fjac[1,2]=-x[3]β*(α/μ)*(1-α)*(x[1]^i)*float(x[2])^-α
fjac[1,3]=1-β*(1-δ+(α/μ)*x[1]^i*float(x[2])^-i)
fjac[2,1]=-(α/μ)*(1-α)*x[1]^i*float(x[2])^-α
d=(1/γ)-1
fjac[2,2]=(θ/γ)*x[3]*x[2]^d+(1-α)*(α/μ)*x[2]^a*x[1]^α
fjac[2,3]=θ*x[2]^(1/γ)
fjac[3,1]=δ-α*x[1]^i*float(x[2])^-i
fjac[3,2]=-(1-α)*x[1]^α*float(x[2])^-α
fjac[3,3]=1
fjac[4,1]=0
fjac[4,2]=1
fjac[4,3]=0
fjac[5,1]=(α/μ)*i*x[1]^b*float(x[2])^-i
fjac[5,2]=(α/μ)*(1-α)*x[1]^i*float(x[2])^-α
fjac[5,3]=0
end

nlsolve(f!,g!,[0.1,0.33,1.2])

When implementing the above code, the error below is returned:

BoundsError: attempt to access 3-element Array{Float64,1} at index [4].

Please how can I fix it?

Itchydon
  • 2,572
  • 6
  • 19
  • 33
ludo
  • 35
  • 9
  • I tried unsuccessfully to fix it using the @inbounds macro. – ludo Aug 16 '17 at 15:14
  • 5
    `fvec[4]=x[2]-0.33` but `fvec` is `[0.1,0.33,1.2]`? So you can't set the fourth element of a length three vector? – daycaster Aug 16 '17 at 15:18
  • @daycaster fvec[4] comes as I want to constraint x[4] =0.33 and fvec[5] is also due tot the fact I want to constraint 1-δ+(α/μ)*x[1]^i*float(x[2])^-i=-1.05^(1/4).(-part of fvec[1]). That's why I think I can not set the fourth element. More info: It is the steady state conditions for a neoclassical growth model – ludo Aug 16 '17 at 15:28
  • You're looking for constrained optimization, not nonlinear solving. – Chris Rackauckas Aug 16 '17 at 18:04
  • I have unsuccessfully tried with JuMP, using NLoptSolver(algorithm=:LD_SLSQP): Message [ArgumentError: invalid NLopt arguments] and when using IpoptSolver: message [Not solved to optimality, Not enough degree of freedom]. Any solver to suggest? – ludo Aug 17 '17 at 12:58

0 Answers0