Question

This is my code:

%cirkel
t=linspace(0,2*pi);
r1=2;
x1=r1.*cos(t);
y1=r1.*sin(t);
cirkel=plot(x1,y1);
set(cirkel,'color','g')
axis equal
hold on

%cardeoide
t=linspace(0,2*pi);
r2=2*(cos(t)+1);
x2=r2.*cos(t);
y2=r2.*sin(t);

cardeoide=plot(x2,y2);
set(cardeoide, 'color','r')
hold off

I need to find the points where these curves intersect. I tried it by doing y1==y2 and x1==x2, but didn't get a good result (got a matrix full of numbers). Then I tried intersect(x1, x2). Also not a pleasing result. Then I resorted to this code. This gives a pretty accurate result. But I am not allowed to use this code (from school). So I was wondering is there a simpler solution?

thx in advance

Was it helpful?

Solution

To get the most accurate results you should not use your sampled points x1, x2, y1, y2. These will be approximations to the two curves, and the probability that the intersection is one of the sampled points is low.

Instead, set up the equations to find the t where the curves intersect and solve for it (x1(t) = x2(t) and y1(t) = y2(t)).

PS. The reason you get a matrix full of numbers when doing y1 == y2 and x1==x2 is very obvious when you get a hang of how MATLAB does things.

OTHER TIPS

Since you sample points on your curves, you will never find exactely the same point on both curves.

So. you should try to find the points with the smallest distance.

dists=pdist2([x1;y1],[x2;y2]);
[~,t1]=min(min(dists,[],2),[],1);
xInt1=x1(t1);
yInt1=y1(t1);
[~,t2]=min(min(dists,[],1),[],2);
xInt2=x2(t2);
yInt2=y2(t2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top