package pr2Calc;



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;

    }


    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("Ȃ");
        }
    }
}
@@ -12,17 +12,16 @@
 Calc;%0A%0A%0A
-%0A
 import j
@@ -287,17 +287,17 @@
 E_MAX = 
-3
+5
 .0;%0A    
@@ -360,17 +360,17 @@
 alfa_ = 
-3
+1
 .0;//%C5%92%0A 
@@ -594,23 +594,1089 @@
  alfa;%0A%0A
-
     %7D%0A%0A
+    private boolean _solveNLEByRegulaFalsi() %7B%0A        double value = 0.0;%0A        double xMid = 0.0, pastMid = 0.0;%0A        double x0 = NEGATIVE_MAX, x1 = POSITIVE_MAX;%0A        iteration_ = 0;%0A%0A        Function%3CDouble, Double%3E func = (x) -%3E (x + alfa_ == 0) ? 1.0 : (Math.sin(x + alfa_) / (x + alfa_));%0A        while (true) %7B%0A            if (++iteration_ == MAXIMUM_IT) break;  //%C9%92B%EA%8D%87%0A            xMid = (x0 * func.apply(x1) - x1 * func.apply(x0)) / (func.apply(x1) - func.apply(x0));%0A            value = func.apply(xMid);//Kp%0A            System.out.println(%22xNext = %22 + xMid + %22, f(xNext) = %22 + value + %22, xPastNext = %22 + pastMid);%0A%0A            if (Math.abs(xMid - pastMid) %3C EPSILON %7C%7C value == 0) %7B%0A                answer_ = xMid;%0A                System.out.printf(%22x = %22 + answer_ + %22 at iteration %22 + (iteration_ - 1) + %22.%22);%0A                return true;%0A            %7D%0A            pastMid = xMid;%0A            if ((value %3C 0)) %7B //%E1%82%A2%0A                x1 = xMid;%0A            %7D else %7B%0A                x0 = xMid;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A%0A
     //fx
@@ -3928,31 +3928,27 @@
 lveNLEBy
-BlsectlonMethod
+RegulaFalsi
 ()) %7B%0A  

kadai10