fsolve to find circle intersections
51 views (last 30 days)
Show older comments
ricard molins on 8 Apr 2015
Commented: Seif eddine Seghiri on 20 Apr 2020
I'm trying to find the intersection points of two circles using fsolve. Currently I'm using this code but the fsolve command doesn't reach a conclusion, probably because I'm not choosing a good initial guess.
function F = myfun1( X)
y = X(2)
F=[(x-1).^2 +(y-2).^2 - 1.5^2 ;
(x-3).^2 +(y-2.5).^2 - 1^2 ;
And I'm calling it by using:
xo = [0,0];
X= fsolve (@myfun1,xo,options)
fsolve stops iterating because "last step was ineffective"
thanks in advance for any help provided
Mohammad Abouali on 12 Apr 2015
Edited: Mohammad Abouali on 14 Apr 2015
% Center of the circles
% Radius of the circles
% x1(x,y) on the first circle satisfy the following equation:
% x2(x,y) on the second circle satisfy the following equation:
% once intersecting we have:
F=@(x) ([(x(1)-C1(1))^2+(x(2)-C1(2))^2-R1^2; ...
fprintf('First intersection point: (%f,%f)\n',x(1),x(2));
fprintf('Second intersection point: (%f,%f)\n',x(1),x(2));
when you run it you get:
First intersection point: (0.900000,2.861818)
Second intersection point: (0.900000,-2.861818)
The main difference here is that the initial guess or starting points is located on one of the circles (so it satisfies one of the equations and not the other. (0,0) that you start doesn't satisfies any of the equations. The convergence rate would be much worse there. setting starting guess as (x,y)=(C(1), C(2)+R) helps it, and that is always the point right on top of one of the circles. Not necessarily the intersection. And Setting the initial guess like this is by no mean any restriction, and in fact choosing proper initial guess is always encouraged.
Alternatively you could have posed this as a constrained optimization problem. Then you had more flexibility for the initial guess.
Seif eddine Seghiri on 20 Apr 2020
@Mohammad Abouali please i want your help when the fsolve is now longer deals with two circles, what if there's 10 or 20 circles ?
More Answers (1)
Roger Stafford on 8 Apr 2015
In case you are interested, there is a much more direct way of finding the two intersection points of two circles than using 'fsolve'.
Let P1 = [x1;y1] and P2 = [x2;y2] be column vectors for the coordinates of the two centers of the circles and let r1 and r2 be their respective radii.
d2 = sum((P2-P1).^2);
P0 = (P1+P2)/2+(r1^2-r2^2)/d2/2*(P2-P1);
t = ((r1+r2)^2-d2)*(d2-(r2-r1)^2);
if t <= 0
fprintf('The circles don''t intersect.\n')
T = sqrt(t)/d2/2*[0 -1;1 0]*(P2-P1);
Pa = P0 + T; % Pa and Pb are circles' intersection points
Pb = P0 - T;
Find more on Calculus 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!