package pr2Calc;


import javafx.geometry.Pos;

import java.util.function.Function;

public class NonlinearEquation {

    public static final double EPSILON = 0.001;//I
    public static final int MAXIMUM_IT = 100;//JԂ񐔂̍ől
    private static final double NEGATIVE_MAX = 0.0;
    private static final double POSITIVE_MAX = 3.0;
    private double initialValue_;//x_0i[
    private double alfa_ = 3.0;//Œ
    private double answer_;//i[
    private int iteration_;//݂̔񐔂i[


    // RXgN^(Œpӂj
    public NonlinearEquation(double initialValue, double alfa) {
        // 
        initialValue_ = initialValue;
        alfa_ = alfa;

    }

    //fx=sin(x+alfa)/(x+a)
    //1.5
    private boolean _solveNLEByBlsectlonMethod() {
        double value;
        double xMid = 0.0, pastMid = 0.0;
        double x0 = NEGATIVE_MAX, x1 = POSITIVE_MAX;
        iteration_ = 0;

        Function<Double, Double> func = (x) -> (Math.sin(x + alfa_) / (x + alfa_)); //_
        while (true) {
            if (++iteration_ == MAXIMUM_IT) break;  //ɒBꍇ
            xMid = (x1 + x0) / 2;
            value = func.apply(xMid);//Kp
            System.out.println("xMid = " + xMid + ", f(xMid) = " + value + ", xPastMid = " + pastMid);

            if (Math.abs(xMid - pastMid) < EPSILON || value == 0) {
                answer_ = xMid;
                System.out.printf("x = " + answer_ + " at iteration " + (iteration_ - 1) + ".");
                return true;
            }
            pastMid = xMid;
            if ((value < 0) ^ (func.apply(x0) < 0)) { //Ⴂ
                x1 = xMid;
            } else {
                x0 = xMid;
            }
        }
        return false;

//
//        value = initialValue_;
//        System.out.printf("xMid = %f, f(xMid)=%f, xPastMid =%f\n", (NEGATIVE_MAX + POSITIVE_MAX) / 2, Math.sin(value + alfa_) / (value + alfa_), pastMid);
//        xMid = (NEGATIVE_MAX + POSITIVE_MAX) / 2;
//        while (Math.abs(Math.sin(value + alfa_) / (value + alfa_) - value) >= EPSILON) {
//            value = Math.sin(value + alfa_) / (value + alfa_);//xvZ
//            if (xMid < pastMid) {//Ԓl菬
//                max = xMid;
//            } else if (xMid > pastMid) {
//                min = xMid;
//            }
//            pastMid = xMid;
//            xMid = (min + max) / 2;
//            System.out.printf("xMid = %f, f(xMid)=%f, xPastMid =%f\n", xMid, Math.sin(value + alfa_) / (value + alfa_), pastMid);
//            if (count > 100) return false;
//            count++;
//
//        }
//        return true;
    }


    private boolean _solveNLEByLinearIteration() {
        double value;      // x_k ɑΉ
        int count = 1;//Œ͍s
        value = initialValue_;
        System.out.println("value = " + String.format("%1$4.15f ", Math.sqrt(10 + alfa_ + value)) + " , pastValue = " + value);
        while (Math.abs(Math.sqrt(10 + alfa_ + value) - value) >= EPSILON) {
            if (count > MAXIMUM_IT) return false;
            value = Math.sqrt(10 + alfa_ + value);
            System.out.print("value = " + String.format("%1$4.15f ", Math.sqrt(10 + alfa_ + value)));
            System.out.println(" , pastValue = " + String.format("%1$4.15f ", value));
            count++;
        }
        answer_ = Math.sqrt(10 + alfa_ + value);
        iteration_ = count;
        System.out.println("X = " + answer_ + " at interation " + iteration_ + ".");
        return true;
    }


    public static void main(String[] args) {
//        NonlinearEquation eqn = new NonlinearEquation();
//        eqn.initialValue_ = 4.0;
//        eqn._alfa = 5.0;
//        if (!eqn._solveNLEByLinearIteration()) {
//            System.out.println("Ȃ");
//        }
        NonlinearEquation eqn = new NonlinearEquation(3, 0);
        if (!eqn._solveNLEByBlsectlonMethod()) {
            System.out.println("Ȃ");
        }
        // ݒ
        // ߎꂽߎڂ̌JԂŉꂽ\
        // Ȃ΁AȂƂ\
    }
}
@@ -15,36 +15,8 @@
 c;%0A%0A
-%0Aimport javafx.geometry.Pos;
 %0A%0Aim
@@ -1476,16 +1476,18 @@
 lue %3C 0)
+/*
  %5E (func
@@ -1501,16 +1501,18 @@
 x0) %3C 0)
+*/
 ) %7B //%E1%82%A2%0A
@@ -1637,880 +1637,8 @@
 e;%0A%0A
-//%0A//        value = initialValue_;%0A//        System.out.printf(%22xMid = %25f, f(xMid)=%25f, xPastMid =%25f%5Cn%22, (NEGATIVE_MAX + POSITIVE_MAX) / 2, Math.sin(value + alfa_) / (value + alfa_), pastMid);%0A//        xMid = (NEGATIVE_MAX + POSITIVE_MAX) / 2;%0A//        while (Math.abs(Math.sin(value + alfa_) / (value + alfa_) - value) %3E= EPSILON) %7B%0A//            value = Math.sin(value + alfa_) / (value + alfa_);//xvZ%0A//            if (xMid %3C pastMid) %7B//%D4%92l%E8%8F%AC%0A//                max = xMid;%0A//            %7D else if (xMid %3E pastMid) %7B%0A//                min = xMid;%0A//            %7D%0A//            pastMid = xMid;%0A//            xMid = (min + max) / 2;%0A//            System.out.printf(%22xMid = %25f, f(xMid)=%25f, xPastMid =%25f%5Cn%22, xMid, Math.sin(value + alfa_) / (value + alfa_), pastMid);%0A//            if (count %3E 100) return false;%0A//            count++;%0A//%0A//        %7D%0A//        return true;%0A
     
@@ -2918,24 +2918,24 @@
 ntln(%22%C2%82%C8%82%22);%0A
+
         %7D%0A  
@@ -2936,62 +2936,8 @@
   %7D%0A
-        // %DD%92%0A        // %DF%8E%EA%82%BD%DF%8E%DA%82%CC%8CJ%D4%82%C5%89%EA%82%BD%5C%0A        // %C8%82%CE%81A%C2%82%C8%82%C6%82%5C%0A
     

code creanup