import static java.lang.Math.*;
class SolverFailedExeption extends Exception {
public SolverFailedExeption(String errorMessage){
super(errorMessage);
}
}
class Main {
public static double solveKepler(double e, double M,
double epsFixedPoint, int NMAX, double epsEquation)
throws SolverFailedExeption{
double FNext = M,
F = FNext + 1000*epsFixedPoint, // make sure the initially F-FNext > eps
i = 0;
while(abs(FNext - F) > epsFixedPoint && i < NMAX){
F = FNext;
FNext = F - (e * sinh(F) - F - M) / (e * cosh(F) -1);
i++;
}
if(abs(M + F - e *sinh(F)) > epsEquation){ // verify the equation, not the fixed point
throw new SolverFailedExeption("Newton-Raphson method for Kepler equation failed");
}
return FNext;
}
public static double solveKepler(double e, double M)
throws SolverFailedExeption{
return Main.solveKepler(e, M, 5e-7, 10000, 5e-7);
}
public static void main(String[] args) {
double F0 = 0.04402263120230271,
e = 4.719,
M = -F0 + e * sinh(F0);
try{
double F = Main.solveKepler(e, M);
System.out.println("F = " + F + " diff = "+ abs(F-F0));
}
catch(SolverFailedExeption ex){
System.out.println(ex.getMessage());
}
}
}