# Solving system equations using Knitro

**URL:** https://discourse.julialang.org/t/solving-system-equations-using-knitro/10490
**Category:** Optimization (Mathematical)
**Created:** [April 23, 2018, 2:42pm UTC](https://discourse.julialang.org/t/solving-system-equations-using-knitro/10490 "2018-04-23T14:42:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![LinOslo](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@LinOslo](https://discourse.julialang.org/u/LinOslo)
#### Post date: [April 23, 2018, 2:42pm UTC](https://discourse.julialang.org/t/solving-system-equations-using-knitro/10490/1 "2018-04-23T14:42:07Z")

</div>

I’m solving a system equation using KNITRO. For example the system takes the form:  
x+3_yˆ3-7+18=0  
sin(y_exp(x)-1)=0

I want to use self-defined functions (instead of using ‘in-line’ functions as in JuMP). Therefore in Julia I run:

```julia
using KNITRO
using Base.Test

function eval_f(x::Vector{Float64})
  return 0
end

function eval_grad_f(x::Vector{Float64},grad::Vector{Float64})
  return [0 0]
end

function eval_g(x::Vector{Float64}, cons::Vector{Float64})
    cons[1] = (x[1]+3)*(x[2]^3-7)+18
    cons[2] = sin(x[2]*exp(x[1])-1)
end

function eval_jac_g(x::Vector{Float64}, jac::Vector{Float64})
    jac[1] = x[2]^3-7
    jac[2] = 3*x[2]^2*(x[1]+3)
    u = exp(x[1])*cos(x[2]*exp(x[1])-1)
    jac[3] = x[2]*u
    jac[4] = u
end

```

Then I start to setup the problem:

```julia
objGoal = KTR_OBJGOAL_MINIMIZE
objType = KTR_OBJTYPE_QUADRATIC

n = 2
x_L = -[KTR_INFBOUND,KTR_INFBOUND]
x_U = [KTR_INFBOUND,KTR_INFBOUND]

m = 2
c_Type = [KTR_CONTYPE_GENERAL,KTR_CONTYPE_GENERAL]
c_L = [0.0,0.0]
c_U = [0.0,0.0]

jac_con = Int32[0,0]
jac_var = Int32[0,1]

x = [0.5,0.5]
lambda = zeros(n+m)
obj = [0.0]

kp = createProblem()

loadOptionsFile(kp, "knitro.opt")
setOption(kp, "hessopt", 6)
initializeProblem(kp, objGoal, objType, x_L, x_U, c_Type, c_L, c_U, jac_var, jac_con)

setFuncCallback(kp, eval_f, eval_g)
setGradCallback(kp, eval_grad_f, eval_jac_g)
solveProblem(kp)

```

However, I always encounter the problem in the eval\_jac\_g function saying that:

```julia
BoundsError: attempt to access 2-element Array{Float64,1} at index [3]

```

What’s the problem here? And what is the code `jac_con = Int32[0,0]`  
and `jac_var = Int32[0,1]` doing?

I’m really new to the Knitro and probably ask a super silly question. I truly hope someone could help me out. Appreciate so much for taking time!

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [April 23, 2018, 7:04pm UTC](https://discourse.julialang.org/t/solving-system-equations-using-knitro/10490/2 "2018-04-23T19:04:06Z")

</div>

I haven’t been through the Knitro interface (though really want to!) but since you have two equations and two unknowns, shouldn’t the jacobian be 2x2 rather than 4x1? If so, then `jac[3]` woudl be the culprit? Instead, are you supposed to go: `jac[1,1]`, … `jac[2,2]`?

Also, in case you made a mistake in the signature of the function, it may make sense to drop the type annotations (i.e. just use `x` instead of `x::Vector{Float64}`), at least until you get it working … and maybe even after.

---

<div class="post-metadata">

### Author: ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)
#### Post date: [April 23, 2018, 8:50pm UTC](https://discourse.julialang.org/t/solving-system-equations-using-knitro/10490/3 "2018-04-23T20:50:47Z")

</div>

Is this a problem you actually want to solve, or some random example you cooked up? It literally has hundreds of solutions on [-6,6] x [-6,6] according to IntervalRootFinding.jl.

---

<div class="post-metadata">

### Author: ![LinOslo](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@LinOslo](https://discourse.julialang.org/u/LinOslo)
#### Post date: [April 23, 2018, 8:51pm UTC](https://discourse.julialang.org/t/solving-system-equations-using-knitro/10490/4 "2018-04-23T20:51:01Z")

</div>

Thanks for the reply, jlperla!  
Unfortunately dropping the type does not solve the problem. And I was following an example of knitro.jl to define the Jacobian function. It seems the Jacobian has to be reshaped into a vector if I understand correctly… But thanks anyway! 🙂
