数值方法双点割线法解非线性方程

计算x^3-3*x+1=0的根,取x0=0.5,x1=0.2``,精确到6位小数

public class Secant {
    final static double WUCHA=0.0000005;
    static double xiaohanshu(double x) {
        var v = x * x * x - 3.00000 * x + 1.00000;
        return v;
    }
    static double hanshu(double x1, double x0) {

        return x1 - (xiaohanshu(x1) / (xiaohanshu(x1) - xiaohanshu(x0))) * (x1 - x0);
    }
    public static void main(String[] args) {
        int count=0;
        double x1=0.2;
        double x0=0.5;
        while(Math.abs((x1-x0))>WUCHA){
            double temp=x1;
            x1=hanshu(x1,x0);
            x0=temp;
            count++;

        }
        System.out.println("结果:"+x1);
        System.out.println("迭代次数:"+count);
    }
}

在这里插入图片描述

发布了30 篇原创文章 · 获赞 9 · 访问量 1328

猜你喜欢

转载自blog.csdn.net/weixin_43625164/article/details/104628701