数値メソッドクラスで行ったアルゴリズムを実装しようとしています。私はメープルで書かれた同じ手順を持っています、そしてそれはうまくいきます。なぜC++で動作しないのか理解できません。
ヘルプやヒントをいただければ幸いです。前もって感謝します。
#include "stdafx.h"
#include "math.h"
#include <iostream>
using namespace std;
double absval(double val)
{
if (val >= 0) return val;
else return -val;
}
double G(double x)
{
double g;
g = 1/((x*x)+12);
return g;
}
int main()
{
double c = 0.011834; /* constant */
double eps = 0.00001; /* precision */
double x0 = 0; /* initial aproximation */
double x1,x2,c1;
int i,no;
i = 1;
cout << "enter max nr of iterations ";
cin >> no;
x1 = x0;
x2 = G(x1);
c1 = (1-c)*eps/c;
while (absval(x1-x0) > c1)
{
i = i+1;
x1 = x2;
x2 = G(x1);
}
cout << x2;
if (i<no) cout << " solution found in allowed nr of iterations ";
else cout << "allowed nr of iterations surpassed ";
}
コードを実行すると、許可された反復回数を要求され、挿入後に閉じます。