Beam Envelope

Suppose we have computed the beam moment matrix $\Sigma_{ij}(s)$ around the ring.
Consider the normalized spatial distribution in $(x,y)$:

(1)
\begin{align} f(x,y) = \frac{\sqrt{-b^2+4 a c}}{2 \pi }\ e^{-\frac{1}{2}(a x^2+b x y+c y^2)} \end{align}

If we define the matrix

(2)
\begin{align} A=\pmatrix{a & \frac{b}{2}\cr \frac{b}{2} & c} \end{align}

the exponential could be written as

(3)
\begin{align} e^{-\frac{1}{2}\vec X^T A\vec X} \end{align}

with $\vec X = \pmatrix{x\cr y}$. Now, we have (see here for a reference)

(4)
\begin{align} \Sigma_{\vec X} = A^{-1} \end{align}

with

(5)
\begin{align} \Sigma_{\vec X} = \pmatrix{<x^2> & <xy> \cr <xy> & <y^2>} \end{align}

We can thus derive that

(6)
\begin{eqnarray} a&=&\frac{<y^2>}{<x^2><y^2>-<xy>^2}\cr b&=&\frac{-2<xy>}{<x^2><y^2>-<xy>^2}\cr c&=&\frac{<x^2>}{<x^2><y^2>-<xy>^2} \end{eqnarray}

We have a family of ellipses, now, and we'd like to choose one to represent the electron beam.
Perhaps we may use

(7)
$$ax^2 +bxy+cy^2 =1$$

where the particle density has dropped by a factor of $\frac{1}{e}$

This is an ellipse with a rotation angle $\theta$ satisfying

(8)
\begin{align} \tan2\theta = \frac{b}{d-a} \end{align}

The equation for the ellipse may also be written in terms of the moments as

(9)
$$<y^2>x^2 -2<xy>xy+<x^2>y^2 ={<x^2><y^2>-<xy>^2}$$

First we find the positions we want to plot at:
dr278=findcells(ring_err,'FamName','DR_278….')
Then we can use the function
plotContours(ring,dr278(1:2:end))