计算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);
}
}