2016-08-30 6 views
0

こんにちは私は他の多くの質問をチェックしましたが、私の実装が収束しない理由を理解できないようです。私はルートに入っても私のプログラムの "エラー、収束しない"部分を得続けます。C++のNewtonsメソッド

関数であるY = X^2 - ここで1

コードである:

// Newton sqaure root finder function 

#include <iostream> 
#include <cmath> 

int main() 
{ 
using namespace std; 

// Enter an initial guess x 
cout << "Enter an initial guess: "; 
double x; 
cin >> x; 


// Define & initialize the error, tolerance and iteration variables 
double tol = 1e-12; 
cout << 1e-12; 
double error = tol + 1; 
int it = 0; 
int max_it = 100; 

// Define the x1 variable to hold the latest result (root approximation) 
double x1; 

// Start while loop with guess x to find the root 
while (error > tol && it < max_it) 
{ 
    x1 = x + (x*x-1)/(2*x); 
    error = fabs(x1 - x); 
    x = x1; 
    it++; 
    cout << error << endl; 
} 

if (error <= tol) 
{ 
    cout << "The root is " << x << endl; 
} 
else 
{ 
    cout << "Error, no convergence" << endl; 
} 

cin.get(); 
cin.get(); 

return 0; 
} 
+5

これは、デバッガを使用するのに最適なプログラムです。あなたはおそらく、この記事を作るのにかかった時に答えを見つけたでしょう。 –

+0

私はお詫びしますが、デバッガの使い方はわかりません。あなたは私にウェブサイトや、それがどのように行われているかをリンクすることができますか?ありがとうございました! – DoubleOseven

+0

これは、どのプラットフォームでコードを書くか、どのコンパイラを使用するかによって大きく異なります。 – Hayt

答えて