# How to find the intercept of two functions?

**URL:** <https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718>\
**Category:** New to Julia\
**Created:** [June 4, 2020, 10:58am UTC](https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718 "2020-06-04T10:58:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [June 4, 2020, 10:58am UTC](https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718/1 "2020-06-04T10:58:35Z")

</div>

How do I find the intercept of two functions? I thought it would be something simple like `findall(in(a), b)` but this gives me `Vector{Int64} with 0 elements`

MWE

```julia
attack_max2 = 0.90
attack_min2 = 0.52
β_V2 = 2.0
b0V2 = 16.0 
Bi = collect(0.1:0.1:200.0)

# clearance scenarios
demand = @. 1.0/((1.0/(attack_max2 + attack_min2))*Bi^β_V2/(b0V2^β_V2+Bi^β_V2)+1/attack_max2)
boom = @. (attack_max2 - attack_min2)*Bi^β_V2/(b0V2^β_V2+Bi^β_V2)+attack_min2

findall(in(demand), boom)

```

I know the overlap because the plot looks like

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/7/d7be996ac3021ca914b4338832889d047af2cfe6.png)

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [June 4, 2020, 11:05am UTC](https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718/2 "2020-06-04T11:05:52Z")

</div>

```julia
_, i = findmin(abs.(demand-boom))
[Bi[i] demand[i] boom[i]]

```

---

<div class="post-metadata">

**Author:** ![hwjsnc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hwjsnc/32/15178_2.png) [@hwjsnc](https://discourse.julialang.org/u/hwjsnc)\
**Post date:** [June 4, 2020, 11:11am UTC](https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718/3 "2020-06-04T11:11:19Z")

</div>

With the approach in this code, it will be very improbable to find an exact match between the curves. Mattrik’s reply works but the approach is very inefficient. You need a [root finding algorithm](https://en.wikipedia.org/wiki/Root-finding_algorithms) (noting that the difference between the curves is zero iff they have the same value). Bisection or Newton’s method are good starting points.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [June 4, 2020, 11:33am UTC](https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718/4 "2020-06-04T11:33:01Z")

</div>

I am open to suggestions on code as I am not hard set on it. I just did it that way because I thought it would work.

It’s basically function demand(Bi\*) = function boom(Bi\*) where Bi\* is the intercept, but I don’t know how to do that in Julia.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [June 4, 2020, 12:19pm UTC](https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718/5 "2020-06-04T12:19:19Z")

</div>

```julia

attack_max2 = 0.90
attack_min2 = 0.52
β_V2 = 2.0
b0V2 = 16.0

# clearance scenarios
demand_func(x) = 1.0/((1.0/(attack_max2 + attack_min2))*x^β_V2/(b0V2^β_V2+x^β_V2)+1/attack_max2)
boom_func(x) = (attack_max2 - attack_min2)*x^β_V2/(b0V2^β_V2+x^β_V2)+attack_min2

myfunc(x) = demand_func(x) - boom_func(x)

# julia> [myfunc(0),myfunc(50)]
# 2-element Array{Float64,1}:
# 0.3799999999999999
# -0.29324849299165867

SideStep(num) = num >= 0 ? 1 : -1

function BiSector(func::Function,x0,x1,maxdepth::Int64=1024,debugflag::Bool=false)
      fx0 = func(x0)
      fx1 = func(x1)
      if SideStep(func(x0)) * SideStep(func(x1)) == 1
          return "Error: f(x0) and f(x1) must be opposite signs"
      end
      depth = 0
      lowerx = x0
      upperx = x1
      lowervalue = fx0
      uppervalue = fx1
      while true
          midpointx = (lowerx + upperx) * 0.5
          if depth >= maxdepth
             return midpointx
          end
          lowsign = SideStep(lowervalue)
          upsign = SideStep(uppervalue)
          if lowsign*upsign == 1 || lowerx == midpointx || midpointx == upperx
              return midpointx
          end
          midpointvalue = func(midpointx)
          midsign = SideStep(midpointvalue)
          if midsign == upsign
              upperx = midpointx
              uppervalue = midpointvalue
          else
              lowerx = midpointx
              lowervalue = midpointvalue
          end
          if debugflag
              println(" depth = ",depth," lowerx = ",lowerx," upperx = ",upperx)
          end
          depth += 1
      end
  end

  flush(stdout)
  println("solution is ",BiSector(myfunc,0.0,50.0))

```

```julia
Starting Julia...
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.4.2 (2020-05-23)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
solution is 14.851477511590069

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [June 4, 2020, 12:25pm UTC](https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718/6 "2020-06-04T12:25:25Z")

</div>

You can use [Roots.jl](https://github.com/JuliaMath/Roots.jl) package.

```julia
using Roots
const attack_max2 = 0.90
const attack_min2 = 0.52
const β_V2 = 2.0
const b0V2 = 16.0 

demand(x) = 1.0/((1.0/(attack_max2 + attack_min2))*x^β_V2/(b0V2^β_V2+x^β_V2)+1/attack_max2)
boom(x) = (attack_max2 - attack_min2)*x^β_V2/(b0V2^β_V2+x^β_V2)+attack_min2

delta(x) = demand(x) - boom(x)

find_zero(delta, (0.1, 200.0), Bisection())
# 14.851477511590069

```

Or you can use Newton method

```julia
using ForwardDiff
D(f) = x -> ForwardDiff.derivative(f, float(x))
find_zero((delta,D(delta)), 10, Roots.Newton())
# 14.851477511590062

```

More details can be found in package itself.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [June 8, 2020, 12:30pm UTC](https://discourse.julialang.org/t/how-to-find-the-intercept-of-two-functions/40718/7 "2020-06-08T12:30:16Z")

</div>

Hello, I thought this was the answer but looking at the value, the intercept cannot be `14.851477511590069` or `14.851477511590062` since `demand(x)` and `boom(x)` never go over `1.0`.

HA, that is the x intercept… I am a silly goose.
