[Solved] find a solution to Kepler’s equation (iOS) (swift)


First of all some corrections in your question which you apparently forgot to mention and I had to look for here.

E0 = M

E = E1 on next iteration if solution not found

Regarding technical terms, E here is called Eccentric Anomaly and M is called mean Anomaly. Where as eps is precision diameter. Also, E=e according to the shared article

Also in swift, we use camel case naming convention for variables and constants but here I have tried to use your names so that you could understand the code.

Now back to business, Following methods will do it for you using recursion:

func solveKeplersEquationForParamas(M:Double)->Void{

    let E:Double = M
    let eps:Double = 0.000006
    let AbsoluteValueOfO:Double = getAbsoluteValueOfO(E, M: M,eps: eps)
    print(NSString(format:"Answer is:%f", AbsoluteValueOfO))


}

func getAbsoluteValueOfO(E:Double,M:Double,eps:Double) -> Double {
    var SinOFE: Double = Double(sin(E))
    SinOFE = E*SinOFE
    var E1 = E;
    let O = E - SinOFE - M

    var AbsoluteValueOfO = fabs(O)
    if AbsoluteValueOfO > eps {
        print("Solution not found. go to step 4");
        let denom = 1-E*sin(E)
        let absDenom = fabs(denom)
        if absDenom>0{
            let deltaE = O/denom
            E1 = E-deltaE
            AbsoluteValueOfO = getAbsoluteValueOfO(E1, M: M, eps: eps)
        }
        else{
            print("Denom became 0, Can't divide by zero Denom is:%f",denom);
            return AbsoluteValueOfO
        }

    }
    else if AbsoluteValueOfO < eps || AbsoluteValueOfO == eps{
        print("Solution found. Returning the value");
        print(NSString(format:"Value of E is:%f", E1))
    }
    return AbsoluteValueOfO
}

Running this in playground like:

solveKeplersEquationForParamas(3.094763)

Playground output:

enter image description here

NOTE: This is the Swift solution to the steps you have mentioned. Responsibility for any error in steps lies on you.

15

solved find a solution to Kepler’s equation (iOS) (swift)