尝试以数值方式求解微分方程(第一次)。我的程序要求提供一堆参数,然后计算将一瓶酒冷却到给定温度所需的时间。这是我的主要内容:
import java.util.Scanner;
public class Step2_lab11 {
public static int TAO = 50;
public static double DELTA_MINUTES = 0.1;
public static void main(String[] args) {
System.out.println("VINETS AVKYLNINGSTID \n");
System.out.println("Ange vinets temperatur:");
Scanner userIn = new Scanner(System.in);
double wineTemp = userIn.nextDouble();
System.out.println("Vinets önskade temperatur:");
double preferredTemp = userIn.nextDouble();
System.out.println("Kylens/frysens temperatur:");
double chillTemp = userIn.nextDouble();
WineChiller wineChiller = new WineChiller();
double elapsedTime = 0.0;
while(wineTemp > preferredTemp) {
elapsedTime = elapsedTime + DELTA_MINUTES;
double dT = wineChiller.getChillingTime(TAO, DELTA_MINUTES, chillTemp, preferredTemp, wineTemp);
wineTemp = wineTemp - dT;
System.out.println(elapsedTime);
}
}
}
这是 WineChiller.java 文件:
public class WineChiller {
public WineChiller() {
}
public double getChillingTime(int TAO, double DELTA_MINUTES, double chillTemp, double preferredTemp, double wineTemp) {
double dT = (wineTemp - chillTemp) * DELTA_MINUTES / TAO;
return dT;
}
}
while 循环的 Syso 部分产生这个(wineTemp = 25,preferredTemp = 16,chillTemp = 5)
0.1
0.2
0.30000000000000004
....
29.300000000000146
29.400000000000148
29.50000000000015
29.60000000000015
29.700000000000152
29.800000000000153
29.900000000000155
不知道为什么它会添加随机小数。我也认为(但我不是 100%)正确答案应该是 30 分钟,而不是 29.9 分钟。我在这里遗漏了一些明显的逻辑错误吗?