Calculating semi-major axis of binary stars from velocity, position and mass [closed]

I'm trying to calculate the 'instantaneous' semi-major axis of a binary system with two equal (known) mass stars for an $N$-body simulation. I know their velocities and positions at a given time, but am unsure how best to calculate the semi-major axis. I tried using the vis-viva equation

$$v^2 ~=~ G(M_1 + M_2)\cdot(2/r - 1/a)$$

However I'm not sure if $r$ should be the distance between the two masses or the distance to the centre of mass and whether the $M$'s should be the masses or the sum and the reduced mass? Whatever I try the semi-major axis seems to oscillate over the orbit at around the correct value, but surely it should be constant? Any advice would be brilliant, thanks.

edit: Not allowed to answer my own question yet, but I'm an idiot and wasn't doing relative velocities, thanks guys.

-

closed as unclear what you're asking by Qmechanic♦Nov 7 '14 at 15:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

I find this question very confusing, too. Are you calculating a 2-body problem or an N-body problem for a higher value of N? If it is the latter, the trajectories won't be ellipses, so it makes no sense to talk about the semi-major axes too accurately because these parameters only make sense for ellipses e.g. for a 2-body problem. In the 2-body case, the Wikipedia article clearly answers all your questions. – Luboš Motl Apr 13 '12 at 14:45
@Mithra: if you figured out your own question, it'd be great if you write up an answer for it and post it once the system allows you to. I think it'll be 8 hours after you asked the question, or something like that. – David Z Apr 13 '12 at 15:09