/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ /** * search a zero of a function f in an intervall x1..x2 * with intervall nesting */ public class IntervallNesting extends SearchZero { //-------------------- // // Constructors public IntervallNesting() { super(); } public IntervallNesting(double precision) { super(precision); } //-------------------- public double searchZero(double x1, double x2, RealFunction f) throws NoZeroFoundException { double y1 = f.at(x1); noOfCalls = 1; if ( Math.abs(y1) < precision ) return x1; double y2 = f.at(x2); noOfCalls = 2; if ( Math.abs(y2) < precision ) return x2; if ( (y1 >= 0) == (y2 >= 0) ) throw new NoZeroFoundException( "function values with equal signs: " + "f.at(" + x1 + ") = " + y1 + ", " + "f.at(" + x2 + ") = " + y2 ); while ( true ) { double x12 = (x2 + x1) / 2.0; double y12 = f.at(x12); ++noOfCalls; if ( Math.abs(y12) < precision ) { return x12; } if ( (y1 >= 0) == (y12 >= 0) ) { x1 = x12; y1 = y12; } else { x2 = x12; y2 = y12; } } } //-------------------- public String toString() { return "IntervallNesting with precision " + precision; } }