2014-09-26 92 views
0

我想知道是否有辦法獲得一定的輸出。對於我的代碼,我使用牛頓拉夫森方法來解決三個方程。我必須在某個迭代中顯示它的根。另一種輸出方式?

正如你可以從你的代碼中看到的,我有輸出爲「根是ROOTCOUNTER迭代。」現在,我在運行程序後得到答案後,在根和計數器中輸入代碼。有沒有辦法讓我的輸出像「」在「+ 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. 
+0

您想要顯示該信息多少次?在什麼時間間隔? – OnePunchMan 2014-09-26 06:10:59

+0

你幾乎把你的問題的答案!你想要「根+ +反擊」後的根是「+根+」。如果'root'和'counter'是變量,那這就是合法的Java。即''System.out.println(「在+計數器+」迭代之後,根是「+ root +」。「);'。你所要做的就是定義變量'root'和'counter',並確保它們被設置爲正確的值('root'將是你已經打印出來的東西,所以不應該很難)。 – ajb 2014-09-26 06:20:53

+0

@ajb,我如何將root的值設置爲for循環中的某個值?就像我知道的第一個方程的根是-1.465572,但我怎麼能沒有實際說'雙根= -1.465572'的根設置爲該值? – evarias 2014-09-27 04:01:38

回答

1

的一般常識:

  • 你打印的是被固定的消息,您正在3次30迭代(不是15,20和30你寫在你的消息)。這for (int i = 0; i < 30; i++)總是相同的。
  • 我想程序的整個精神是迭代直到它收斂,所以使用2個變量。可以存儲前一個值和當前值。比較這兩個變量,您可以成像,如果收斂已經發生,並且循環。由於比較結果不會給出確切的0值(double s不準確),因此請使用閾值e,在此閾值下您的值被視爲相等。
  • 從這種意義上說,您可以使用while循環讓它收斂,然後退出循環或使用for命令使用預定義數量的循環,並且在給定迭代內收斂時使用break
  • 至於你表達的主要問題,你可以在每次迭代中存儲根估計,而不是在每個循環中打印。除非你想看到融合過程。
  • double finalRoot = Math.floor(x * 1e6)/1e6放入您的代碼中,例如您可以在循環完成後使用它進行打印。在循環之前定義它,以便在循環完成後可訪問。

希望給你一個大致的想法。

+0

沒錯。重點是迭代直到收斂。我不理解如何使用2個變量來存儲以前的值和當前的值。我怎樣才能做到這一點? – evarias 2014-09-27 04:08:09

+0

我不知道你是否想出了它,但無論如何,只是在每次迭代開始時,您將最終的根存儲在前一個變量中,並且在迭代結束時將新計算的根值存儲到當前值。因此,比較每次迭代結束時的2個值 – Eypros 2014-09-29 06:11:40