Clear Filters
Clear Filters

Intersection of line and curve from thier points

19 views (last 30 days)
I have a line with 2 points on it and a curve with 4 points on it
For the line we have :
x_line = [7020 0]
y_line = [0 250000]
For the curve we have :
x_curve = [5620 4850 4290 3600]
y_curve = [0 100000 200000 300000]
I need to know the intersection point of the line and the curve
Any help will be appreciated.

Accepted Answer

Star Strider
Star Strider on 8 Dec 2017
Try this:
x_line = [7020 0];
y_line = [0 250000];
x_curve = [5620 4850 4290 3600];
y_curve = [0 100000 200000 300000];
b_line = polyfit(x_line, y_line,1); % Fit ‘line’
y_line2 = polyval(b_line, x_curve); % Evaluate ‘line’ At ‘x_curve’
x_int = interp1((y_line2-y_curve), x_curve, 0); % X-Value At Intercept
y_int = polyval(b_line,x_int); % Y-Value At Intercept
figure(1)
plot(x_line, y_line)
hold on
plot(x_curve, y_curve)
plot(x_int, y_int, '+r') % Plot Intercept Point
hold off

More Answers (3)

Mohamed Ameen
Mohamed Ameen on 8 Dec 2017
Thank you very much, it worked... But if you could explain the steps a little more, that will be great.
Thanks again for your help.
  1 Comment
Star Strider
Star Strider on 8 Dec 2017
As always, my pleasure.
First, ‘line’ is a linear function, so creating an equation for it (using polyfit) is straightforward. I cannot compare the y-values for ‘y_line’ and ‘y_curve’ directly, so I evaluate ‘line’ at the ‘x_curve’ points to create y-values at those points as ‘y_line2’. I then subtract ‘y_curve’ from them, and use interp1 to find the x-value (as ‘x_int’) where they are 0. I then use polyval with ‘x_int’ to get the y-value at the interception, ‘y_int’.
The plots are then straightforward.

Sign in to comment.


Mohamed Ameen
Mohamed Ameen on 8 Dec 2017
Thanks a lot Mr. Strider, that's really helpful

Jamie Hetherington
Jamie Hetherington on 24 Dec 2019
Starstrider this solution was excellent. I needed a method to identify the point of intersection between a straight line and a plotted curve of residuals. I was thinking it could be done solving simulatenous equations but would've taken more time to create and implement.
Great stuff!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!