Java——xe^x-1=0(Newton/NewNewton/DownNewton/CutLine)解非线性方程

25 篇文章 2 订阅
10 篇文章 0 订阅

在这里插入图片描述

f(x)=xex -1=0

x0=0.5附近的根,迭代终止条件|xk+1-xk|<10^6

(点击此处)下载源代码

import java.text.DecimalFormat;
public static void main(String[] args) {
	Newton(0.5);
	newNewton(0.5);
	downNewton(0.5);
	cutLine(0.5,0.6);}

public static void Newton(double x0){
	System.out.println("Newton:");
	DecimalFormat df = new DecimalFormat("#####0.00000000000");
	double xn=x0;
	double xnp;
	double c=0;
	int i=0;
	System.out.println("x" + (i++) + "=" + df.format(x0));
	while(true){
		xnp=xn-((xn*(Math.pow(Math.E,xn))-1)/((xn+1)*(Math.pow(Math.E,xn))));
		if((xnp-xn)>0){
			c=(xnp-xn);
		}else{
			c=-(xnp-xn);
		}
		System.out.println("x" + (i++) + "=" + df.format(xnp));
		if(c<Math.pow(10, -6))
			break;
		xn=xnp;
		xnp=0;
	}
	System.out.println("差值=" + df.format(c) + " < 10^(-6)");
	System.out.println("___________________________________");
}

public static void newNewton(double x0){
	System.out.println("NewNewton:");
	DecimalFormat df = new DecimalFormat("#####0.00000000000");
	double xn=x0;
	double xnp;
	double M=((xn+1)*(Math.pow(Math.E,xn)));
	double c=0;
	int i=0;
	System.out.println("x" + (i++) + "=" + df.format(x0));
	while(true){
		xnp=xn-((xn*(Math.pow(Math.E,xn))-1)/M);
		if((xnp-xn)>0){
			c=(xnp-xn);
		}else{
			c=-(xnp-xn);
		}
		System.out.println("x" + (i++) + "=" + df.format(xnp));
		if(c<Math.pow(10, -6))
			break;
		xn=xnp;
		xnp=0;
	}
	System.out.println("差值=" + df.format(c) + " < 10^(-6)");
	System.out.println("___________________________________");
}

public static void downNewton(double x0){
	System.out.println("DownNewton:");
	DecimalFormat df = new DecimalFormat("#####0.00000000000");
	double xn=x0;
	double xnp;
	double c=0;
	int T=1;
	double xk1;
	double xk;
	int i=0;
	System.out.println("x" + (i++) + "=" + df.format(x0));
	while(true){
		xnp=xn-(1/T)*(((xn*(Math.pow(Math.E,xn))-1)/((xn+1)*(Math.pow(Math.E,xn)))));
		if((xn*(Math.pow(Math.E,xn))-1)>0){
			xk=(xn*(Math.pow(Math.E,xn))-1);
		}else{
			xk=-(xn*(Math.pow(Math.E,xn))-1);
		}
		if((xnp*(Math.pow(Math.E,xnp))-1)>0){
			xk1=(xnp*(Math.pow(Math.E,xnp))-1);
		}else{
			xk1=-(xnp*(Math.pow(Math.E,xnp))-1);
		}
		
		if(xk1>xk){
			T=T*2;	
		}else{
			System.out.println("x" + (i++) + "=" + df.format(xnp));
			System.out.println("下山因子为:"+T+" //~// 其中|F(X(k+1))|="+df.format(xk1)+" //~// 其中|F(Xk)|="+df.format(xk));
			if((xnp-xn)>0){
				c=(xnp-xn);
			}else{
				c=-(xnp-xn);
			}
			if(c<Math.pow(10, -6))
				break;
			xn=xnp;
			xnp=0;
		}	
	}
	System.out.println("差值=" + df.format(c) + " < 10^(-6)");
	System.out.println("___________________________________");
}

public static void cutLine(double x0,double x1){
	System.out.println("CutLine:");
	DecimalFormat df = new DecimalFormat("#####0.00000000000");
	double xn=x0;
	double xn2=x1;
	double xnp;
	double c=0;
	int i=0;
	System.out.println("x" + (i++) + "=" + df.format(x0));
	System.out.println("x" + (i++) + "=" + df.format(x1));
	while(true){
		xnp=xn2-((xn2-xn)*((xn2*(Math.pow(Math.E,xn2))-1)/((xn2*(Math.pow(Math.E,xn2))-1)-(xn*(Math.pow(Math.E,xn))-1))));
		if((xnp-xn2)>0){
			c=(xnp-xn2);
		}else{
			c=-(xnp-xn2);
		}
		System.out.println("x" + (i++) + "=" + df.format(xnp));
		if(c<Math.pow(10, -6))
			break;
		xn=xn2;
		xn2=xnp;
		xnp=0;
	}
	System.out.println("差值=" + df.format(c) + " < 10^(-6)");
	System.out.println("___________________________________");
}

Newton Java

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GodOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值