Private Sub btnComputeL1_Click()
Dim Pi As Double, G As Double, Ms As Double, Mt As Double, a1 As Double, a2 As Double
Dim accs As Double, acct As Double, accx As Double, acctot As Double, accnet As Double
Dim hs As Double, v As Double
Pi = Atn(1) * 4 'define Pi
G = 6.6727004301751E-11 'define gravitational constant
Ms = Val(txtM1.Text) ' Get mass of primary body from the GUI
Mt = Val(txtM2.Text) ' Get mass of secondary body from the GUI
a1 = Val(txtSMA.Text) ' Get semi-major axis of secondary body from the GUI
p1 = 2 * Pi * Sqr(a1 ^ 3 / (G * (Ms + Mt))) 'period of Earth
hs = a1 * (Mt / (3 * Ms)) ^ (1 / 3) 'compute the hill sphere as a starting point
lb = hs * 0.98: ub = hs * 1.02 'as an upper bound and a lower bound, choose 98% and 102% of the hill sphere
While Abs(ub - lb) > 1 'While |ub - lb| is less than 1 gives a precision of 1 meter
a2 = (ub + lb) / 2 'semi-major axis of the L1 particle with respect to the secondary body
accs = G * Ms / (a1 - a2) ^ 2 'acceleration by the primary body on the L1 satellite
acct = G * Mt / (a2) ^ 2 'acceleration by the secondary body on the L1 satellite
'accx = G * Mt / (2 * a1) ^ 2 'acceleration by an Earth-mass object in Earth's L3 point on the L1 satellite
acctot = accs - acct + accx 'compute combined acceleration
accnet = acctot / accs 'multiplier to convert primary mass into simulated primary mass
p2 = 2 * Pi * Sqr((a1 - a2) ^ 3 / (G * Ms * accnet)) ' period of satellite around the simulated primary mass
If (p1 < p2) Then 'set new upper or lower bound
lb = a2
Else
ub = a2
End If
Wend
txtL1.Text = a2 'output the answer to the GUI
v = 2 * Pi * (a1 - a2) / p1 'compute the orbital velocity of the L1 particle
txtVelocity.Text = v 'output the orbital velocity to the GUI
End Sub