有一个解决Lorentz系统的代码,我需要在相应的数组解决方案的每次迭代中保存所有解x,y,z,即5000个解x,y,z放在一个数组中,如何这是最好的吗?那么如何将数组转换为字符串呢?
public class Butterfly {
public static double dx(double x, double y, double z) {
return -10*(x - y);
}
public static double dy(double x, double y, double z) {
return -x*z + 28*x - y;
}
public static double dz(double x, double y, double z) {
return x*y - 8*z/3;
}
public static void main(String[] args) {
double x = 0.0, y = 20.0, z = 25.0; //
double dt = 0.001;
// uses Euler method
for (int i = 0; i < 5000; i++) {
//
double xnew = x + dx(x, y, z) * dt;
double ynew = y + dy(x, y, z) * dt;
double znew = z + dz(x, y, z) * dt;
x = xnew;
y = ynew;
z = znew;
double[][] xyzArray = new double[5000][3];
for (i = 0; i < xyzArray.length; i++) {
for (int j = 0; i < xyzArray.length; j++) {
xyzArray[i][j] = x;
}
}
for (i = 0; i < xyzArray.length; i++) {
for (int j = 0; i < xyzArray.length; j++) {
System.out.println(xyzArray[i][j]);
}
System.out.println();
}
}
}
}
- 编辑代码后,程序显示5000倍的值x,y,z,而不是所有值。我认为以这种方式失去了其他解决方案。毕竟,每个变量我有5000个决定,我需要保存每个......
// uses Euler method
double[][] xyzArray = new double[5000][3];
for (int i = 0; i < xyzArray.length; i++) {
for (i = 0; i < 5000; i++) {
double xnew = x + dx(x, y, z) * dt;
double ynew = y + dy(x, y, z) * dt;
double znew = z + dz(x, y, z) * dt;
xyzArray[i][0] = xnew;
xyzArray[i][1] = ynew;
xyzArray[i][2] = znew;
}
}
for (int i = 0; i < xyzArray.length; i++) {
System.out.println(xyzArray[i][0] + ", " + xyzArray[i][1] + ", " + xyzArray[i][2]);
}
}
编辑后生成以下代码。仍会输出第一次迭代的5,000个解决方案:它在IDEAS或jshell中不起作用。我知道代码是正确的,但我无法理解为什么我得到错误的结果
public class Butterfly {
public static double dx(double x, double y, double z) {
return -10 * (x - y);
}
public static double dy(double x, double y, double z) {
return -x * z + 28 * x - y;
}
public static double dz(double x, double y, double z) {
return x * y - 8 * z / 3;
}
public static void main(String[] args) {
double x = 0.0, y = 20.0, z = 25.0;
double dt = 0.001;
double[][] xyzArray = new double[5000][3];
for (int i = 0; i < xyzArray.length; i++) {
double xnew = x + dx(x, y, z) * dt;
double ynew = y + dy(x, y, z) * dt;
double znew = z + dz(x, y, z) * dt;
xyzArray[i][0] = xnew;
xyzArray[i][1] = ynew;
xyzArray[i][2] = znew;
}
for (int i = 0; i < xyzArray.length; i++) {
System.out.println(xyzArray[i][0] + ", " + xyzArray[i][1] + ", " + xyzArray[i][2]);
}
}
}
答案 0 :(得分:0)
您已在循环中声明了您的数组。您不想创建5000个数据点阵列的5000个副本!您只需要1个5000个数据点的数组,因此您需要在循环外声明和创建数组。
double[][] xyzArray = new double[5000][3];
// uses Euler method
for (int i = 0; i < xyzArray.length; i++) {
// ... compute xnew, ynew, znew ... etc ...
获得当前步骤的新值后,您需要将它们保存在数组的[i]
条目中。您将x
存储在[i][0]
子条目中,y
存储在[i][1]
子条目中,z
存储在[i][2]
中子条目。
xyzArray[i][0] = xnew;
xyzArray[i][1] = ynew;
xyzArray[i][2] = znew;
在计算完所有值后打印出值:
} // end of Euler method loop
for (int i = 0; i < xyzArray.length; i++) {
System.out.println(xyzArray[i][0] + ", " + xyzArray[i][1] +", " + xyzArray[i][2]);
}
您的revision 3 code有效。
jshell
Butterfly.main(new String[] {})
你会看到你的5000 x,y,z值 - 都是不同的。
C:\>"\Program Files\Java\jdk-10\bin\jshell.exe"
| Welcome to JShell -- Version 10
| For an introduction type: /help intro
jshell> public class Butterfly {
...>
...> public static double dx(double x, double y, double z) {
...> return -10*(x - y);
[... many lines omitted for brevity ...]
...> }
...> }
...> }
| created class Butterfly
jshell> Butterfly.main(new String[] {})
0.2, 19.98, 24.933333333333334
0.39780000000000004, 19.960633333333334, 24.870840444444443
0.5934283333333333, 19.9419174796712, 24.81245854319926
0.786913224796712, 19.92386713960567, 24.758126085937494
[... many lines omitted for brevity ...]
0.381817425662861, 0.5879585365342771, 12.654916756967012
0.38387883677157514, 0.59322959817818, 12.621394805096582
0.3859723443856412, 0.5985398896533907, 12.587965480571079
0.3880980198383187, 0.6038899688589537, 12.554628592467306
jshell>