4. 使用牛顿迭代法求方程的解:
x^3-2x-5=0
区间为[2,3]
这里的"^"表示乘方。 

package chapter4;

public class demo4 {
    public static void main(String[] args) {
        double x=2;
        for(int i=0;i<20;i++) {
            x=-f(x)/f1(x)+x;
        }
        System.out.println(x+"");
    }
    
    static double f(double x) {
        double ans;
        ans=Math.pow(x, 3)-2*x-5;
        return ans;
    }
    
    static double f1(double x) {
        double ans;
        ans=3*Math.pow(x, 2)-2;
        return ans;
    }
}

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄