# Optimization of Curves

**URL:** <https://discourse.julialang.org/t/optimization-of-curves/50312>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [November 17, 2020, 5:31pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312 "2020-11-17T17:31:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [November 17, 2020, 5:31pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/1 "2020-11-17T17:31:26Z")

</div>

What is a good option for finding the x value of a tangent point of a curve (such as matching price and value), or finding the minimum or maximum of a curve.

---

<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:** [November 17, 2020, 6:00pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/2 "2020-11-17T18:00:43Z")

</div>

Depends — what kind of curve do you have, and how is it specified?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [November 17, 2020, 6:16pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/3 "2020-11-17T18:16:13Z")

</div>

Elsewhere I was shown how to use the intermediate value theorem. I also would want to find local minimums and maximums, critical values and Fermat’s theorem.

I’m not entirely sure what you mean by curves. It would be for continuous functions.

---

<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:** [November 17, 2020, 6:48pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/4 "2020-11-17T18:48:50Z")

</div>

For example, do you have a smooth function of a single variable, and a program that can evaluate this function at any desired point?

If so, you can use ApproxFun.jl for this sort of thing. You can give it a smooth function on an interval and ask for all of the extrema, find where the derivative matches a certain value, etcetera.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [November 17, 2020, 6:56pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/5 "2020-11-17T18:56:50Z")

</div>

Ok thanks, I can look into that, I think most are smooth variables, some periodic or with several minima, I was using Roots, to find the point of a curve with a slope of .01, but it didn’t give the number.

```julia
using Roots
∂xf(x)=0.1 * exp(0.1 * x)
g(x) = ∂xf(x)- .01
find_zeros(g, -1, 1)  

```

it just says float64

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [November 17, 2020, 7:10pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/6 "2020-11-17T19:10:54Z")

</div>

`find_zeros` returns `Float64[]`, an empty array of `Float64` to let you know that there are no zeros of the given function over that interval. Perhaps you meant `g(x) = ∂xf(x)- .1`?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [November 17, 2020, 7:15pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/8 "2020-11-17T19:15:32Z")

</div>

Yeah my bad, I figured it out, x=-23… so when I changed the range to -100,100 it worked perfectly. Is there a way to set it so that I can have

```julia
using Roots
∂xf(x)=0.1 * exp(0.1 * x)
g(x) = ∂xf(x)- .01
t=find_zeros(g, -100, 100)  
dy=f(t)

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [November 17, 2020, 8:09pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/9 "2020-11-17T20:09:39Z")

</div>

I’m assuming `f` is a function of a single variable, and `find_zeros` returns an array of values (possibly of length 1, but not guaranteed, as you discovered). In that case, not without sorting out the zero you are looking for or broadcasting `f` over the returned values. However, if you know there is only one zero, say through monotonicity or some other property, you can use bisection, as in `t = find_zero(g, a, b)` which will return a number, not a vector. As an aside, a more robust alternative to `find_zeros` (though maybe even a bit more difficult to work with the output) is `IntervalRootFinding`’s `roots` function.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [November 17, 2020, 8:50pm UTC](https://discourse.julialang.org/t/optimization-of-curves/50312/10 "2020-11-17T20:50:55Z")

</div>

Here’s how to use `IntervalRootFinding.jl` for that, trying to address @j_verzani’s valid point about the output being difficult to use. (The timing is on the second run, i.e. post compilation.)

```julia
julia> using IntervalArithmetic, IntervalRootFinding

julia> g(x) = 0.1 * exp(0.1 * x) - 0.01
g (generic function with 1 method)

julia> @time rts = roots(g, -Inf..Inf)
  0.110170 seconds (30.03 k allocations: 1.707 MiB)
1-element Array{Root{Interval{Float64}},1}:
 Root([-23.0259, -23.0258], :unique)

julia> rts2 = mid.(interval.(rts))
1-element Array{Float64,1}:
 -23.025850929389335

```

The first result proves that there is a unique root of this function on the real line (since only one result was returned in the array).  
The second returns a good approximation as floats.

EDIT: You can also get more accurate roots by using `BigFloat`s once you’ve found these first approximations:

```nohighlight
julia> @time rts3 = roots(g, big.(rts), Newton, 1e-50)
  0.002826 seconds (1.45 k allocations: 84.455 KiB)
1-element Array{Root{Interval{BigFloat}},1}:
 Root([-23.0259, -23.0258]₂₅₆, :unique)

julia> diam.(rts3) # diameters (widths) of the intervals
1-element Array{BigFloat,1}:
8.29072181289066684037089778828838358827136034985883012962275533136184813103593e-76

julia> rts4 = mid.(interval.(rts3))
1-element Array{BigFloat,1}: -23.02585092994045684017991454684364207601101488628772976033327900967572609677374

```

In general it is not possible to use an actual infinite interval. In that case you can just use a very wide interval like `-1e10..1e10`.

You can also use this together with e.g. `ForwardDiff` for automatic differentiation, if you need to use derivatives of a given function.
