我想知道是否有辦法獲得一定的輸出。對於我的代碼,我使用牛頓拉夫森方法來解決三個方程。我必須在某個迭代中顯示它的根。另一種輸出方式?
正如你可以從你的代碼中看到的,我有輸出爲「根是ROOT後COUNTER迭代。」現在,我在運行程序後得到答案後,在根和計數器中輸入代碼。有沒有辦法讓我的輸出像「」在「+ counter +」迭代之後根是「+ root」。「」?換句話說,我該如何讓程序找到根並打印出來,而不是我必須與計數器一起手動輸入它?
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.
您想要顯示該信息多少次?在什麼時間間隔? – OnePunchMan 2014-09-26 06:10:59
你幾乎把你的問題的答案!你想要「根+ +反擊」後的根是「+根+」。如果'root'和'counter'是變量,那這就是合法的Java。即''System.out.println(「在+計數器+」迭代之後,根是「+ root +」。「);'。你所要做的就是定義變量'root'和'counter',並確保它們被設置爲正確的值('root'將是你已經打印出來的東西,所以不應該很難)。 – ajb 2014-09-26 06:20:53
@ajb,我如何將root的值設置爲for循環中的某個值?就像我知道的第一個方程的根是-1.465572,但我怎麼能沒有實際說'雙根= -1.465572'的根設置爲該值? – evarias 2014-09-27 04:01:38