gpt4 book ai didi

java - 另一种输出方式?

转载 作者:行者123 更新时间:2023-12-01 12:29:30 25 4
gpt4 key购买 nike

我想知道是否有办法获得特定的输出。对于我的代码,我使用牛顿拉夫森方法来求解三个方程。我必须在某个迭代中显示它的根。

正如您从代码中看到的,我的输出为“在 COUNTER 次迭代之后,根为 ROOT”。现在,在运行程序得到答案后,我在代码中输入根和计数器。有没有办法可以得到像“”The root is “+ root” after “+ counter +” iterations.” 这样的输出?”换句话说,我怎样才能让程序找到根并打印它,而不是我必须手动输入它和计数器?

public static void main(String[] args)
{

//x^3 + x^2 + 1
//2x^3 - 2x^2 - 2
//3x^3 + 3x^2 + 3

newrap1();
newrap2();
newrap3();
}
public static double func1(double x)
{
double f1;
f1 = Math.pow(x, 3) + Math.pow(x, 2) + 1;
return f1;
}
public static double func2(double x)
{
double f2;
f2 = 2*Math.pow(x, 3) - 2*Math.pow(x, 2) - 2;
return f2;
}
public static double func3(double x)
{
double f3;
f3 = 3*Math.pow(x, 3) + 3*Math.pow(x, 2) + 3;
return f3;
}
public static double der1(double x)
{
double d1;
d1 = 3*Math.pow(x, 2) + 2*x;
return d1;
}
public static double der2(double x)
{
double d2;
d2 = 6*Math.pow(x, 2) - 4*x;
return d2;
}
public static double der3(double x)
{
double d3;
d3 = 9*Math.pow(x, 2) + 6*x;
return d3;
}
public static void newrap1()
{
double x = 100;
for (int i = 0; i < 30; i++)
{
double diff;
diff = func1(x)/der1(x);
if (diff == 0) return;
x -= diff;
System.out.println(Math.floor(x * 1e6) / 1e6);

}
System.out.println("The root is -1.465572 after 20 iterations.");
System.out.println();
}
public static void newrap2()
{
double x = 100;
for (int i = 0; i < 30; i++)
{
double diff;
diff = func2(x)/der2(x);
if (diff == 0) return;
x -= diff;
System.out.println(Math.floor(x * 1e6) / 1e6);
}
System.out.println("The root is 1.465571 after 15 iterations.");
System.out.println();
}
public static void newrap3()
{
double x = 100;
for (int i = 0; i < 30; i++)
{
double diff;
diff = func3(x)/der3(x);
if (diff == 0) continue;
x -= diff;
System.out.println(Math.floor(x * 1e6) / 1e6);
}
System.out.println("The root is -1.465572 after 20 iterations.");
System.out.println();
}

这是当前输出

66.556258
44.260755
29.39754
19.489335
12.884605
8.482183
5.547478
3.589458
2.277446
1.382632
0.729147
0.100537
-4.269108
-2.99942
-2.190119
-1.719717
-1.511997
-1.467533
-1.465575
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
The root is -1.465572 after 20 iterations.

66.778557
44.631345
29.867195
20.025493
13.466126
9.09625
6.188412
4.259889
2.993429
2.186424
1.717784
1.511383
1.467482
1.465574
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
The root is 1.465571 after 15 iterations.

66.556258
44.260755
29.39754
19.489335
12.884605
8.482183
5.547478
3.589458
2.277446
1.382632
0.729147
0.100537
-4.269108
-2.99942
-2.190119
-1.719717
-1.511997
-1.467533
-1.465575
-1.465572
-1.465572
-1.465572
The root is -1.465572 after 20 iterations.

最佳答案

一些一般注意事项:

  • 您正在打印一条已修复的消息,并且您正在进行 3 次 30 次迭代(而不是您在消息中写入的 15、20 和 30)。这个for (int i = 0; i < 30; i++)总是一样的。
  • 我猜程序的整个精神就是迭代直到收敛,所以使用 2 个变量。一个可以存储先前的值,一个可以存储当前的值。比较这两个变量,您可以想象是否发生收敛和 break循环。因为比较不会给出准确的 0值( double 不准确)使用阈值 e在这种情况下,你们的值(value)观被认为是平等的。
  • 从这个意义上说,您可以使用 while循环以使其收敛,然后退出循环或使用 for 使用预定义数量的循环命令并使用 break当它在给定的迭代内收敛时。
  • 对于您表达的主要问题,您可以在每次迭代中存储根估计,而不是在每个循环中打印它。除非你想看到收敛过程。
  • 推杆double finalRoot = Math.floor(x * 1e6) / 1e6例如,在您的代码中,您可以使用它在循环完成后进行打印。不过,请在循环之前定义它,以便在循环完成后可以访问。

希望能给您一个总体概念。

关于java - 另一种输出方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26052994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com