# Error while using Optimization.jl

**URL:** https://discourse.julialang.org/t/error-while-using-optimization-jl/112002
**Category:** General Usage
**Tags:** question, package, optimization
**Created:** [March 23, 2024, 3:46am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002 "2024-03-23T03:46:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![npsasikumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/npsasikumar/32/43609_2.png) [@npsasikumar](https://discourse.julialang.org/u/npsasikumar)
#### Post date: [March 23, 2024, 3:46am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002/1 "2024-03-23T03:46:48Z")

</div>

**function errorfunction(data, p1)**  
\*\* ddprob, ddprob\_response = data\*\*  
\*\* error = 0.0\*\*  
\*\* for i in 1:length(ddprob)\*\*  
\*\* error += sum(abs2, ddprob\_response[i][:] - ddprob[i][:])\*\*  
\*\* end\*\*  
\*\* return error\*\*  
**end**

Above function is the error function from the systems in order to acheive synchronization. While using prob = OptimizationProblem(errorfunction, z0, p1) the output is “prob = OptimizationProblem. In-place: true  
u0: 2-element Vector{Float64}:  
0.0  
0.0”

When I wanted to use **solve(prob, OptimizationOptimJL.NelderMead())**, I am gettin message as: MethodError: no method matching getindex(::Float64, ::Colon)

Closest candidates are:

getindex(::Number, !Matched::Integer)

@ Base number.jl:96

getindex(::Number)

@ Base number.jl:95

getindex(::Number, !Matched::Integer…)

@ Base number.jl:101

and in stack trace

1. errorfunction(data::Vector{Float64}, p1::Vector{Float64}) @ [Other cell: line 6]

```julia
[

```

```
error = 0.0

```

```julia

```

```
for i in 1:length(ddprob)

```

```julia

```

```
    error += sum(abs2, ddprob_response[i][:] - ddprob[i][:])

```

```julia

```

```
end

```

```julia

```

```
return error

```

```julia
]

```

_Few suggestions/ideas would be helpful to me. Thank you in advance_

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [March 23, 2024, 5:39am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002/2 "2024-03-23T05:39:17Z")

</div>

It’s hard to figure things out at the moment, can you post a complete example and try to format your code a little better?

Off the top of my head I would bet that `ddprob` is a matrix, and that you should be indexing it as `ddprob[i, :]` instead of `ddprob[i][:]` (which would work in Python). In Julia, whatever the shape of the array `a`, `a[i]` for integer `i` is the `i`-th element in the flattened array, hence a scalar. That’s why you cannot apply `getindex(..., :)` (“colon”) on it.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![npsasikumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/npsasikumar/32/43609_2.png) [@npsasikumar](https://discourse.julialang.org/u/npsasikumar)
#### Post date: [March 23, 2024, 8:44am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002/3 "2024-03-23T08:44:35Z")

</div>

Here, _ddprob_ is a matrix and **size** (_ddprob_) = (3, 100001). In the program, I wanted to find the error between _ddprob\_response_ and _ddprob_, where both matrices are the same **size**. So here **length** would be 100001.

So the function I have defined is:  
**function** errorfunction(_ddprob_, _ddprob\_response_, p1)  
error = 0.0  
for i in 1:length(_ddprob_)  
error += **sum** (abs2, _ddprob\_response_[i][:] - _ddprob_[i][:])  
end  
return error  
end

To the above function, I wanted to optimize, so the code used is:

**prob = OptimizationProblem** (_errorfunction_, _z0,_ _p1_)

here _z0_ is the initial guess and _p1_ is the parameters. And the output to the above code is:  
**true  
u0: 2-element Vector{Float64}:  
0.0  
0.0**

After this I use to evaluate the code, which is:  
**solve(prob, OptimizationOptimJL.NelderMead())**

but this time, error message is shown as follow:  
**MethodError: no method matching getindex(::Float64, ::Colon)**

I hope, I explained it correctly, if not please suggest me. Thank you so much.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [March 23, 2024, 8:52am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002/4 "2024-03-23T08:52:35Z")

</div>

It is as I assumed: you are indeed trying to manipulate a matrix by first selecting the row and then performing operations. But the syntax to do that in Julia might be counter-intuitive if you come from Python.  
Does the following example help you understand what is going on?

```julia
julia> M = rand(2, 3)
2×3 Matrix{Float64}:
 0.954341 0.924978 0.168994
 0.22158 0.888139 0.999073

julia> size(M)
(2, 3)

julia> length(M)
6

julia> M[1, 1] == M[1]
true

julia> M[2, 1] == M[2]
true

julia> M[1, 2] == M[3]
true

julia> M[1, :]
3-element Vector{Float64}:
 0.9543413417562148
 0.9249777531861214
 0.16899390662598546

julia> M[1]
0.9543413417562148

julia> M[6]
0.9990731572586894

julia> M[1][:]
ERROR: MethodError: no method matching getindex(::Float64, ::Colon)

Closest candidates are:
  getindex(::Number, ::Integer)
   @ Base number.jl:96
  getindex(::Number)
   @ Base number.jl:95
  getindex(::Number, ::Integer...)
   @ Base number.jl:101
  ...

```

Side note: you can format your code with triple backticks to make it easier to read for others, as mentioned in the post I linked.

---

<div class="post-metadata">

### Author: ![npsasikumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/npsasikumar/32/43609_2.png) [@npsasikumar](https://discourse.julialang.org/u/npsasikumar)
#### Post date: [March 23, 2024, 9:10am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002/5 "2024-03-23T09:10:12Z")

</div>

The output of the code

> ddprob = DataDrivenProblem(sol, use\_interpolation = true)

is

`Continuous DataDrivenProblem{Float64} ##DDProblem#1130 in 3 dimensions and 1000001 samples`

similarly for _ddprob\_response_

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [March 23, 2024, 9:12am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002/6 "2024-03-23T09:12:47Z")

</div>

Okay, what is your question here?

---

<div class="post-metadata">

### Author: ![npsasikumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/npsasikumar/32/43609_2.png) [@npsasikumar](https://discourse.julialang.org/u/npsasikumar)
#### Post date: [March 23, 2024, 9:19am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002/7 "2024-03-23T09:19:54Z")

</div>

> solve(prob, OptimizationOptimJL.NelderMead())

When I am trying to run the above code,

```julia
MethodError: no method matching errorfunction(::Vector{Float64})

Closest candidates are:

errorfunction(::Any, !Matched::Any)

```

In the **Stack trace**

 ![error](https://global.discourse-cdn.com/julialang/original/3X/c/0/c0daa8f55088385f99d63e830826b0ea5d247fb6.png)

I am getting the above message.

> prob = OptimizationProblem(errorfunction, z0, p1, lb = [-1.0, -1.0], ub = [1.0, 1.0])

output:

```julia
prob
OptimizationProblem. In-place: true
u0: 2-element Vector{Float64}:
 0.0
 0.0

```

Thank you.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [March 23, 2024, 9:29am UTC](https://discourse.julialang.org/t/error-while-using-optimization-jl/112002/8 "2024-03-23T09:29:45Z")

</div>

Can you please post one complete code file, surrounded by backticks, with all the necessary imports and definitions, so that we can reproduce the bug? And it seems you still haven’t corrected your array indexing problem
