I'm dealing with a mathematics/astrodynamics problem that consist into writing a Matlab code that computes the possible intersections of two ellipses with a common focus. I'm writing a series of if-statements but I'm having trouble to set all conditions. I have to cover all possible cases, i.e., ellipses contained one inside the other (no intersections), the case of same line of apsides, all the cases of tangency condition, all cases of two intersection points and so on. Can you help me?
Here is my code, but it requires some adjustments:
clear all; close all; clc;
global tol twopi
a1=200000;
e1=0.6;
a2 =250000;
e2=0.4;
D_omega=0; % difference between pericenters
tol = 1.e-30; % tolerance for numeric zero
twopi = pi + pi;
% Solutions initialization
xiA = 0.0;
yiA = 0.0;
xiB = 0.0;
yiB = 0.0;
cosDomeg = cos(D_omega);
sinDomeg = sin(D_omega);
p1 = a1*(1.0-e1*e1); % semilatus rectum
p2 = a2*(1.0-e2*e2);
r_p1 = a1*(1.0-e1); % pericenter radius
r_a1 = a1*(1.0+e1); % apocenter radius
r_p2 = a2*(1.0-e2);
r_a2 = a2*(1.0+e2);
if(r_a1 < r_p2) % ellipse 1 contained inside ellipse 2
nSol = 0;
str = 'ellipse 1 contained inside ellipse 2';
elseif(r_a2 < r_p1) % ellipse 2 contained inside ellipse 1
nSol = 0;
str = 'ellipse 2 contained inside ellipse 1';
elseif(r_p1 < r_p2 && r_a1 < r_a2 && abs(D_omega) < tol) % two ellipses with same apse line (Delta theta = 0), ellipse 1 contained inside ellipse 2
nSol = 0;
elseif(r_p2 < r_p1 && r_a2 < r_a1 && abs(D_omega) < tol) % two ellipses with same apse line (Delta theta = 0), ellipse 2 contained inside ellipse 1
nSol = 0;
elseif(abs(p2-p1) < tol && abs(e2-e1) < tol && abs(D_omega) < tol) % two identical ellipses
% identical ellipses
nSol = -1;
else
a = p1-p2;
b = p1*e2*cosDomeg-p2*e1;
c = -p1*e2*sinDomeg;
% general solution
k1 = b*b + c*c;
k2 = a*b;
k3 = a*a - c*c;
discr = k2*k2 - k1*k3;
if(discr < tol) % they do not intersect !!!(qui non dovrebbe essere discr<tol ?)!!!!
nSol = 0;
str = 'delta negative: no intersections';
elseif(abs(discr) < tol && abs(sinDomeg) > tol)
% the discriminant is null and the two axes are neither parallel nor
% antiparallel
% TWO IDENTICAL SOLUTIONS (tangency point)
nSol = 1;
cth1 = -k2/k1;
sth1 = (a + b*cth1)/c;
cth2 = cth1;
sth2 = sth1;
str = 'delta = 0: two identical solutions!';
else
% TWO DISTINCT SOLUTIONS
nSol = 2;
if(abs(sinDomeg) > tol)
cth1 = (-k2 + sqrt(discr))/k1;
cth2 = (-k2 - sqrt(discr))/k1;
sth1 = (a + b*cth1)/c;
sth2 = (a + b*cth2)/c;
str = '2 solutions: general case';
else
if(abs(a/b - 1) < tol) % ellipses are parallel or anti-parallel and are tangent at theta = 180 (apocenter)
nSol = 1;
cth1 = -1.0;
cth2 = cth1;
sth1 = 0.0;
sth2 = 0.0;
str = '1 solution: tangency at apocenter';
elseif(abs(a/b + 1) < tol) % ellipses are parallel or anti-parallel and are tangent at theta = 0 (pericenter)
nSol = 1;
cth1 = 1.0;
cth2 = cth1;
sth1 = 0.0;
sth2 = 0.0;
str = '1 solution: tangency at pericenter';
else % special case: same apse line with 2 distinct solutions
cth1 = -a/b;
cth2 = cth1;
sth1 = (1 - cth1^2)^0.5;
sth2 = -sth1;
str = '2 solution: same apse line';
end
end
th1 = atan2(sth1,cth1);
th1 = mod(th1,twopi);
th2 = atan2(sth2,cth2);
th2 = mod(th2,twopi);
%Compute coordinates of intersection points in the perifocal r.f.
%of ellipse 1
[xiA,yiA] = Get_perifocal_coordinates(a1,e1,th1);
[xiB,yiB] = Get_perifocal_coordinates(a1,e1,th2);
end
end
P.S. My work activity: Study possbile intersections between asteroids and Keplerian orbits derived by propagation of Invariant Manifolds computed starting from Lyapunov orbits (by assuming to be outisde of the circle of influence of the Earth and not sphere since I'm considering the simpe 2D case). So, we are in heliocentric perspective, within the ecliptic plane (with a good approximation).
From the context of astrodynamics, I assume you're trying to solve the problem of finding the intersection points of two coplanar ellipses that share one focus. (As was pointed out in the comments, "confocal" usually refers to ellipses sharing both foci.)
In polar coordinates, two ellipses with one focus at the origin are given by $$ r_1(\theta) = \frac{c_1}{1 + \epsilon_1 \cos \theta} \qquad r_2(\theta) = \frac{c_2}{1 + \epsilon_2 \cos (\theta - \delta)} $$ where $\delta$ is the angle of the pericenter of ellipse #2 (assuming we have chosen our coordinates so that the pericenter of ellipse #1 is at $\theta = 0$), the $c_i$'s are the semi-latus rectums (semi-lati recta?), and the $\epsilon_i$'s are the eccentricities.
Demanding that these two quantities be equal, and defining $\rho = c_1/c_2$, leads to reduces to $$ 1 + \epsilon_1 \cos \theta = \rho \left( 1 + \epsilon_2 \cos (\theta - \delta) \right) \\ 1- \rho = \rho \epsilon_2 \cos(\theta - \delta) - \epsilon_1 \cos \theta $$ After an annoying amount of trig identities, the right-hand side of this equation can be transformed to $$ 1 - \rho = \sqrt{ \rho^2 \epsilon_2^2 + \epsilon_1^2 - 2 \rho \epsilon_1 \epsilon_2 \cos \delta} \cos(\theta - \alpha) $$ where $\alpha$ is defined by $$ \tan \alpha = \frac{\rho \epsilon_2 \sin \delta}{\rho \epsilon_2 \cos \delta - \epsilon_1}$$
We can see from this equation that: