计算

计算方法上机

牛顿迭代

代码

Newton.java

import java.math.BigDecimal;

//注意ep和ws及整数位数有关,这里整数位数为1位
//最后取有效位数 具体参数是小数位数,故取值也与整数位数有关
//迭代时,注意x1要赋值给x0    x0 = x1 ;

public class Newton {
    double  approroot ;

    double x0;
    double x1;
    String st1;


    int ws ;
    double ep ;
    public Newton(int ws , double ap) {
        this.ws = ws ;
        approroot = ap;
        x1 = x0 = approroot ;
        ep = (1/2)*Math.pow(10, (1 - ws)) ;

    }

    double f(double  x ){
        double x_1;
//        x_1 = (x * x * x -1)/3 ;
        x_1 = x - (x * x * x -3 * x -1)/(3*x*x -3) ;
        return x_1 ;
    }

    void calculate(){
        int i = 0;
        st1 = "x" + i +"  =  " + approroot ;
        do {
            x0 = x1 ;
            i ++ ;
            x1 = f(x0);  
            st1 = st1 + "\n" + "x" + i +"  =  " + x1 ;

        } while (Math.abs(x1 - x0) > ep );
        st1 = st1 + "\n" + "x" + i +"  =  " + x1 ;
        BigDecimal q = new BigDecimal(x1);
        x1 = q.setScale(ws - 1, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
Mainframe.java



    import java.awt.Button;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.TextArea;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    public class Mainframe extends Frame {

        TextArea t1 ,t11;
        TextArea t2, t22;
        TextArea t3,t33;
        TextArea t4,t44;
        TextArea t5;
        Button button;


        public static void main(String[] args) {
            new Mainframe().launchframe();

        }
        void init(){
            setLayout(null);

            button = new Button("click here to Start");
            button.setBounds(500,500,100,50);
            button.setForeground(Color.red);
            button.setBackground(Color.GREEN);

            t1 = new TextArea();
            t1.setBounds(210, 50, 200, 50);
            t11 = new TextArea();
            t11.setText("输入   x : ");
            t11.setBounds(10, 50, 200, 50);

            t2 = new TextArea();
            t2.setBounds(210, 100, 200, 50);
            t22 = new TextArea();
            t22.setText("输入有效数字位数(阿拉伯数字) : ");
            t22.setBounds(10, 100, 200, 50);

            t3 = new TextArea();
            t3.setBounds(110, 200, 400, 100);
            t33 = new TextArea();
            t33.setText("中间过程   : ");
            t33.setBounds(10, 200, 100, 100);

            t4 = new TextArea();
            t4.setBounds(110, 300, 400, 50);
            t44 = new TextArea();
            t44.setText("输出结果 : ");
            t44.setBounds(10, 300, 100, 50);

            t5 = new TextArea();
            t5.setText("方程为:     x * x * x - 3 * x  - 1 = 0");
            t5.setBounds(10, 400, 400, 50);
        }
        public void launchframe() {
            init();
            setBounds(100, 100, 800, 600);
            setBackground(Color.BLUE);
            setResizable(false);
            add(t1);
            add(t2);
            add(t3);
            add(t4);
            add(t11);
            add(t22);
            add(t33);
            add(t44);
            add(t5);
            add(button);

            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

            button.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {

                    Newton newton = new Newton(Integer.parseInt(t2.getText()), Double.parseDouble(t1.getText()));

                    newton.calculate();
                    t4.setText(newton.x1 + "");
                    t3.setText(newton.st1);
                }
            });

            setVisible(true);
        }

    }

牛顿迭代 x0 = x1 ;

setLayout(null); 可设置button位置

二分法(1)

代码

Dichotomy.java

package erfenfa;

public class Dichotomy {

    double a, b, ep;

    public Dichotomy(double a,double b, double ep) {
        this.a = a;
        this.b = b;
        this.ep = ep;
    }

    public static void main(String[] args) {
        //dic.Calculation();
//        System.out.println(dic.Calculation());

    }
    double f(double  x ){
        double y;
        y = x * x * x - 2 * x  - 5 ;
        return y ;
    }
    public  double Calculation(){
        double c = b - a;
        double a1 = a;
        double b1 = b;
        for(int i =0 ;c >= ep; i ++){
            double middle ;
            middle = (a1 + b1) / 2;
            if(f(a1)*f(middle) < 0){
                b1 = middle ;
                c = (b1 - a1)/2 ;   //注意要除2
            }
            else if (f(a1)*f(middle) > 0) {
                a1 = middle;
                c = (b1 - a1)/2 ;
            }
            else {
                c = 0;
            }


        }
        return (a1 + b1)/2;
    }

}
Mainframe.java

package erfenfa;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Mainframe extends Frame {

    TextArea t1 ,t11;
    TextArea t2, t22;
    TextArea t3,t33;
    TextArea t4,t44;
    TextArea t5;
    Button button;

    public static void main(String[] args) {
        new Mainframe().launchframe();

    }
    void init(){

        button = new Button("click here to Start");
        button.setForeground(Color.red);
        button.setBackground(Color.GREEN);
//        button.setFocusable(true);

        t1 = new TextArea();
        t1.setBounds(110, 50, 200, 50);
        t11 = new TextArea();
        t11.setText("Input a ");
        t11.setBounds(10, 50, 100, 50);

        t2 = new TextArea();
        t2.setBounds(110, 100, 200, 50);
        t22 = new TextArea();
        t22.setText("Input b ");
        t22.setBounds(10, 100, 100, 50);

        t3 = new TextArea();
        t3.setBounds(110, 150, 200, 50);
        t33 = new TextArea();
        t33.setText("Input ep ");
        t33.setBounds(10, 150, 100, 50);

        t4 = new TextArea();
        t4.setBounds(110, 200, 200, 50);
        t44 = new TextArea();
        t44.setText("output  ");
        t44.setBounds(10, 200, 100, 50);

        t5 = new TextArea();
        t5.setText("y = x * x * x - 2 * x  - 5 ");
        t5.setBounds(10, 300, 300, 50);
    }
    public void launchframe() {
        init();
        setBounds(100, 100, 800, 600);
//        setBackground(Color.green);
        setResizable(false);
    //    setLayout(new FlowLayout(FlowLayout.LEADING, 20 , 40));
        add(t1);
        add(t2);
        add(t3);
        add(t4);
        add(t11);
        add(t22);
        add(t33);
        add(t44);
        add(t5);
        add(button);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        button.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                Dichotomy dic = new Dichotomy(Double.parseDouble(t1.getText()), Double.parseDouble(t2.getText()), Double.parseDouble(t3.getText()));
                dic.Calculation();
                t4.setText(dic.Calculation() + "");
            }
        });

        setVisible(true);
    }


}

二分法(2)

代码

Dichotomy.java

package erfenfa2;

public class Dichotomy {

    double a, b, ep , c , result;
    double a1 ;
    double b1 ;
    int time = 0;

    public void Dichotomyset(double a,double b) {
        this.a = a;
        this.b = b;
        c = b - a;
        a1 = a;
        b1 = b;
    }

//    public static void main(String[] args) {
//        //dic.Calculation();
////        System.out.println(dic.Calculation());
//
//    }
    double f(double  x ){
        double y;
        y = x * x * x - 2 * x  - 5 ;
        return y ;
    }

    public  void Calculation(){

            double middle ;
            middle = (a1 + b1) / 2;
            if(f(a1)*f(middle) < 0){
                b1 = middle ;
                c = (b1 - a1) / 2 ;
            }
            else if (f(a1)*f(middle) > 0) {
                a1 = middle;
                c = (b1 - a1) / 2 ;
            }
            else {
                c = 0;
            }

        result =  (a1 + b1)/2;
        time ++;
    }

}

Mainframe.java
package erfenfa2;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Mainframe extends Frame {

    TextArea t1 ,t11;
    TextArea t2, t22;
    TextArea t3,t33;
    TextArea t4,t44;
    TextArea t6,t66;
    TextArea t5;
    Button button;

    boolean first ;
    Dichotomy dic;

    public static void main(String[] args) {
        new Mainframe().launchframe();

    }
    void init(){
        first = true ;
        dic = new Dichotomy();

        button = new Button("click here to Start");
        button.setForeground(Color.red);
        button.setBackground(Color.GREEN);

        t1 = new TextArea();
        t1.setBounds(110, 50, 200, 50);
        t11 = new TextArea();
        t11.setText("Input a : ");
        t11.setBounds(10, 50, 100, 50);

        t2 = new TextArea();
        t2.setBounds(110, 100, 200, 50);
        t22 = new TextArea();
        t22.setText("Input b : ");
        t22.setBounds(10, 100, 100, 50);

        t3 = new TextArea();
        t3.setBounds(110, 200, 200, 50);
        t33 = new TextArea();
        t33.setText("output ep : ");
        t33.setBounds(10, 200, 100, 50);

        t4 = new TextArea();
        t4.setBounds(110, 250, 200, 50);
        t44 = new TextArea();
        t44.setText("output result : ");
        t44.setBounds(10, 250, 100, 50);

        t6 = new TextArea();
        t6.setBounds(110, 300, 200, 50);
        t66 = new TextArea();
        t66.setText(" 二分法次数 : ");
        t66.setBounds(10, 300, 100, 50);

        t5 = new TextArea();
        t5.setText("y = x * x * x - 2 * x  - 5 ");
        t5.setBounds(10, 400, 300, 50);
    }
    public void launchframe() {
        init();
        setBounds(100, 100, 800, 600);
//        setBackground(Color.green);
        setResizable(false);
    //    setLayout(new FlowLayout(FlowLayout.LEADING, 20 , 40));
        add(t1);
        add(t2);
        add(t3);
        add(t4);
        add(t11);
        add(t22);
        add(t33);
        add(t44);
        add(t5);
        add(t6);
        add(t66);
        add(button);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        button.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                double aa = Double.parseDouble(t1.getText()) ;
                double bb = Double.parseDouble(t2.getText()) ;


                if (true == first ) {
                    dic.Dichotomyset(aa, bb);
                    first = false ; 
                } 

                dic.Calculation();
                t4.setText(dic.result + "");
                t3.setText(dic.c + "");

   t6.setText(dic.time + "");
            }
        });

        setVisible(true);
    }


}

ep 应该和(bn-an)/2 比较

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值