⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch7_1_1.java

📁 Java环境下实现的数值分析程序
💻 JAVA
字号:
class ch7_1_1{
    static double f1(double x,double y){
		return Math.pow(x,2)+Math.pow(y,2);
	}
	static double f2(double x,double y){
		return Math.pow(x,2)+Math.pow(y,2);
	}
	static void ModEuler(double x0,double y0,double xn,int n){
		double yp=0.0;
		double yc=0.0;
		double x=x0;
		double y=y0;
		double h=(xn-x0)/n;
		System.out.println("x[0]="+x+" "+"y[0]="+y);
		for(int i=1;i<=n;i++){
			yp=y+h*f1(x,y);
			x=x0+i*(xn-x0)/n;
			yc=y+h*f1(x,yp);
			y=(yp+yc)/2.0;
			System.out.println("x["+i+"]="+x+"  "+"y["+i+"]="+y);
	    }
	}
	static void Runge_Kutta(double x0,double y0,double xn,int n){
		double K1=0.0;
		double K2=0.0;
		double K3=0.0;
		double K4=0.0;
		double x=x0;
		double y=y0;
		double h=(xn-x0)/n;
		System.out.println("x[0]="+x+" "+"y[0]="+y);
		for(int i=1;i<=n;i++){
			K1=f2(x,y);
			K2=f2(x+((xn-x0)/n)/2.0,y+((xn-x0)/n)*K1/2.0);
			K3=f2(x+((xn-x0)/n)/2.0,y+((xn-x0)/n)*K2/2.0);
			K4=f2(x+((xn-x0)/n),y+((xn-x0)/n)*K3);
			y=y+h*(K1+2*K2+2*K3+K4)/6.0;
			x=x0+i*(xn-x0)/n;
			System.out.println("x["+i+"]="+x+" "+"y["+i+"]="+y);
		}
	}
	public static void main(String []args){
		double xn=1.0;
		double x0=0.0;
		double y0=0.0;
		int n=10;
		System.out.println("用改进欧拉法:");
		ModEuler(x0,y0,xn,n);
		System.out.println("*************************");
		
		xn=1.0;
		x0=0.0;
		y0=0.0;
		n=10;
		System.out.println("用四阶龙格-库塔公式:");
		Runge_Kutta(x0,y0,xn,n);
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -