【计算方法实验】实验2:牛顿迭代法

/**
用牛顿迭代法求方程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);
	}
		
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值