I've created a code, that calculates the root of function f(x) = sin(x). I use 3 as x_0. Here's the output:
Expand|Select|Wrap|Line Numbers
- Insert the number of iterations.
- 5
- Starting iteration for 5 rounds. Using xOld = 3
- 3.1425465430742778316641761193750426173210144042969
- 3.1415926533004769893864249752368777990341186523438
- 3.141592653589793115997963468544185161590576171875
- 3.141592653589793115997963468544185161590576171875
- 3.141592653589793115997963468544185161590576171875
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <stdlib.h>
- #include <math.h>
- using namespace std;
- int main()
- {
- // double tol = 1.0E-300;
- int nMax;
- int n;
- double xOld;
- double xNew;
- cout << "Insert the number of iterations. \n";
- cin >> nMax;
- cout << "Starting iteration for " << nMax << " rounds. Using xOld = 3" << endl;
- xOld = 3;
- cout.precision(50);
- for( n = 1; n <= nMax ; n++ )
- {
- xNew = xOld - tan(xOld);
- cout << xNew << endl;
- // if( fabs((xNew - xOld) / xOld ) < tol ) break;
- xOld = xNew;
- }
- }
If anyone knows what the problem is, do tell. Also improvements are welcome. Thanks in advance.