#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double num, conv, old;
int tries, x;
cout << "Enter a value greater than 0: " << endl;
cin >> num;
cout << "How many times should we run this equation?" << endl;
cin >> tries;
old = num / 2;
conv = ((num / old) + old) / 2;
cout << "Try 0 = " << old << endl;
for (x = 1; x <= tries; x++)
{
cout << "Try " << x << " = " << conv << endl;
}
system("pause");
}
So saving the value of conv, and replacing old with conv in the equation as many times as tries.
Enter a value greater than 0:
25
How many times should we run this equation?
5
Try 0 = 12.5
Try 1 = 7.25
Try 2 = 7.25
Try 3 = 7.25
Try 4 = 7.25
Try 5 = 7.25
Press any key to continue . . .
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double num, conv, old;
int tries, x;
cout << "Enter a value greater than 0: " << endl;
cin >> num;
cout << "How many times should we run this equation?" << endl;
cin >> tries;
cout << "Try 0 = " << num / 2 << endl;
for (x = 1; x <= tries; x++)
{
old = num / 2;
conv = ((num / old) + old) / 2;
cout << "Try " << x << " = " << conv << endl;
}
system("pause");
}
Enter a value greater than 0:
25
How many times should we run this equation?
5
Try 0 = 12.5
Try 1 = 7.25
Try 2 = 7.25
Try 3 = 7.25
Try 4 = 7.25
Try 5 = 7.25
Press any key to continue . . .
You are doing what is called "single-point iteration", and if you do it right you should end up with the square root of num.
conv and old are fundamentally the same variable: possibly successive iterates. You can work out the RHS of an equation and then reassign to the variable on the left.
- In your line 21, replace old by conv and then move this line before the loop: it is an initial value, set only once.
- Replace both occurrences of old in line 22 by conv: yes, you are allowed to do that; it's the whole point of the iteration.
- You should then be able to remove variable old completely.
ALTERNATIVELY (and I'm putting this here just in case you are told to use a non-deterministic loop in the future)
- put conv = num / 2; before the loop as an initial value.
- replace the current line 21 by old = conv; and leave the current line 22 as it is.
- you keep both old and conv this time, but you can then base your decision to continue looping on the difference between successive iterates, abs(conv-old), rather than having a fixed number of tries.
So, fixing the number of tries (deterministic loop) means you may do either an unnecessary number of iterations or you may not do enough. To base convergence on the difference between successive iterates you will have to store the previous iterate (the "ALTERNATIVELY" approach above). How many iterates it takes depends on the tolerance set (which is a bit tight below).
I don't do code-fixing by PM @JarJarz:
(a) I might inadvertently give bad advice; the forum at least gives a chance for others to correct it;
(b) the rest of your class is entitled to the same advice, for good or bad.
EDITED!! Don't use - see correction in later post (thanks to @doug4 for the correction).
#include <iostream>
#include <cmath>
using namespace std;
int ipow( int n, int p ) // n to the power p (positive integer)//ERROR: n and result should be integers{
int result = 1;
while ( p-- ) result *= n;
return result;
}
double nthroot( double num, int n ) // find nth root of num
{
double TOLERANCE = num / 1.0e10; // cut-off at this difference between successive iterates
int MAXITER = 1000; // cut-off on iterations if not converging
double x = num / n; // initial guess
double xold = x + 1.0; // anything not close to initial guess
int iter = 0; // iteration counter
while ( abs( x - xold ) > TOLERANCE && iter < MAXITER ) // conditions to keep looping
{
iter++;
xold = x; // store previous iterate
x = ( ( n - 1 ) * x + num / ipow( x, n - 1 ) ) / n; // iteration step (Newton-Raphson for f(x) = x^n - num)
cout << "Iteration " << iter << ": " << x << '\n'; // REMOVE FOR FINAL VERSION
}
return x;
}
int main()
{ int num, root;// ERROR; num should be double
cout << "Input number and root: ";
cin >> num >> root;
double value = nthroot( num, root );
cout << "------\n" << num << " to the power 1/" << root << " is " << value;
}
#include <iostream>
#include <cmath>
usingnamespace std;
//======================================================================
double ipow( double x, int p ) // x to the power p (p is a positive integer)
{
double result = 1;
while ( p-- ) result *= x;
return result;
}
//======================================================================
double nthroot( double num, int n ) // find nth root of num
{
double TOLERANCE = num / 1.0e10; // cut-off at this difference between successive iterates
int MAXITER = 1000; // cut-off on iterations if not converging
double x = num / n; // initial guess
double xold = x + 1.0; // anything not close to initial guess
int iter = 0; // iteration counter
while ( abs( x - xold ) > TOLERANCE && iter < MAXITER ) // conditions to keep looping
{
iter++;
xold = x; // store previous iterate
x = ( ( n - 1 ) * x + num / ipow( x, n - 1 ) ) / n; // iteration step (Newton-Raphson for f(x) = x^n - num)
cout << "Iteration " << iter << ": " << x << '\n'; // REMOVE FOR FINAL VERSION
}
return x;
}
//======================================================================
int main()
{
double num;
int root;
cout << "Input number and root: ";
cin >> num >> root;
cout.setf( ios::fixed ); cout.precision( 10 );
double value = nthroot( num, root );
cout << "------\n" << num << " to the power 1/" << root << " is " << value;
}
//======================================================================
Input number and root: 6.25 2
Iteration 1: 2.5625000000
Iteration 2: 2.5007621951
Iteration 3: 2.5000001162
Iteration 4: 2.5000000000
Iteration 5: 2.5000000000
------
6.2500000000 to the power 1/2 is 2.5000000000