📄 ordinarydifferentialequationtest.java
字号:
package numericalAnalysis.ode;
import java.util.Scanner;
import numericalAnalysis.function.Function;
public class OrdinaryDifferentialEquationTest {// -------------注意!!!!!!y'=f(x,y)的f(x,y)在Function接口写入-----------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
OrdinaryDifferentialEquation ode;
MyFunction function;//被积函数
String anotherChoice;// 新的方法
String another;// 新建一个常微分方程初值问题的判断
System.out.println("\n----------------------------本程序求解常微分方程初值问题.注意!是求其数值解"
+ "----------------------------\n\n");
do{
//确认y'=f(x,y)的f(x,y)已经写入Function接口
System.out.println("你要的y'=f(x,y)的f(x,y)若未在源码中写好则请先写好,若写好了请按c:\n");
another=input.next();
//输入初值
System.out.println("请输入初始条件y(x0)=y0的x0:\n");
double x0 = input.nextDouble();
System.out.println("请输入初始条件y(x0)=y0的y0:\n");
double y0 = input.nextDouble();
function=new MyFunction();
ode=new OrdinaryDifferentialEquation(function,x0,y0);//新建这个初值问题
do {
int choice = 0;
boolean retry = true;
while (retry) {
retry = false;
System.out.println();
System.out.println("1---四阶古典龙格库塔法");
System.out.println("2---显式四阶阿当姆斯公式");
System.out.println("3---四阶阿当姆斯预测校正法(即隐式四阶阿当姆斯公式)");
System.out.println("4---欧拉公式");
System.out.println("请选择:");
try {
choice = Integer.parseInt(input.next());
} catch (NumberFormatException e) {
System.out.println("请别胡乱输入!\n");
retry = true;
}
}
switch (choice) {
case 1:
System.out.println("求数值解,现在构造节点!");
System.out.println("请输入节点xi的步长:");
double h = input.nextDouble();
System.out.println("请输入节点xi的最大值xn:");
double xLimit = input.nextDouble();
System.out.println();
System.out.println("结果:");
System.out.println(ode.rungeKutta(h, xLimit));
break;
case 2:
System.out.println("求数值解,现在构造节点!");
System.out.println("请输入节点xi的步长:");
h = input.nextDouble();
System.out.println("请输入节点xi的最大值xn:");
xLimit = input.nextDouble();
System.out.println();
System.out.println("结果:");
System.out.println(ode.explicitAdams(h, xLimit));
break;
case 3:
System.out.println("求数值解,现在构造节点!");
System.out.println("请输入节点xi的步长:");
h = input.nextDouble();
System.out.println("请输入节点xi的最大值xn:");
xLimit = input.nextDouble();
System.out.println();
System.out.println("结果:");
System.out.println(ode.predictorCorrectorAdams(h, xLimit));
break;
case 4:
System.out.println("求数值解,现在构造节点!");
System.out.println("请输入节点xi的步长:");
h = input.nextDouble();
System.out.println("请输入节点xi的最大值xn:");
xLimit = input.nextDouble();
System.out.println();
System.out.println("结果:");
System.out.println(ode.euler(h, xLimit));
break;
}
System.out.println(" 要再使用其他方法(对于刚才这个初值问题)吗? y/n:");
anotherChoice = input.next();
} while (anotherChoice.equalsIgnoreCase("y"));
System.out.println("\n 输入另一个常微分方程初值问题? y/n:");
another = input.next();
}while(another.equalsIgnoreCase("y"));
}
private static class MyFunction implements Function {
public double f(double x) {
return 0;
}
// -------------注意!!!!!!y'=f(x,y)的f(x,y)在此写入-----------------------------------------
public double f(double x, double y) {
return x+y;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -