# Optimization problem that fails to converge

**URL:** https://discourse.julialang.org/t/optimization-problem-that-fails-to-converge/121666
**Category:** New to Julia
**Created:** [October 23, 2024, 7:10pm UTC](https://discourse.julialang.org/t/optimization-problem-that-fails-to-converge/121666 "2024-10-23T19:10:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Iforire](https://avatars.discourse-cdn.com/v4/letter/i/76d3ee/32.png) [@Iforire](https://discourse.julialang.org/u/Iforire)
#### Post date: [October 23, 2024, 7:10pm UTC](https://discourse.julialang.org/t/optimization-problem-that-fails-to-converge/121666/1 "2024-10-23T19:10:18Z")

</div>

Hi all,

I am very new to Julia and maths is quite old for me now, so I am struggling with my code.  
The thing is that I would like to find the best linear combination of six parameters that fits the seventh parameter.  
Let me explain:

- I have seven 1-column matrices of 300 data for each (say C1,C2…C7)
- I would like to find the best vector (a,b,c,d,e,f) (all positive) that minimizes C7 - a_C1-b_C2-c_C3-d_C4-e_C5-f_C6  
I have tried the optimization package using NLopt.LD\_LBFGS() and NLopt.GN\_DIRECT() but it failed to converge.  
Here is the code for the last one:

```julia
function optim(a,b,c,d,e,f)
	deviation = (C7 - a*C1 - b*C2 - c*C3 - d*C4 - e*C5 - f*C6)
	somme = 0
	for i in deviation
		somme = somme + i^2
	end
	return somme
end

optfun = OptimizationFunction(optim, Optimization.AutoForwardDiff())

begin
	init = zeros(6)
	prob = OptimizationProblem(optfun, init, lb = [0, 0, 0, 0, 0, 0], ub = [1.0, 1.0, 1.0, 1.0, 1.0,1.0])
end

sol = solve(prob, NLopt.GN_DIRECT(), maxtime = 10.0)

```

And it gives me “NLopt failed to converge: FORCED\_STOP”

Would you have a suggestion for me?

Thanks a lot!

Iforire

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [October 23, 2024, 7:57pm UTC](https://discourse.julialang.org/t/optimization-problem-that-fails-to-converge/121666/2 "2024-10-23T19:57:48Z")

</div>

Welcome to Julia and Julia Discourse.

Because you stated that your math is rusty, I will mention that the product of a matrix and a vector is just the linear combination of the columns of the matrix, with the vector elements as the linear coefficients. Therefore, your problem looks like a [nonnegative least squares](https://en.wikipedia.org/wiki/Non-negative_least_squares) problem. It can be solved very simply using the [NonNegLeastSquares](https://github.com/JuliaLinearAlgebra/NonNegLeastSquares.jl) package. Here is a solution using random values for the vectors c1 through c7:

```julia
using NonNegLeastSquares: nonneg_lsq
using LinearAlgebra: norm
c1_to_c6 = rand(300,6) # Contains columns c1 through c6
c7 = rand(300) # Contains column 7
coefs = nonneg_lsq(c1_to_c6, c7)
@show coefs
@show norm(c1_to_c6 * coefs - c7)

```

This produces the following output (it will be different for you due to the random entries):

```julia
coefs = [0.17908585324436196; 0.10343518649759845; 0.09408512183744712; 0.21328999063315954; 0.2224834340815174; 0.1077332059326006;;]
norm(c1_to_c6 * coefs - c7) = 5.595322154323634
5.595322154323634

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 23, 2024, 10:17pm UTC](https://discourse.julialang.org/t/optimization-problem-that-fails-to-converge/121666/3 "2024-10-23T22:17:06Z")

</div>

> [@Iforire](#):
>
> `function optim(a,b,c,d,e,f)`

Probably it is expecting a function that takes a vector of arguments. Read the documentation.

---

<div class="post-metadata">

### Author: ![Iforire](https://avatars.discourse-cdn.com/v4/letter/i/76d3ee/32.png) [@Iforire](https://discourse.julialang.org/u/Iforire)
#### Post date: [October 24, 2024, 7:55am UTC](https://discourse.julialang.org/t/optimization-problem-that-fails-to-converge/121666/4 "2024-10-24T07:55:55Z")

</div>

Thanks both of you, it helped me a lot!  
Both methods are now working perfectly.  
I had to struggle a little bit with the matrix multiplications but in the end, it worked, thanks.
