计算

来源:互联网 发布:淘宝商城半身裙 编辑:程序博客网 时间:2024/04/28 11:17

计算方法上机

牛顿迭代

代码

Newton.javaimport 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.javapackage 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.javapackage 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.javapackage 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.javapackage 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
原创粉丝点击