// Iteration.java MM 2012 /** * Nullstelleniteration * mittels Newton und Regula falsi. */ public class Iteration { /* ------------------------------------------------- */ // Attribut /** * Aktuelle Iterationstiefe. */ private int anzahl = 0; /* ------------------------------------------------- */ // get-Methode /** * Liest Iterationstiefe. * @return anzahl */ public int getAnzahl() { return anzahl; } /* ------------------------------------------------- */ // Verfahren /** * Nullstellenbestimmung mittels Newton. * @param fkt differenzierbare Funktion * @param x0 Naeherung * @param eps Genauigkeit * @param max maximale Iterationstiefe * @return Naeherung einer Nullstelle */ public double newton( DifferenzierbareFunktion fkt, double x0, double eps, int max) { anzahl = 0; double y0 = fkt.wert( x0), y1; do { // System.out.println( anzahl + ": " + x0); y1 = fkt.wertErsteAbleitung( x0); x0 -= y0 / y1; y0 = fkt.wert( x0); anzahl++; } while(( Math.abs( y0) >= eps)&& ( anzahl < max)); return x0; } /** * Nullstellenbestimmung mittels Ragula falsi. * @param fkt Funktion * @param x0 1. Naeherung * @param x1 2. Naeherung * @param eps Genauigkeit * @param max maximale Iterationstiefe * @return Naeherung einer Nullstelle, * falls Iterationsvoraussetzung korrekt. */ public double regulaFalsi( Funktion fkt, double x0, double x1, double eps, int max) { double y0 = fkt.wert( x0); // Funktionswerte double y1 = fkt.wert( x1); anzahl = 0; do { // System.out.print ( " x0: " + x0); // System.out.println( "\tx1: " + x1); double x = x0 - ( x1 - x0) / ( y1 - y0) * y0; double y = fkt.wert( x); if( Math.signum( y) == Math.signum( y1)) { x1 = x0; y1 = y0; } x0 = x; y0 = y; anzahl++; } while(( Math.abs( y0) >= eps)&& ( anzahl < max)); return x0; } }