# MATLAB: How to find common tangents between 2 ellipses

ellipseMATLABsolvetangent

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 x2f_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!

alpha1=30; alpha2=-45; %EDITa1=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 matrixT2=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]; %EDITC1=T1.'*R(alpha1).'*E1*R(alpha1)*T1; %homogeneous ellipse equation matrixC2=T2.'*R(alpha2).'*E2*R(alpha2)*T2;syms Lx Ly Lz  %line coefficientsequ1=[Lx,Ly,Lz]/C1*[Lx;Ly;Lz]==0; %tangent to ellipse 1equ2=[Lx,Ly,Lz]/C2*[Lx;Ly;Lz]==0; %tangent to ellipse 2equ3=Lx^2+Ly^2+Lz^2==1;       %resolve the scale ambiguity in the coefficientssol=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 solutionsfimplicit(@(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 onfimplicit(@(x,y) qform(x,y,C2));for i=1:4 fimplicit(@(x,y) lform(x,y,sol(i,:))); %dumb warnings - ignoreendWarning: 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 offaxis equal, grid onaxis([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);endfunction val=lform(x,y,L) sz=size(x); val=reshape( [x(:),y(:),x(:).^0]*L(:) ,sz);end