-6

I am writing codes to find a solution (E) to Kepler's equation:

E - e*sin(E) = M

and all angles are expressed in radians, M = 3.52821, e = 0.016714

and theses are the steps:

  1. First guess, put E = Eo = M
  2. Find the value of O = E - e*sin(E) - M
  3. If |O| <= 0.000006, go to step 6 If |O| > 0.000006, proceed with step 4
  4. Find delta_E = O/(1-e*sin(E))
  5. Take new value E1 = E - delta_E, go to step 2
  6. The present value of E is the solution, correct within 0.000006 of the true value

However, I don't know how to write codes (swift) of those steps, please help me.

Thanks to @NSNoob

I finally figure the solution out!!!

let M = 3.52821
var e = 0.016714
var E = M
var O = E - (e * sin(E)) - M

while (abs(O) > 0.000006) {
    var Delta_E = O / (1-(e*cos(E)))
    E = E - Delta_E
    O = E - (e * sin(E)) - M
}

print(E)

1 Answers1

0

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.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • Actually, I tried this in the playground, but I didn't get the value of E I want – JonahThePoo Jul 21 '16 at 08:27
  • I set my M value to "M = 3.52821" and run this, but it appears error – JonahThePoo Jul 21 '16 at 08:30
  • @JonahThePoo Then you should clarify the steps because as I pointed out they are not correct and I merely followed the steps you mentioned i.e. User gives value of mean anomaly which is equal to E0. E=E0. eps is 0.000006. E=e. and then you take absolute value of E - E*sin(E) - M. If value is <=eps, the solution is correct, otherwise you take E1 with E - delta_E where as Delta E is O/(1-e*sin(E)). u repeat the process until you get a value of E which falls in <= eps. Is that correct? As mentioned in the share article, there seem to be differnt formulas for E2,E3 and so on but u didnt mention it – NSNoob Jul 21 '16 at 08:33
  • @JonahThePoo Yes for 3.52821 it runs for more than 8000 times and after it the program stops, due to overload of execution I suppose – NSNoob Jul 21 '16 at 08:36
  • Yes, you're right! I apologize for my unclearness, I want to find the correct value of E that the O is <= eps. So how can I set the output of the codes to the value of E instead of the O? – JonahThePoo Jul 21 '16 at 08:38
  • @JonahThePoo I have edited the code to show value of E. It gives me E = 6.864552 for M = 3.094763 – NSNoob Jul 21 '16 at 08:43
  • Thanks again!!!! But my book says if M = 8.68841943037594, it will get E = 2.436915, but the answer I get from the playground is 4.431216... I have no idea... – JonahThePoo Jul 21 '16 at 08:57
  • Well then read the books again and see if you told me something wrong about the logic. According to the logic you described, 4.431216 is the answer because at E = 4.431216 and M = 8.688419, absolute value becomes 0.000003, so it becomes correct answer – NSNoob Jul 21 '16 at 09:07
  • Okay, Thank you very very much. I'm just a swift beginner. :) – JonahThePoo Jul 21 '16 at 09:10
  • I think the reason is that "e" actually doesn't equal the value of "E" – JonahThePoo Jul 21 '16 at 15:25
  • @JonahThePoo Yes that is it. In VBA example [here](http://www.stargazing.net/kepler/kepler.html), see that they named e variable ecc. So What is ecc? If you figure out, we shall get the code corrected in no time – NSNoob Jul 21 '16 at 15:28
  • @JonahThePoo is ecc given eccentricity? Which would be eps in our code? – NSNoob Jul 21 '16 at 15:54
  • In the case of M = 3.52821, e = 0.016714; in the case of M = 8.688419430375935, e = 0.01670747835792311 – JonahThePoo Jul 21 '16 at 16:09
  • @JonahThePoo So you mean value of e will be provided by user? – NSNoob Jul 21 '16 at 16:11
  • Thank you very much for your effort all day! I just added the solution below my question :) Thanks again:) – JonahThePoo Jul 21 '16 at 17:07