# How to add this formula as an objective function?

**URL:** https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [October 1, 2019, 1:33am UTC](https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345 "2019-10-01T01:33:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Shor](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Shor](https://discourse.julialang.org/u/Shor)
#### Post date: [October 1, 2019, 1:33am UTC](https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345/1 "2019-10-01T01:33:18Z")

</div>

Hi, I am new here.  
I don’t know how to add this formula in my minimization objective function.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/b/ab84da0697712ad4a180636fc83eba41fab94ca6.png)  
where //x//\_1 stands for the sum of the absolute values of the components of x. (x is a 10x1 array)  
Anyone can help me with this?

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [October 1, 2019, 5:16am UTC](https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345/2 "2019-10-01T05:16:16Z")

</div>

maybe `sum(abs.(x))` ? the dot means that the function is applied over all elements of the array (is called broadcasting)

---

<div class="post-metadata">

### Author: ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)
#### Post date: [October 1, 2019, 5:50am UTC](https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345/3 "2019-10-01T05:50:37Z")

</div>

You can handle this problem by lifting it’s formulation, i.e. introducing new dummy decision variable t, adding constraints \quad x ≤ t; \quad -x ≤ t and then modifying the objective function as \lVert Ax - b\rVert\_2 + λt. With this formulation we reduced the problem to the classical constrained quadratic program, which can be handled easily with JuMP (check it’s documentation)

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [October 1, 2019, 6:45am UTC](https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345/4 "2019-10-01T06:45:44Z")

</div>

I don’t think that works if `x` is a vector (your inequalities interpreted point wise give the infinity norm, not the one norm).

---

<div class="post-metadata">

### Author: ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)
#### Post date: [October 1, 2019, 7:46am UTC](https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345/5 "2019-10-01T07:46:43Z")

</div>

Yes, you are right. This should work:  
\min ∥ A x − b ∥\_2 + λ \sum\_{i=1}^{n} t\_i;\quad x \preccurlyeq t ;\quad − x \preccurlyeq t

---

<div class="post-metadata">

### Author: ![Shor](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Shor](https://discourse.julialang.org/u/Shor)
#### Post date: [October 1, 2019, 3:21pm UTC](https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345/6 "2019-10-01T15:21:33Z")

</div>

Yes. I tried this code but Julia returns this error: MethodError: no method matching abs(::Array{VariableRef,1})

---

<div class="post-metadata">

### Author: ![Shor](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Shor](https://discourse.julialang.org/u/Shor)
#### Post date: [October 1, 2019, 4:15pm UTC](https://discourse.julialang.org/t/how-to-add-this-formula-as-an-objective-function/29345/7 "2019-10-01T16:15:51Z")

</div>

It works. Thank you!
