遍历数组以计算相对误差
Looping through an array to calculate relative error
我试图通过四个嵌套的 while 循环让我的程序 运行 通过从数组 {-5, -4, -3, -2 中绘制来计算用户输入数字的相对误差, -1, -1/2, -1/3, -1/4, 0, 1/4, 1/3, 1/2, 1, 2, 3, 4, 5}。以下是此作业的摘录:
"Consider the de Jager formula w^a x^b y^c z^d, 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 的值和近似值的相对误差最接近的百分之一。"
下面的代码是我目前所拥有的。我意识到它在当前状态下无法正常工作,但我希望我至少走在正确的轨道上。我只是不确定从这里去哪里或如何修复我的嵌套循环。非常感谢任何帮助,因为我真的在为这应该如何工作而苦苦挣扎。我也意识到 for 循环对于这种嵌套循环会更好,但我需要先使用 while 循环才能使用 for。
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;
public final class ABCDGuesser1 {
private ABCDGuesser1() {
}
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive numbers not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
int[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0, 1 / 4,
1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
int a = 0;
int i = 0;
while (i < charm.length) {
a = charm[i];
double max1 = (Math.pow(w, a));
i++;
int b = 0;
int j = 0;
while (j < charm.length) {
b = charm[j];
double max2 = (Math.pow(x, b));
j++;
int c = 0;
int k = 0;
while (k < charm.length) {
c = charm[k];
double max3 = (Math.pow(y, c));
k++;
int d = 0;
int n = 0;
while (n < charm.length) {
d = charm[n];
double max4 = (Math.pow(z, d));
n++;
}
out.print(max1);
}
}
}
in.close();
out.close();
}
}
如 Invexity 所述,double[] 魅力,而不是 int[] 魅力
在循环的同时,跟踪 bestEstimate
所以你的代码应该是
package area51;
import java.io.PrintStream;
public final class ABCDGuesser1 {
private ABCDGuesser1() {
}
public static void main(String[] args) {
PrintStream out = System.out;
//out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = 2.567;
//out.print("Enter four postitive numbers not equal to 1: ");
double w = 123.4;
double x = 2.567;
double y = 8.9;
double z = 12.6;
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0, 1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
String bestIndexes = "";
for (int i = 0; i < charm.length; i++) {
double max1 = (Math.pow(w, charm[i]));
for (int j = 0; j < charm.length; j++) {
double max2 = (Math.pow(x, charm[j]));
for (int k = 0; k < charm.length; k++) {
double max3 = (Math.pow(y, charm[k]));
for (int n = 0; n < charm.length; n++) {
double max4 = (Math.pow(z, charm[n]));
double currentEstimate = max1 * max2 * max3 * max4;
if (Math.abs(mu - currentEstimate) < Math.abs(mu - bestEstimate)) {
bestEstimate = currentEstimate;
bestIndexes = ""+i+","+j+","+k+","+n;
}
}
//out.print(max1);
}
}
}
out.println("Best Estimate = " + bestEstimate);
out.println("Best Indexes = " + bestIndexes);
}
}
/*Could you tell me why these two programs give different outputs? The only difference between them that I can see is that the first uses while loops with no method, while the second uses for loops and a main method. I don't want to change the for loop program anymore if I have to, I just want the while loop program's output to match that of the for loop one. The output for the first program is:
Enter a positive real-valued universal physical or mathematical constant: 238900
Enter four postitive number not equal to 1: 14
102329
1936
13
Most appropriate exponents for w, x, y and z: 1 14 6 12
Best Estimate = 253104.94721879277
Relative error = 0.059459804180798555%
Exception in thread "main" java.lang.AssertionError: Violation of: this is open
at components.simplewriter.SimpleWriter1L.println(SourceFile:177)
at ABCDGuesser1.main(ABCDGuesser1.java:93)
Second program:
Enter a positive real-valued universal physical or mathematical constant: 238900
Enter four postitive number not equal to 1: 14
102329
1936
13
Most appropriate exponents for w, x, y and z: -4.0 2.0 1.0 -3.0
Best Estimate = 240193.14762851998
Relative error = 0.0054129243554624324
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive number not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0,
1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
double a, b, c, d;
double bestIndex1 = 0;
double bestIndex2 = 0;
double bestIndex3 = 0;
double bestIndex4 = 0;
int i = 0;
while (i < charm.length) {
a = (Math.pow(w, charm[i]));
i++;
int j = 0;
while (j < charm.length) {
b = (Math.pow(x, charm[j]));
j++;
int k = 0;
while (k < charm.length) {
c = (Math.pow(y, charm[k]));
k++;
int n = 0;
while (n < charm.length) {
d = (Math.pow(z, charm[n]));
double currentEstimate = a * b * c * d;
if (Math.abs(mu - currentEstimate) < Math.abs(mu
- bestEstimate)) {
bestEstimate = currentEstimate;
bestIndex1 = charm[i];
bestIndex2 = charm[j];
bestIndex3 = charm[k];
bestIndex4 = charm[n];
}
n++;
}
}
}
double relError = Math.abs((mu - bestEstimate) / mu);
out.println("Most appropriate exponents for w, x, y and z: "
+ bestIndex1 + " " + bestIndex2 + " " + bestIndex3 + " "
+ bestIndex4);
out.println("Best Estimate = " + bestEstimate);
out.println("Relative error = " + relError + "%");
in.close();
out.close();
}
}
private static double relError(double constant, double fin) {
double estimate = Math.abs((constant - fin) / constant);
return estimate;
}
/**
* Main method.
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive number not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0,
1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
double bestIndex1 = 0;
double bestIndex2 = 0;
double bestIndex3 = 0;
double bestIndex4 = 0;
for (int i = 0; i < charm.length; i++) {
double a = (Math.pow(w, charm[i]));
for (int j = 0; j < charm.length; j++) {
double b = (Math.pow(x, charm[j]));
for (int k = 0; k < charm.length; k++) {
double c = (Math.pow(y, charm[k]));
for (int n = 0; n < charm.length; n++) {
double d = (Math.pow(z, charm[n]));
double currentEstimate = a * b * c * d;
if (Math.abs(mu - currentEstimate) < Math.abs(mu
- bestEstimate)) {
bestEstimate = currentEstimate;
bestIndex1 = charm[i];
bestIndex2 = charm[j];
bestIndex3 = charm[k];
bestIndex4 = charm[n];
}
}
}
}
}
double error = relError(mu, bestEstimate);
out.println("Most appropriate exponents for w, x, y and z: "
+ bestIndex1 + " " + bestIndex2 + " " + bestIndex3 + " "
+ bestIndex4);
out.println("Best Estimate = " + bestEstimate);
out.print("Relative error = ");
out.println(error);
in.close();
out.close();
}
}
我试图通过四个嵌套的 while 循环让我的程序 运行 通过从数组 {-5, -4, -3, -2 中绘制来计算用户输入数字的相对误差, -1, -1/2, -1/3, -1/4, 0, 1/4, 1/3, 1/2, 1, 2, 3, 4, 5}。以下是此作业的摘录:
"Consider the de Jager formula w^a x^b y^c z^d, 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 的值和近似值的相对误差最接近的百分之一。"
下面的代码是我目前所拥有的。我意识到它在当前状态下无法正常工作,但我希望我至少走在正确的轨道上。我只是不确定从这里去哪里或如何修复我的嵌套循环。非常感谢任何帮助,因为我真的在为这应该如何工作而苦苦挣扎。我也意识到 for 循环对于这种嵌套循环会更好,但我需要先使用 while 循环才能使用 for。
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;
public final class ABCDGuesser1 {
private ABCDGuesser1() {
}
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive numbers not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
int[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0, 1 / 4,
1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
int a = 0;
int i = 0;
while (i < charm.length) {
a = charm[i];
double max1 = (Math.pow(w, a));
i++;
int b = 0;
int j = 0;
while (j < charm.length) {
b = charm[j];
double max2 = (Math.pow(x, b));
j++;
int c = 0;
int k = 0;
while (k < charm.length) {
c = charm[k];
double max3 = (Math.pow(y, c));
k++;
int d = 0;
int n = 0;
while (n < charm.length) {
d = charm[n];
double max4 = (Math.pow(z, d));
n++;
}
out.print(max1);
}
}
}
in.close();
out.close();
}
}
如 Invexity 所述,double[] 魅力,而不是 int[] 魅力
在循环的同时,跟踪 bestEstimate
所以你的代码应该是
package area51;
import java.io.PrintStream;
public final class ABCDGuesser1 {
private ABCDGuesser1() {
}
public static void main(String[] args) {
PrintStream out = System.out;
//out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = 2.567;
//out.print("Enter four postitive numbers not equal to 1: ");
double w = 123.4;
double x = 2.567;
double y = 8.9;
double z = 12.6;
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0, 1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
String bestIndexes = "";
for (int i = 0; i < charm.length; i++) {
double max1 = (Math.pow(w, charm[i]));
for (int j = 0; j < charm.length; j++) {
double max2 = (Math.pow(x, charm[j]));
for (int k = 0; k < charm.length; k++) {
double max3 = (Math.pow(y, charm[k]));
for (int n = 0; n < charm.length; n++) {
double max4 = (Math.pow(z, charm[n]));
double currentEstimate = max1 * max2 * max3 * max4;
if (Math.abs(mu - currentEstimate) < Math.abs(mu - bestEstimate)) {
bestEstimate = currentEstimate;
bestIndexes = ""+i+","+j+","+k+","+n;
}
}
//out.print(max1);
}
}
}
out.println("Best Estimate = " + bestEstimate);
out.println("Best Indexes = " + bestIndexes);
}
}
/*Could you tell me why these two programs give different outputs? The only difference between them that I can see is that the first uses while loops with no method, while the second uses for loops and a main method. I don't want to change the for loop program anymore if I have to, I just want the while loop program's output to match that of the for loop one. The output for the first program is:
Enter a positive real-valued universal physical or mathematical constant: 238900
Enter four postitive number not equal to 1: 14
102329
1936
13
Most appropriate exponents for w, x, y and z: 1 14 6 12
Best Estimate = 253104.94721879277
Relative error = 0.059459804180798555%
Exception in thread "main" java.lang.AssertionError: Violation of: this is open
at components.simplewriter.SimpleWriter1L.println(SourceFile:177)
at ABCDGuesser1.main(ABCDGuesser1.java:93)
Second program:
Enter a positive real-valued universal physical or mathematical constant: 238900
Enter four postitive number not equal to 1: 14
102329
1936
13
Most appropriate exponents for w, x, y and z: -4.0 2.0 1.0 -3.0
Best Estimate = 240193.14762851998
Relative error = 0.0054129243554624324
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive number not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0,
1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
double a, b, c, d;
double bestIndex1 = 0;
double bestIndex2 = 0;
double bestIndex3 = 0;
double bestIndex4 = 0;
int i = 0;
while (i < charm.length) {
a = (Math.pow(w, charm[i]));
i++;
int j = 0;
while (j < charm.length) {
b = (Math.pow(x, charm[j]));
j++;
int k = 0;
while (k < charm.length) {
c = (Math.pow(y, charm[k]));
k++;
int n = 0;
while (n < charm.length) {
d = (Math.pow(z, charm[n]));
double currentEstimate = a * b * c * d;
if (Math.abs(mu - currentEstimate) < Math.abs(mu
- bestEstimate)) {
bestEstimate = currentEstimate;
bestIndex1 = charm[i];
bestIndex2 = charm[j];
bestIndex3 = charm[k];
bestIndex4 = charm[n];
}
n++;
}
}
}
double relError = Math.abs((mu - bestEstimate) / mu);
out.println("Most appropriate exponents for w, x, y and z: "
+ bestIndex1 + " " + bestIndex2 + " " + bestIndex3 + " "
+ bestIndex4);
out.println("Best Estimate = " + bestEstimate);
out.println("Relative error = " + relError + "%");
in.close();
out.close();
}
}
private static double relError(double constant, double fin) {
double estimate = Math.abs((constant - fin) / constant);
return estimate;
}
/**
* Main method.
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive number not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0,
1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
double bestIndex1 = 0;
double bestIndex2 = 0;
double bestIndex3 = 0;
double bestIndex4 = 0;
for (int i = 0; i < charm.length; i++) {
double a = (Math.pow(w, charm[i]));
for (int j = 0; j < charm.length; j++) {
double b = (Math.pow(x, charm[j]));
for (int k = 0; k < charm.length; k++) {
double c = (Math.pow(y, charm[k]));
for (int n = 0; n < charm.length; n++) {
double d = (Math.pow(z, charm[n]));
double currentEstimate = a * b * c * d;
if (Math.abs(mu - currentEstimate) < Math.abs(mu
- bestEstimate)) {
bestEstimate = currentEstimate;
bestIndex1 = charm[i];
bestIndex2 = charm[j];
bestIndex3 = charm[k];
bestIndex4 = charm[n];
}
}
}
}
}
double error = relError(mu, bestEstimate);
out.println("Most appropriate exponents for w, x, y and z: "
+ bestIndex1 + " " + bestIndex2 + " " + bestIndex3 + " "
+ bestIndex4);
out.println("Best Estimate = " + bestEstimate);
out.print("Relative error = ");
out.println(error);
in.close();
out.close();
}
}