/**
用牛顿迭代法求方程f(x)=e^x-3-x=0,在区间(1,2)内的根,输出迭代结果并统计所用迭代次数,取 误差=10^(-5) ,x0=2
*/
public class Newton{
public static void main(String[] args){
Iteration it = new Iteration();
it.setX0(2);
it.setAC(0.00001);
double temp = 0;
it.show();
while(!it.check()){
it.next();
it.show();
}
}
}
class Iteration{
double cu;
boolean isFinish;
double ac; //精度
int count;
double err = -1;
public double next(){
count++;
double temp = Math.pow(Math.E, cu);
temp = (temp - 3 - cu)/(temp - 1);
cu = cu - temp;
err = Math.abs(temp);
if(err <= ac) isFinish = true;
return cu;
}
public boolean check(){
return isFinish;
}
public void setX0(double x0){
cu = x0;
}
public void setAC(double ac){
this.ac = ac;
}
public double getCU(){
return cu;
}
public void show(){
System.out.print(cu);
System.out.print("\t");
System.out.print(count);
System.out.print("\t");
System.out.println(err);
}
}
【计算方法实验】实验2:牛顿迭代法
最新推荐文章于 2023-12-24 17:43:17 发布