我正在尝试实现我们在数值方法类中所做的算法。我有一个用 Maple 编写的相同程序,它工作正常。我不明白为什么它不能在 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 ";
}
运行代码时,它会询问允许的迭代次数,并在插入后关闭。