How to indicate intersection points on graph?

265 views Asked by At

I am using Insertion and Merge sort and trying to indicate the intersection point on the graph below. I found a possible solution on rosettacode, but a bit confusing for a newbie like me... Where should I look, do you guys know how to indicate it in Julia?

The output would is like enter image description here

I am trying to show something like this enter image description here

1

There are 1 answers

4
Carlos A. Michelén Ströfer On

Your data has some noise, which means there might be more than one intersection point (all close). Assuming no noise, just two monotonically increasing functions, this should work:

    using Plots
    x = 1:5
    y1 = x .+ 5
    y2 = 3x 
    idx = argmin(abs.(y1-y2))
    plot(x, [y1, y2], label=["y1", "y2"], markershape=:circle)
    scatter!([x[idx]], [y1[idx]], label="intersection", markershape=:circle, markersize=10)

See the resulting plot here.

Note that this method gives you 1/4 possible points closest to the intersection. The intersection itself is not defined for functions only defined from discrete points. By changing the order of y1 and y2 in the code above you can get all 4 points. For fine enough discretization using one of those 4 points might be good enough for your needs.

A better approximation would be to use those 4 points to fit 2 lines, for y1 and y2, and find the intersection of those two lines analytically (e.g. with the equations y1=m1*x+b1, ...). But I leave this to you ;)