# Problem using ModellingToolkit and Galacticoptim to solve a vector/matrix optimization problem

**URL:** <https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470>\
**Category:** General Usage\
**Created:** [June 6, 2021, 5:34pm UTC](https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470 "2021-06-06T17:34:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [June 6, 2021, 5:34pm UTC](https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470/1 "2021-06-06T17:34:57Z")

</div>

Hey folks, I was trying to resolve a problem using MTK and GalacticOptim to solve a simple optimization problem. The issue is that I am no really getting an error message that tells me where the problem is.

Here is the code:

```julia
using ModelingToolkit, GalacticOptim
using LinearAlgebra
@variables x[1:4]
@parameters Q, R
A = [1 1 1 1; 1 2 3 4; 1 3 6 10; 1 4 10 20]
B = [1 1 1 1; 1 2 3 4; 1 3 6 10; 1 4 10 20]
loss_quad = x'*Q*x + x'*R*x 
u1 = [1.,1.,1.,1.]
p = [
    Q => A,
    R => B
]
sys2 = OptimizationSystem(loss_quad,[x],[Q, R])
prob2 = OptimizationProblem(sys2, u1, p ,grad=true,hess=true)
solve(prob2,Newton())

```

But the error message I get is just.:

```julia
ERROR: syntax: invalid let syntax around /xxx/.julia/packages/SymbolicUtils/JmtMa/src/code.jl:283
Stacktrace:
 [1] top-level scope
   @ REPL[52]:100: 

```

I tried looking around for other examples. My guess is that it has to do with how `u1` or `p` are specified, but not sure. If anyone has a suggestion it would be appreciated.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 6, 2021, 5:43pm UTC](https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470/2 "2021-06-06T17:43:54Z")

</div>

Array variables aren’t supported yet (though @shashi is very close to done!). So the error is something about the fact that `Q => A` where `A` is an array. If you hardcode the parameters it would work though.

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [June 6, 2021, 5:53pm UTC](https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470/3 "2021-06-06T17:53:23Z")

</div>

Oh thanks @ChrisRackauckas .

I tried to hardcode it like this to get around the array.

```julia
p = [
    Q => [1. 1. 1. 1.; 1. 2. 3. 4.; 1. 3. 6. 10.; 1. 4. 10. 20.],
    R => [1. 1. 1. 1.; 1. 2. 3. 4.; 1. 3. 6. 10.; 1. 4. 10. 20.]
]

```

Still hitting the same error. I will keep playing with it though. I was working through some other MTK tutorials to just get a better feel for the syntax. I think it is probably just some small dumb thing. Thanks again.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 6, 2021, 5:59pm UTC](https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470/4 "2021-06-06T17:59:55Z")

</div>

> [@00krishna](#):
>
> `Q => [1. 1. 1. 1.; 1. 2. 3. 4.; 1. 3. 6. 10.; 1. 4. 10. 20.]`

That’s still an array, not like `Q => 1.0`. You could do `@parameters Q[1:4,1:4]` and then do `Q[1] => ...`, etc. right now. That’s the current limitation I mentioned.

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [June 6, 2021, 6:05pm UTC](https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470/5 "2021-06-06T18:05:16Z")

</div>

Oh I got you. Yeah that makes sense. Sorry I did not understand the first time.

---

<div class="post-metadata">

**Author:** ![jerry\_ji](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@jerry\_ji](https://discourse.julialang.org/u/jerry_ji)\
**Post date:** [June 3, 2022, 12:56pm UTC](https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470/6 "2022-06-03T12:56:32Z")

</div>

Is there any update on this questions? I am unable to get it to work with vector of variables/parameters.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 3, 2022, 1:41pm UTC](https://discourse.julialang.org/t/problem-using-modellingtoolkit-and-galacticoptim-to-solve-a-vector-matrix-optimization-problem/62470/7 "2022-06-03T13:41:42Z")

</div>

Start a new thread with a concrete MWE to work from. Things should work.
