# Help with Cartesian State Vectors → Keplerian Orbit Elements C#

EDIT

I am currently attempting to try the standard Cartesian State Vectors to Keplerian Orbit Elements conversion but i am having problems getting my head around the formulas and the use of vectors. My premise for doing this is so i get past the simple calculations like velocity, period that i can do quite easily. The resources i have been using are This, This, This and finaly the textbook "Fundamentals of Astrodynamics" By Bate, Mueller, White

I have calculated by hand and then replicated in C# shown below. With some ballpark starting values:

$$\mathbf{v} = 7770 \ \hat{\mathbf{y}} \ \text{(m/s)}$$

$$\mathbf{r} = 6771000 \ \hat{\mathbf{x}} \ \text{(m)}$$

$$\mu = 4.0\text{E+}14 \ \text{(m³/s²)}$$

I have so far manage to successfully use the following formulas:

$$\mathbf{H} = \mathbf{r} \times \mathbf{v}$$

$$M = \frac{1}{2} v^2 - \frac{\mu}{r}$$

$$p = \frac{H^2}{\mu}$$

$$a = \frac{-\mu}{2M}$$

$$e = \sqrt{1 - \frac{p}{a}},$$

where $\mathbf{H}$ is the angular momentum vector, $M$ is the total energy (inertial frame), p is the semi-latus rectum, e is eccentricity, and $\mathbf{r}$ and $\mathbf{v}$ (and $r$ and $v$) are the position and velocity vectors (and magnitudes).

However i am having problems trying to follow up with:

$$\hat n = \hat K \times \mathbf{h}$$ (as seen in PG61 of the Book mentioned above)

And correctly using: $$\vec e = \frac{\vec v \times \vec h}{\mu} \ - \frac{\vec r}{||r||}$$

IN the PDF it is shown as:

$$\hat n = \mathbf{(0,0,1)^T} \times \mathbf{h} = (-hy,hx,0)^T$$

I am unsure what to plug in or how to use these types of formulas (or i even have the information)

I have included some of my C# code so you can see my line of thinking.progress.

double V = 7770;
double R = 6771000;
double Mu = 4e+14;

Vector velocity = new Vector(0, V);
Vector Radius = new Vector(R, 0);

double H = Vector.CrossProduct(Radius, velocity);// Angular Momentum
double M = (Math.Pow(V, 2) / 2) - (Mu / R);//Mechanical Energy
double p = Math.Pow(H, 2) / Mu;// Semi-Latus Recum

double a = -Mu / (2 * M);//Semi-Major Axis? Comes out only 40km less than
SLR and no where near the correct SMA.
double test = Vector.Multiply(Radius, velocity);//test dot product.

//double e = Math.Sqrt(1 - p / a);//Eccentricty ? No where near correct
vaule.
//double e2 = (Math.Pow(V, 2) - Mu / R) * Radius - test * velocity /
Mu);//Eccentricty ?


I have been fiddling with the code and reading material for around two months and i have gotten some progress but my lack of experience with calculating Vectors is letting me down. Help with examples i can reverse engineer or get a better understanding of what i need to do to get going.

• I've added the MathJax formatting for equations that seem to be the same as what your C# code block shows, without attention to if they are correct or not. This will make it easier to see what you are doing for those who don't "think in code".
– uhoh
Feb 25, 2018 at 7:37
• This is a really great edit and it is much clearer what you already know and have tried, and what you'd like to do next. Well done!
– uhoh
Mar 1, 2018 at 6:43
• @notovny there's been an edit to the question; you may want to take another crack at it now.
– uhoh
Mar 1, 2018 at 9:37
• One problem is that your code is using 2D vectors, which will only get you so far in the process. For example, $\hat{n}$ is inherently a 3D construct, as it points to the node of the orbit. Mar 1, 2018 at 15:07
• @Crhis so if i were to use a Vector3D this would be better suited? What would i use for K? Mar 2, 2018 at 3:16

I will post a tentative answer because comments don't allow enough room. I always use the vis-viva equation for things like this. If you are just calculating energies or position and velocity at a fixed point (especially the apses) it's a little faster because it only uses scalars:

$$v^2 = \mu \left(\frac{2}{r} - \frac{1}{a} \right)$$

Flip that around to get:

$$a = \frac{1}{ (2/r - v^2/\mu) }$$

With your staring values, I get a value for $a$ of about 6923046 m, which is larger than your starting radius, so you've started at periapsis. Setting $r_{peri}$ to 6771000 m and using $e = 1 - r_{peri}/a$ I get an eccentricity of about 0.02196.

By definition you've started at one of the apses, since $r$ and $v$ are perpendicular. The task is only to figure out which one.

• Thanks for the reply and sorry for the massive delay. I can already do as you described i was more looking at calculating the orbital elements from a position and velocity vector, but am getting stumped on how to apply some of the formulas. Feb 28, 2018 at 6:16
• @Nzjeux I see. Asking how to implement formulas in a particular programming language is really a question about programming and so pushes the envelope of being on-topic in Space Exploration SE. I'm still not really sure exactly what your question is. I believe that there are other answers here that give a step-by-step explanation of converting an initial state vector $(\mathbf{r}, \mathbf{v})$ to Keplerian elements, I'll try to look for them and you should too. Can you consider shortening your question and explaining (in the question) more clearly exactly what a helpful answer would be?
– uhoh
Feb 28, 2018 at 6:30
• I have edited the question to better suit what i was asking, as well as done some more reading on the material. Been so busy at work haven't had time to get into it. Also like to thank you for you patience. Mar 1, 2018 at 6:22