# Linearization

**URL:** https://discourse.julialang.org/t/linearization/58167
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [March 29, 2021, 3:31pm UTC](https://discourse.julialang.org/t/linearization/58167 "2021-03-29T15:31:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![user1200](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@user1200](https://discourse.julialang.org/u/user1200)
#### Post date: [March 29, 2021, 3:31pm UTC](https://discourse.julialang.org/t/linearization/58167/1 "2021-03-29T15:31:37Z")

</div>

Hi  
I have simple objective function (goal), solving with Ipopt  
for example:  
`Min, sum (sin(data[i]*x) for i=1:length(data) )`

I was just curious if there is some way/wrap to achieve :  
maximize linearity of **process** to goal.

Like in this example obj is Min, sum - so process is simple cumsum.  
_Reason behind this is to work with trajectory (process), for example finding path which looks (the most) like “line”._

I tried like “distance between process’s segments”, but it doesnt seem to work in Ipopt.  
(I found some math hints but too math 😊

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [March 29, 2021, 8:32pm UTC](https://discourse.julialang.org/t/linearization/58167/2 "2021-03-29T20:32:16Z")

</div>

Hi there.

> I tried like “distance between process’s segments”

Take a read of [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757). It’s easier to help if you can provide the code as a minimal working example.

> maximize linearity of **process** to goal

Can you clarify what this means?

---

<div class="post-metadata">

### Author: ![user1200](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@user1200](https://discourse.julialang.org/u/user1200)
#### Post date: [March 30, 2021, 10:44am UTC](https://discourse.julialang.org/t/linearization/58167/3 "2021-03-30T10:44:25Z")

</div>

Hi  
Thanks, yes:  
Code is

```julia
using Random
using JuMP
using Ipopt
Random.seed!(1234)
n=1000
data = randn(n)
model = Model(Ipopt.Optimizer)
@variable(model, x)
@variable(model, a)	#max distance
@NLexpressions(model, begin
        y[ii=1:10], sum(	sin(x*data[i]) for i = 1+floor(Int,(n/10)*ii-1):floor(Int,(n/10)*ii) )	# 10 segments y[1:10] - just cumsum
        yy[ii=1:9], (-y[ii]+y[ii+1]	)^2	# just abs(distance) , but ^2
end)
@NLconstraint(model, [i=1:9],yy[i] <= a)	#max distance , 10 segments=9 distances
#@NLconstraint(model, [i=1:9],yy[i] >= 2)	#next to implement : slope of process, each segment must raise minimum 2)
@NLobjective(
        model,
Min,
	a # minimize max distance
)
optimize!(model)
print(	objective_value(model)	)
value.(y) # currently zeros(10), but when slope added

```

**Usage:**  
Normaly I just min/max objective function, but in graph ( **y** here) I observe sometimes very strange behaviours. So if graph of process looks like line, it would be very good.

**Example:**  
I have other max objective function working with Ipopt. Sometimes value of y[i] jumps above global optimum, then went down.  
From time perspective (y[1]…y[10]), I was curious, if there is (simplyfied 🥦 ) way to ‘model’ graph.

Image:  
(1) Current situation, ignoring graph, only min/max obj (newest y[10]  
(2) Better graph  
\*(3) Line graph  
\*(4) Best line graph with high obj  
_( \* good solutions)_

 ![aay](https://global.discourse-cdn.com/julialang/original/3X/b/d/bdc04bcc382420a6f4030aa1ec76eec5de4cf2f5.png)

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [March 30, 2021, 10:09pm UTC](https://discourse.julialang.org/t/linearization/58167/4 "2021-03-30T22:09:33Z")

</div>

I still don’t really understand the problem I’m afraid. You’re picking `x` that minimizes the maximum distance between two points `y[i]`?

Ipopt assumes the problem is convex, so using a function like `sin` will result in a locally optimal solution. You may see some weird artifacts. You could try multiple restarts by using `@variable(model, x, start = 1.0)` for different `start` values.
