Home > Back-end >  How to find crossection of hline and a function in python?
How to find crossection of hline and a function in python?

Time:08-12

I have created a plot like in the figure below, red plot is original data, blue is a fitted function, the horizontal lines are different levels. I need to find the both intersection points with each line. do you have any suggestions ? Thanks In advance.

fitting

CodePudding user response:

An easy way to do this numerically is to subtract the y-value of each horizontal line from your fit and then to solve the equation fit(x) - y = 0 for x. For this, scipy.optimize.fsolve can be used as follows (enter image description here

CodePudding user response:

  1. A simple solution to this would be to just calculate the intersection points knowing the functions of the lines.

Example:

Line 1 : y = 2x

Line 2 : y = x^2

Intersection points:

2x = x^2

0 = x^2 - 2x

x1 = 2

x2 = 0

For y just substitute x in one of the functions,

y1 = 2x y1 = 2*(2) y1 = 4

y2 = 2x y2 = 2*(0) y2 = 0

Intersection point 1 of line 1 and line 2:

Itersection point 1: (2, 4)

Intersection point 2: (0,0)

enter image description here

Hope this helps.

  • Related