How to find the intersection of two curves
59 views (last 30 days)
Show older comments
theta=0:pi/400:2*pi;
x=sin(pi*theta)+cos(pi*theta);
y=cos(pi*theta)-2*sin(pi*theta);
x1=sin(pi*theta)+cos(pi*theta)+2;
plot(y,x,y,x1);
hold on
0 Comments
Answers (4)
Walter Roberson
on 29 Dec 2016
You can also proceed symbolically if you have the symbolic toolbox:
syms theta Y
y = cos(pi*theta)-2*sin(pi*theta);
THETA = solve(y == Y, theta);
x = sin(pi*THETA(2)) + cos(pi*THETA(2));
x1 = sin(pi*THETA(1)) + cos(pi*THETA(1)) + 2;
y_common = solve(x == x1, Y);
x_common = simplify( subs(x, Y, y_common), 'step', 20)
Roger Stafford
on 29 Dec 2016
Edited: Roger Stafford
on 29 Dec 2016
You can convert this into the equations of two slanted ellipses. The first ellipse is:
(x-y)^2+(2*x+y)^2 = 5*x^2+2*x*y+2*y^2 = 9
(Can you see why?)
The second ellipse must therefore be:
5*(x-2)^2+2*(x-2)*y+2*y^2 = 9
(Can you see why that is so?)
By subtracting the left sides of these two equations you get a straight line. Solving for y in terms of x in the line and substituting that back into the equation of the first ellipse gives you a quadratic equation in x which has two solutions, namely the x values of the two intersections of the two ellipses. You can then easily find the two corresponding y’s.
2 Comments
Walter Roberson
on 30 Dec 2016
Please post the code for the curves, not just a partly-obscured picture of the code.
Roger Stafford
on 31 Dec 2016
Edited: Roger Stafford
on 1 Jan 2017
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 % Define acceptable error tolerance
rpt = true; % Pass through while-loop at least once
while rpt % Repeat while-loop until rpt is false
x1 = f1(t1); y1 = g1(t1);
x2 = f2(t2); y2 = g2(t2);
rpt = sqrt((x2-x1)^2+(y2-y1)^2)>=tol; % Euclidean distance apart
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); % <-- These last two lines added later
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 x2 y1 y2 t1 t2
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.
0 Comments
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!