fsolve to find circle intersections

조회 수: 45 (최근 30일)
ricard molins
ricard molins 2015년 4월 8일
답변: Richard Zapor 2023년 8월 13일
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)
x= X(1)
y = X(2)
F=[(x-1).^2 +(y-2).^2 - 1.5^2 ;
(x-3).^2 +(y-2.5).^2 - 1^2 ;
];
end
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
Mohammad Abouali 2015년 4월 12일
편집: Mohammad Abouali 2015년 4월 14일
% Center of the circles
C1=[0,0];
C2=[5,0];
% Radius of the circles
R1=3;
R2=5;
% x1(x,y) on the first circle satisfy the following equation:
%(x1(1)-C1(1))^2+(x1(2)-C1(2))^2-R1^2=0
% x2(x,y) on the second circle satisfy the following equation:
%(x2(1)-C2(1))^2+(x2(2)-C2(2))^2-R2^2=0;
% once intersecting we have:
% x1(1)=x2(1);
% x1(2)=x2(2);
% then
F=@(x) ([(x(1)-C1(1))^2+(x(2)-C1(2))^2-R1^2; ...
(x(1)-C2(1))^2+(x(2)-C2(2))^2-R2^2]);
opt=optimoptions(@fsolve);
opt.Algorithm='levenberg-marquardt';
opt.Display='off';
x=fsolve(F,[C1(1),C1(1)+R1],opt);
fprintf('First intersection point: (%f,%f)\n',x(1),x(2));
x=fsolve(F,[C1(1),C1(1)-R1],opt);
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.
  댓글 수: 4
Salman  Khan
Salman Khan 2017년 5월 22일
Hi, C1=[0.1,1.6242]; C2=[0.1,0]; R1=.2503; R2=1.6242; The above code is failing for this case. Here, the initial guess is the center of one of the circles and instead of two contact points, only one is obtained.
Seif eddine Seghiri
Seif eddine Seghiri 2020년 4월 20일
@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 ?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Roger Stafford
Roger Stafford 2015년 4월 8일
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')
else
T = sqrt(t)/d2/2*[0 -1;1 0]*(P2-P1);
Pa = P0 + T; % Pa and Pb are circles' intersection points
Pb = P0 - T;
end
  댓글 수: 3
Anders Simonsen
Anders Simonsen 2017년 4월 3일
Thanks Roger, it works like a charm, especially for those who don't have the "fsolve" function.
Gergely Hunyady
Gergely Hunyady 2019년 10월 19일
Thanks. Working fine, much faster than fsolve

댓글을 달려면 로그인하십시오.


Richard Zapor
Richard Zapor 2023년 8월 13일
By first applying coordinate transformations a reduced algebra solution is possible. Given Circle (x1,y1,R) and Circle (x2,y2,P) find the two intersection points of the circles. Define d=distance(C1,C2). There are multiple conditions for Zero and One intersection points. Here we assume two points thus d<P+R, d+P>R, and d-P>-R.
  1. Translate to place (x1,y1) at the origin.
  2. Rotate to place (x2,y2) at (0,d) where d=distance(C1,C2).
  3. Y=(R^2P^2+d^2)/(2*d)
  4. X=sqrt(R^2Y^2)
  5. xy=[+X Y ;X Y] Two solution points in transformed space
  6. theta=atan2(x2x1,y2y1) A Matlab quadrant atan where -pi<atan2()<=pi
  7. xy=xy*rot(theta)+[x1 y1] where rot(t)=[cos(t) -sin(t); sin(t) cos(t)]
In the transformed space many simplifications occur. R^2=X^2+Y^2, P^2=X^2+(d-Y)^2 so after subtracting gives R^2-P^2=Y^2-(d-Y)^2= Y^2-d^2+2dY-Y^2 = 2dY-d^2 thus Y=(R^2-P^2+d^2)/(2*d) and X follows as X=sqrt(R^2-Y^2). Now de-rotate and de-translate to acheive the points in the original coordinate system.

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by