I'm trying to implement an algorithm we did in numerical methods class. I have an identical procedure written in Maple and it works fine. I can't understand why it isn't working in C++.
Any help or tips would be appreciated; thanks in advance.
#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 ";
}
When running the code it asks for the allowed number of iterations and after inserting that it closes.