/*
 * "Grundkurs Programmieren in Java - Band 1 (3. Auflage, 2006)I"
 * 2001-2006, Carl Hanser Verlag
 * Loesungsvorschlag zu Aufgabe 7.13 (Version 3.0)
 * (c) 2001-2006 D. Ratz, J. Scheffler, D. Seese, J. Wiesenberger
 *
 */

import Prog1Tools.IOTools;

public class Echolot {

  public static double g = 9.81;

  // a)
  public static double f (double x) {
    return Math.pow(x,2) - g*(x - 1 + Math.exp(-x));
  }

  // b)
  public static double fs (double x) {
    return 2*x - g*(1 - Math.exp(-x));
  }

  // c)
  public static double newton (double x0, double eps) {

    double xAlt, xNeu = x0;
    int k = 0;

    do {
      k++;
      xAlt = xNeu;
      xNeu = xAlt - f(xAlt) / fs(xAlt);
    } while ((Math.abs(xNeu-xAlt) > eps*Math.abs(xNeu)) && (k < 50));

    if (Math.abs(xNeu-xAlt) > eps*Math.abs(xNeu))
      return -1;
    else
      return xNeu;
  }


  // d)
  public static double tiefe(double t, double x) {
    return g*(x*t+Math.exp(-x*t)-1)/Math.pow(x,2);
  }

  // e)
  public static void main (String[] args) {
    double x0  = IOTools.readDouble("Startwert x0 = ");
    double eps = IOTools.readDouble("Abbruchkonstante eps = ");
    double x = newton(x0,eps);
    if (x > 0)
      System.out.println("Tiefe des Meeres: " + tiefe(10,x) + " Meter");
    else
      System.out.println("Newton-Verfahren konvergiert nicht!");
  }
}

/*
Beispielablauf:

Startwert x0 = 10
Abbruchkonstante eps = 1e-7
Tiefe des Meeres: 11.17162642682642 Meter

Startwert x0 = 1
Abbruchkonstante eps = 1e-10
Newton-Verfahren konvergiert nicht!
*/

