MATLAB Answers

How to find common tangents between 2 ellipses ?

16 views (last 30 days)
Adrien Luthi
Adrien Luthi on 3 Nov 2020
Commented: Matt J on 2 Aug 2021
Hi everyone,
I am trying to find the common tangents between 2 ellipses by using solve function. I have f(x1) and g(x2) which are my 2 elipses (ellipse 1 the smallest and ellipse 2) equations and the 2 unknows (x1 and x2) and their derivative at each point which gives me the slope of their tangents at each point.
Then, my equations to solve are:
and here my Matlab code:
syms x1 x2
f_x1 = -sqrt(b1^2*(1-((x1-xC1)/a1)^2)) + yC1;
g_x2 = sqrt(b2^2*(1-((x2-xC2)/a2)^2)) + yC2;
f_x1_prime = b1^2*(x1-xC1)/(a1^2*sqrt(b1^2*(1-((x1-xC1)/a1)^2)));
g_x2_prime = -b2^2*(x2-xC2)/(a2^2*sqrt(b2^2*(1-((x2-xC2)/a2)^2)));
eq1 = (g_x2 - f_x1)/(x2 - x1) == f_x1_prime;
eq2 = f_x1_prime == g_x2_prime;
[S] = solve(eq1, eq2, x1, x2, 'ReturnConditions', true, 'Real', true);
assume(S.conditions)
restriction = [S.x1>xC1, S.x1<xC1+a1, S.x2<xC2, S.x2>xC2-a2];
solx = solve(restriction, S.parameters);
x1 = subs(S.x1, S.parameters, solx);
x2 = subs(S.x2, S.parameters, solx);
As result, I have something in S but the warning and no solution after having assume the condition and solve the parameter.
S =
struct with fields:
x1: [1×1 sym]
x2: [1×1 sym]
parameters: [1×1 sym]
conditions: [1×1 sym]
and the warning:
Warning: Unable to find explicit solution. For options, see help.
> In sym/solve (line 317)
In findTangentPoints (line 24)
In testTangent (line 11)
Does anyone can help me to use this function solve or see any errors ? I am going to be crazy...
Thanks a lot everyone and have a nice evening!
  1 Comment
John D'Errico
John D'Errico on 25 Dec 2020
Note that as you have written f and g, you are restricting an ellipse to a specific branch of that curve. That happens as soon as you take a sqrt. In fact, a pair of non-intersecting ellipses will have FOUR common tangent lines.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 25 Dec 2020
Edited: Matt J on 1 Feb 2021
alpha1=30; alpha2=-45; %EDIT
a1=0.015; b1=0.020; xC1=0.13; yC1=0.09;
a2=0.08; b2=0.10; xC2=0.27; yC2=0.11;
T1=eye(3); T1(1:2,end)=[-xC1;-yC1]; %translation matrix
T2=eye(3); T2(1:2,end)=[-xC2;-yC2];
E1=diag(1./[a1^2,b1^2 -1]);
E2=diag(1./[a2^2,b2^2,-1]);
R=@(t) [cosd(t), -sind(t) 0; sind(t) cosd(t) 0; 0 0 1]; %EDIT
C1=T1.'*R(alpha1).'*E1*R(alpha1)*T1; %homogeneous ellipse equation matrix
C2=T2.'*R(alpha2).'*E2*R(alpha2)*T2;
syms Lx Ly Lz %line coefficients
equ1=[Lx,Ly,Lz]/C1*[Lx;Ly;Lz]==0; %tangent to ellipse 1
equ2=[Lx,Ly,Lz]/C2*[Lx;Ly;Lz]==0; %tangent to ellipse 2
equ3=Lx^2+Ly^2+Lz^2==1; %resolve the scale ambiguity in the coefficients
sol=solve([equ1,equ2,equ3]);
sol=double([sol.Lx,sol.Ly,sol.Lz]); %convert symbolic solutions to numeric doubles
sol=sol(2:2:end,:);%discard redundant solutions
%%% Display the solutions
fimplicit(@(x,y) qform(x,y,C1));
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
hold on
fimplicit(@(x,y) qform(x,y,C2));
for i=1:4
fimplicit(@(x,y) lform(x,y,sol(i,:))); %dumb warnings - ignore
end
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
hold off
axis equal, grid on
axis([0.1,0.4,0,.24])
function val=qform(x,y,Q)
sz=size(x);
xy=[x(:),y(:),x(:).^0];
val=reshape( sum( (xy*Q).*xy ,2) ,sz);
end
function val=lform(x,y,L)
sz=size(x);
val=reshape( [x(:),y(:),x(:).^0]*L(:) ,sz);
end
  14 Comments
Matt J
Matt J on 2 Aug 2021
I can't understand what the code is doing, but as I said before, the question belongs in its own thread. Also, I am now even more certain that the problem is underdetermined. In the 3D case, you have three unknowns (a plane has 3 degrees of freedom) but only two equations (one for each ellipsoid).

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!