/* * <日付を入力> * 例題6.3 * f(x) = x^2 - 3 の根を2分法,ニュートン法,割線法を使って求める * プログラムを作れ.ただし,2分法,ニュートン法,割線法はそれぞれ * メソッドとし,メイン関数から呼び出してそれぞれの結果を表示 * すること.また,ε = 10E-5とする. */ public class NonlinearEquation { // フィールド static final double EPS = 1.0e-5; // メインメソッド public static void main(String [] args) { // 変数の定義 double ansBisection = 0.0; // 2分法の計算結果を入れる変数 // メソッドを使うためオブジェクトを生成 NonlinearEquation nonEqu = new NonlinearEquation(); // 2分法の計算 ansBisection = nonEqu.calculateBisection(1.0, 3.0); // 結果の表示 // 計算で求めた結果 System.out.println("根: " + Math.sqrt(3)); // 2分法で求めた結果 System.out.println("2分法: " + ansBisection); } // main // メソッドの定義 // 根を求める関数の定義 double defineFunction(double x) { return x*x-3; } // 2分法 double calculateBisection(double a, double b) { double c = 0.0; double fa; double fc; // 反復計算回数の計算 int n = (int)(Math.log((b-a)/EPS)/Math.log(2.) + 0.5); // 2分法 for (int i=0; i