Q:

Find points of intersection matlab

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

A:

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.

