# Find intersection on graph matlab ## How do I find the intersection point of two lines in MATLAB 6.5 (R13)?

Currently, there is no function in MATLAB that allows you to find intersection of any two lines or line segments. If you know that two lines in 2D intersect (are not skew) and you know two points on each of those lines, you can find the intersection using the following formula:

L1_x1 = 1;

L1_y1 = 2;

L1_x2 = 3;

L1_y2 = 4;

L2_x1 = 1;

L2_y1 = 4;

L2_x2 = 3;

L2_y2 = 3;

plot([L1_x1 L1_x2], [L1_y1 L1_y2])

hold on

plot([L2_x1 L2_x2], [L2_y1 L2_y2])

Dx12 = L1_x1-L1_x2;

Dx34 = L2_x1-L2_x2;

Dy12 = L1_y1-L1_y2;

Dy34 = L2_y1-L2_y2;

Dx24 = L1_x2-L2_x2;

Dy24 = L1_y2-L2_y2;

ts = [Dx12 -Dx34; Dy12 -Dy34] \ [-Dx24; -Dy24];

P = ts(1)*[L1_x1; L1_y1] + (1-ts(1))*[L1_x2; L1_y2];

Q = ts(2)*[L2_x1; L2_y1] + (1-ts(2))*[L2_x2; L2_y2];

plot(P(1), P(2), 'ro')

plot(Q(1), Q(2), 'bo')

hold off

P and Q both contain the values of the common intersection point.

Alternately, if you have discrete data and know that some point is in data sets for both lines, you can use the "intersect" function.

Finally, if you own the "Mapping Toolbox" for MATLAB, you can use the "polyxpoly" function to calculate the intersection point.

For more information on these or any other command, type the following at the MATLAB command prompt:

help function_name

doc function_name

## Plotting and finding the intersection of 2 curves

Sell = [12,22,28]

Price1 = [15,30,50]

Price2 = [25,42,50]

plot(Sell, Price1, 'b.-', 'LineWidth', 2, 'MarkerSize', 20);

grid on;

hold on;

plot(Buy, Price2, 'r.-', 'LineWidth', 2, 'MarkerSize', 20);

xlabel('Sell or Buy Price in \$', 'FontSize', 20);

ylabel('Price1 or Price2 in \$', 'FontSize', 20);

coeff1 = polyfit(Sell(2:3), Price1(2:3), 1)

matchPrice = (coeff2(2) - coeff1(2)) / (coeff1(1) - coeff2(1))

y = coeff1(1) * matchPrice + coeff1(2)

caption = sprintf('Match at x = \$%.2f, y = \$%.2f', matchPrice, y);

title(caption, 'FontSize', 20);

darkGreen = [0, 0.5, 0];

line([matchPrice, matchPrice], [0, y], 'Color', darkGreen, 'LineWidth', 2);

line([0, matchPrice], [y, y], 'Color', darkGreen, 'LineWidth', 2);

fprintf('Done running %s.m ...\n', mfilename); ## How to find the intersection of two curves

You can use a modified version of the Newton-Raphson method for finding the intersection of two parameterized curves, provided the parameter functions defining the curves can be differentiated. For each intersection point the method requires an estimated value for each of the two parameters that would yield that point.

Let x1 = f1(t1), y1 = g1(t1) define one curve and x2 = f2(t2), y2 = g2(t2) define the second curve. Let their respective derivative functions be called df1dt1, dg1dt1, df2dt2, and dg2dt2. Let t1e and t2e be the respective estimated values of t1 and t2 for some intersection point. Then do this:

t1 = t1e; t2 = t2e;

tol = 1e-13

rpt = true;

while rpt

x1 = f1(t1); y1 = g1(t1);

x2 = f2(t2); y2 = g2(t2);

rpt = sqrt((x2-x1)^2+(y2-y1)^2)>=tol;

dt = [df1dt1(t1),-df2dt2(t2);dg1dt1(t1),-dg2dt2(t2)]\[x2-x1;y2-y1];

t1 = t1+dt(1); t2 = t2+dt(2);

end

x1 = f1(t1); y1 = g1(t1);

x2 = f2(t2); y2 = g2(t2);

I tried this with a slightly modified version of your original ellipses:

x1 = sin(t1)+cos(t1); y1 = cos(t1)-2*sin(t1);

x2 = sin(t2)+cos(t2)+1.5; y2 = cos(t2)-2*sin(t2)+1;

using the estimates t1 = 1 and t2 = 2.4, determined by an appropriate inspection of the plots of the two curves, and had the following results:

x1 x2y1y2t1t2

1.3817732906760 1.4380694650099 -1.1426396637477 -1.0883200766435 1.0000000000000 2.4000000000000

1.3930006621432 1.3942125905603 -1.0625407651143 -1.0624834020886 0.9588192419883 2.4310674208882

1.3930927453049 1.3930928781613 -1.0617974598551 -1.0617968855823 0.9584414863689 2.4318614253186

1.3930928207253 1.3930928207253 -1.0617968503328 -1.0617968503328 0.9584411766348 2.4318614660485

As you can see, in three steps from the original estimates an intersection point was found to an accuracy of at least 13 decimal places. The same method can be used for the second intersection point of these curves, given an appropriate estimate of the corresponding parameters.

Nonlinear Equation and fsolve() in Matlab

## (Automatically) Finding the intersection point of two graphes in my plot

I have written a script to analyse vectors out of a .txt-file und displays them under certain criteria. The plot I get looks like this. Now I am trying to find the coordinates of the intersection point between my plot and the right red line.

I don't have functions to describe the lines, only vectors containing the data points. Bot yvectors are plotted over the same xvector. I thought a simple for-loop would suffice but the problem is, that the needed y-value in both y-vectors has a differing location in the vector. So for example if the right value was located in yvector1(100) it would be located in yvalue2(70).

I hope i could somehow explain my problem in an understandable way. Every help is very much appreciated.

The kind of simple loop i tried looks like this:

for cnt=1:numel(xvector)

s1=yvector1(cnt);

s2=yvector2(cnt);

zz=abs(s1-s2);

if zz <= 0.005

fprintf('location at %1.2f kN and %1.2f mm\n\n',yvector1(cnt),xvector(cnt))

end

end

## How to find intersection between line and curve

Just because the only tool you own is a hammer does not mean every problem must be treated as itf it is a nail. Similarly, just because the only tool you know how to use is polyfit, does not make that a remotely good idea to use here.

The simplest solution is to use a tool designed to solve your problem. Almost certainly, the proper tool here is a spline model, of SOME UNSPECIFIED SORT. However, you could also easily just use tools which use linear interpolation, as that would be pretty accurate. In fact though, linear interpolation can be viewed as just a low order spline.

You have not provided any data, so I cannot show you how to solve this directly, so I'll need to make up some data. Sadly, I'm not feeling very creative today, but there really is no need to be that creative. Simple will do.

x = -5:.1:5;

y = sin(x);

plot(x,y,'o')

yline(0.1); OK. So how can we find the locations of the intersections of my simple function, with the horizontal line at y==0.1? Yes, I know that happens at the multiple solutions of the problem sin(x)==0.1. So what? In fact, there are multiple solutions, and we wish to find them all. However, for purpose of comparison, it will be useful to use the analytical solution from asin. The primary solution lives here:

x0 = asin(0.1);

x0 =

0.10016742116156

But there are also solutions at infinitely many locations, the three solutions we should find from the graphic lie at:

x0 = [-pi - x0, x0, pi - x0];

sin(x0)

ans =

0.0999999999999998 0.1 0.1

As you should see, all three elements of x0 yield the desired value for sin(x0). The tiny discrepancy in one of them is just floating point trash in the least significant bits.

How should we find those points now? DO NOT USE POLYFIT! DO NOT USE POLYFIT! DO NOT USE POLYFIT!!!! There, I've said it three times, so it must be true.

Instead, again, we need to use a valid tool for the purpose. The first one I'll suggest is Doug Schwarz's intersections tool. It lives on the file exchange. (Link provided below.)

[xlin,ylin] = intersections(x,y,[-5 5],[.1 .1])

xlin =

-3.24188910837421

0.10016854536593

3.04131030579016

ylin =

0.1

0.1

0.1

Intersections just uses linear interpolation to find those points of intersection. In fact, each of them looks to be correct to within roughly 4 significant digits.

Next, we could use a spline. Both spline or pchip will be acceptable here, though spline might be more accurate for many problems. pchip also will be good, sometimes best on problems with significant slope changes, or flat regions, where use of a spline may result in ringing behavior.

I'll use a tool of my own to generate the functional inverse of that spline to give the three solutions. (Again, you can download my SLM toolbox from the file exchange. I'll give a link at the end.)

spl1 = pchip(x,y);

xpchip = slmsolve(spl1,0.1)

xpchip =

-3.24176645744013 0.100167703314966 3.04143262180259

spl2 = spline(x,y);

xspline = slmsolve(spl2,0.1)

xspline =

-3.24176010343974 0.100167421255852 3.04142521171113

Now, how well did we do?

abs(x0 - xlin')

ans =

0.000129033622857655 1.12420437066441e-06 0.000114926638069335

abs(x0 - xpchip)

ans =

6.38268877750647e-06 2.82153406486185e-07 7.3893743612885e-06

abs(x0 - xspline)

ans =

2.86883903299895e-08 9.42918382262903e-11 2.0717103588197e-08

To be honest, probably any of the three solutions are probably adequate for most purposes. But for god sakes, DON'T USE POLYFIT! Just because Taylor series are effectively made from polynomials, does not mean they are a good choice.

https://www.mathworks.com/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersections

https://www.mathworks.com/matlabcentral/fileexchange/24443-slm-shape-language-modeling

Note that while parts of my SLM toolbox use the optimization toolbox, slmsolve uses nothing special itself.

Find Intersections in Matlab 1

## How find and plot intersection of specific points on the graph

Introduction: There are two signals in time domain.First one is a vibration signal in time domain and another is a phase signal in the same time domain.  Problem:

Now the goal is to mark points on the vibration signal at exactly the point where we have peaks in phase signal as show in figure. I don't want the graphs intersection point!!!! All I wanted to find is point on the vibration signal when I have a peak in reference phase signal. The above is the vibration signal(red) along with the reference signal(blue) plotted in same time domain

[pk,lc] = findpeaks(reference_signal,'MinPeakDistance',0.001,'MinPeakHeight',0.05);

The used the find peaks function to find the locations of peak (of phase signal) in Time , which means for the entire range of signal I would get only 13 unique points (lc) in time axis as

lc = [ 0.0015, 0.0030, 0.0045, 0.0060, 0.0075, 0.0090, 0.0105, 0.0120, 0.0135, 0.0150, 0.0165, 0.0180, 0.0195]

whereas the time domain for the actual vibration signal would look like something like this.

Time_Vibration_signal = [0.00047, 0.00049, 0.00051, 0.00053, 0.00055, 0.00057, 0.00059, 0.00061, 0.00063 and so on]

Now I want to exactly mark the points lc on the vibration signal. i,e when the vibration signal crosses the first point (peak) say 0.0015 seconds I want a marker on the vibration signal and for the second point and so on.

I'm stuck after this point. Could someone help me this? Any hints or suggestions would be helpful. Thanks in advance

### You will also like:

.

430 431 432 433 434