使用 de Jager 公式和 While 循环计算相对误差
Calculate Relative Error Using de Jager formula and While Loops
首先,我使用 for 循环进行了这项工作。但是我不能使用循环。它必须是这些嵌套的 while 循环。
由于我的程序使用 for 循环运行良好,所以我的嵌套 while 逻辑一定有问题。每次运行下面的代码时,我总是返回 0 作为 closestEstimate(我初始化的 closestEstimate 起点)。这不会发生,我用 for 循环得到了正确的百分比估计值。所以我的嵌套 while 循环逻辑在某处存在问题。
说明如下:
"Now consider the de Jager formula waxbyczd, where each of a, b, c, and d is one of the 17 numbers {-5, -4, -3, -2, -1, -1/2, -1/3, -1/4, 0, 1/4, 1/3, 1/2, 1, 2, 3, 4, 5}. The "迷人理论”断言,可以使用 de Jager 公式和你的四个个人数字来近似 μ,相对误差在 1% 以内。例如,假设你选择近似平均距离从地球到月球以英里为单位:μ = 238,900。假设您是 OSU 体育迷,那么您的个人数字是 OSU 上一届全国冠军赛季的获胜次数(14;也是任何人一年中获胜的记录大学队),俄亥俄体育场的座位数(102,329),杰西欧文斯在柏林获得四枚金牌的年份(1936 年),以及你打高中曲棍球时的球衣号码(13)。那么 14- 的值5102329119361/2134大约是239,103,大约在μ的0.08%以内。
你的工作是创建一个 Java 程序,询问用户应该近似什么常数 μ,然后依次询问四个个人数字 w、x、y 和 z 中的每一个。然后程序应计算并报告使 de Jager 公式尽可能接近 μ 的指数 a、b、c 和 d 的值,以及公式 waxbyczd 的值和近似值的相对误差最接近的百分之一。"
任何关于我在下面的代码中出错的地方的帮助将不胜感激。如果有帮助,我也可以 post 工作 for-loop 版本。唯一未包含的代码是收集以下数字的静态方法调用,但由于它们在 for 循环版本中不是问题,因此我没有在此处包含它们,因为它会使代码更长且可读性更差。让我知道是否有帮助。
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
//represents our universal physical or mathematical constant. any positive real number
out.println(
"First, let's get the double value for our mathematical constant:");
double mu = getPositiveDouble(in, out);
//represents our four personal numbers (not equal to 1) for da jager formula
out.println(
"Now we need four numbers with personal significance to you "
+ "(doubles not equal to 1.0):");
double w = getPositiveDoubleNotOne(in, out);
double x = getPositiveDoubleNotOne(in, out);
double y = getPositiveDoubleNotOne(in, out);
double z = getPositiveDoubleNotOne(in, out);
//these are the 17 numbers as exponents for second part of da jager formula
double[] charmingTheory = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4,
0, 1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
//initialize counters for our four loops
int i = 0;
int j = 0;
int k = 0;
int l = 0;
//starting estimate
double closestEstimate = 0;
while (i < charmingTheory.length) {
double num1 = Math.pow(w, charmingTheory[i]);
out.println(num1);
i++;
while (j < charmingTheory.length) {
double num2 = Math.pow(x, charmingTheory[j]);
j++;
while (k < charmingTheory.length) {
double num3 = Math.pow(y, charmingTheory[k]);
k++;
while (l < charmingTheory.length) {
double num4 = Math.pow(z, charmingTheory[l]);
double estimate = num1 * num2 * num3 * num4;
if (Math.abs(mu - estimate) < Math
.abs(mu - closestEstimate)) {
closestEstimate = estimate;
}
l++;
}
}
}
}
out.println("Closest Estimate is:");
out.println(closestEstimate);
您没有在每个 while
循环开始时重置 j,k,l
循环计数器的值。因此它们只 运行 一次,第一次通过外部 i
循环。您需要将每个 while
循环的开始更新为如下所示:
j=0;
while (j < charmingTheory.length)
//....
k=0;
while (k < charmingTheory.length)
//....
l=0;
while (l < charmingTheory.length)
我怀疑这是与 for
循环一起工作的原因是你这样做了:
for(int j=0; j < charmingTheory.length; j++)
首先,我使用 for 循环进行了这项工作。但是我不能使用循环。它必须是这些嵌套的 while 循环。
由于我的程序使用 for 循环运行良好,所以我的嵌套 while 逻辑一定有问题。每次运行下面的代码时,我总是返回 0 作为 closestEstimate(我初始化的 closestEstimate 起点)。这不会发生,我用 for 循环得到了正确的百分比估计值。所以我的嵌套 while 循环逻辑在某处存在问题。
说明如下:
"Now consider the de Jager formula waxbyczd, where each of a, b, c, and d is one of the 17 numbers {-5, -4, -3, -2, -1, -1/2, -1/3, -1/4, 0, 1/4, 1/3, 1/2, 1, 2, 3, 4, 5}. The "迷人理论”断言,可以使用 de Jager 公式和你的四个个人数字来近似 μ,相对误差在 1% 以内。例如,假设你选择近似平均距离从地球到月球以英里为单位:μ = 238,900。假设您是 OSU 体育迷,那么您的个人数字是 OSU 上一届全国冠军赛季的获胜次数(14;也是任何人一年中获胜的记录大学队),俄亥俄体育场的座位数(102,329),杰西欧文斯在柏林获得四枚金牌的年份(1936 年),以及你打高中曲棍球时的球衣号码(13)。那么 14- 的值5102329119361/2134大约是239,103,大约在μ的0.08%以内。
你的工作是创建一个 Java 程序,询问用户应该近似什么常数 μ,然后依次询问四个个人数字 w、x、y 和 z 中的每一个。然后程序应计算并报告使 de Jager 公式尽可能接近 μ 的指数 a、b、c 和 d 的值,以及公式 waxbyczd 的值和近似值的相对误差最接近的百分之一。"
任何关于我在下面的代码中出错的地方的帮助将不胜感激。如果有帮助,我也可以 post 工作 for-loop 版本。唯一未包含的代码是收集以下数字的静态方法调用,但由于它们在 for 循环版本中不是问题,因此我没有在此处包含它们,因为它会使代码更长且可读性更差。让我知道是否有帮助。
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
//represents our universal physical or mathematical constant. any positive real number
out.println(
"First, let's get the double value for our mathematical constant:");
double mu = getPositiveDouble(in, out);
//represents our four personal numbers (not equal to 1) for da jager formula
out.println(
"Now we need four numbers with personal significance to you "
+ "(doubles not equal to 1.0):");
double w = getPositiveDoubleNotOne(in, out);
double x = getPositiveDoubleNotOne(in, out);
double y = getPositiveDoubleNotOne(in, out);
double z = getPositiveDoubleNotOne(in, out);
//these are the 17 numbers as exponents for second part of da jager formula
double[] charmingTheory = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4,
0, 1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
//initialize counters for our four loops
int i = 0;
int j = 0;
int k = 0;
int l = 0;
//starting estimate
double closestEstimate = 0;
while (i < charmingTheory.length) {
double num1 = Math.pow(w, charmingTheory[i]);
out.println(num1);
i++;
while (j < charmingTheory.length) {
double num2 = Math.pow(x, charmingTheory[j]);
j++;
while (k < charmingTheory.length) {
double num3 = Math.pow(y, charmingTheory[k]);
k++;
while (l < charmingTheory.length) {
double num4 = Math.pow(z, charmingTheory[l]);
double estimate = num1 * num2 * num3 * num4;
if (Math.abs(mu - estimate) < Math
.abs(mu - closestEstimate)) {
closestEstimate = estimate;
}
l++;
}
}
}
}
out.println("Closest Estimate is:");
out.println(closestEstimate);
您没有在每个 while
循环开始时重置 j,k,l
循环计数器的值。因此它们只 运行 一次,第一次通过外部 i
循环。您需要将每个 while
循环的开始更新为如下所示:
j=0;
while (j < charmingTheory.length)
//....
k=0;
while (k < charmingTheory.length)
//....
l=0;
while (l < charmingTheory.length)
我怀疑这是与 for
循环一起工作的原因是你这样做了:
for(int j=0; j < charmingTheory.length; j++)