# Newton's Method

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 02-14-2011
hottiefee
That didn't change anything that I can see. The answer still comes out to -1.#INF. Even when I try sqroot(16) I get the same answer (-1.#INF not 4). Why would I change TOLERANCE to 100? Since the program has to have a tolerance of .000001, would I have to use #include<iomanip> and setprecision(6)? I forgot how to define a tolerance..
• 02-14-2011
nimitzhunter
Quote:

Originally Posted by hottiefee
That didn't change anything that I can see. The answer still comes out to -1.#INF. Even when I try sqroot(16) I get the same answer (-1.#INF not 4). Why would I change TOLERANCE to 100? Since the program has to have a tolerance of .000001, would I have to use #include<iomanip> and setprecision(6)? I forgot how to define a tolerance..

I edited the other post, you need to get rid of the line "x=0" in findRoots function.

Also you need
Code:

guess = c/2;
in main() before you call findRoot();
• 02-14-2011
hottiefee
Awesome. It's working now :) Thanks! The only thing is how do I set it to where it always shows the answer with 6 decimal places? I tried setpresicion(7) but that displays 7 digits instead of 6 decimal places.
• 02-14-2011
nimitzhunter
Use fixed along with setpricision.
Code:

cout<< fixed << setprecison(6).
• 02-14-2011
hottiefee
Oh yeah!! Thanks. I haven't used that since the begining of last semester so I forgot it. Thank you so much for the help :)
• 02-14-2011
nimitzhunter
No problem.
• 02-14-2011
csharp100
Quote:

Originally Posted by nimitzhunter
Use fixed along with setpricision.
Code:

cout<< fixed << setprecison(6).

I believe you will also need the library,
Code:

#include <iomanip>
to use setprecision.
• 02-15-2011
Elysia
#define TOLERANCE 10^-6
this is incorrect. "^" is not the power operator.
I believe there is a syntax such as
const float g_Tolerance = 10E-6;
Can't remember exactly.
Also, use const instead of define in C++.
• 02-16-2011
eros
This is what i came up with, not the most efficient but it works
Code:

double Sqrt(double num)
{
return Guess(num/2, num, 0, 256);
}
double Guess(double x, double y, int depth, int maxDepth)
{
if (++depth < maxDepth)
return Guess(x - (((x * x) - y) / (2 * x)), y, depth, maxDepth);
return x;
}

• 02-17-2011
Elysia
What? You invented your own sqrt or something?
• 02-17-2011
eros
Quote:

Originally Posted by Elysia
What? You invented your own sqrt or something?

Using Newton's Method, yes i did.
• 02-19-2011
nimitzhunter
Quote:

Originally Posted by Elysia
#define TOLERANCE 10^-6
this is incorrect. "^" is not the power operator.
I believe there is a syntax such as
const float g_Tolerance = 10E-6;
Can't remember exactly.
Also, use const instead of define in C++.

I just can't shake MATLAB out of my system sometimes.
• 02-19-2011
Mozza314
For a bit of fun, I thought I'd write my version of this program. Criticisms encouraged :-).

Code:

#include <cassert>
#include <cmath>
#include <cstdlib>
#include <iostream>

#include "boost/bind.hpp"
#include "boost/cstdint.hpp"

template <typename Function1, typename Function2>
double NewtonsMethod(
Function1 f,
Function2 fPrime,
double x,
double tolerance,
boost::uint32_t maxIterations)
{
assert(tolerance > 0);
assert(maxIterations != 0);

double last;
boost::uint32_t iterations = 0;

do
{
last = x;
x -= f(x) / fPrime(x);
++iterations;
}
while (std::abs(x - last) > tolerance && iterations != maxIterations);

return x;
}

double XPowNMinusC(double x, boost::uint32_t n, double c) { return std::pow(x, n) - c; }
double XPowNMinusCPrime(double x, boost::uint32_t n, double c) { return n * std::pow(x, n - 1); }

int main(int argc, char** argv)
{
if (argc != 3)
{
std::cerr << "Usage: " << argv[0] << " n x" << std::endl;
std::cerr << "(Prints the nth root of x)" << std::endl;
return 1;
}

boost::uint32_t n;
double x;

n = atoi(argv[1]);
x = atof(argv[2]);

double nthRootOfX = NewtonsMethod(
boost::bind(XPowNMinusC, _1, n, x), // f
boost::bind(XPowNMinusCPrime, _1, n, x), // f'
x / 2, // x[0]
10E-6, // tolerance
100); // max iterations

std::cout << nthRootOfX << std::endl;

return 0;
}

• 02-19-2011
Elysia
The only thing I can say is avoid atoi (use boost::lexical_cast instead) and I would prefer lambdas over boost::bind (error messages are horrible as usual).
Otherwise it looks like a very nice C++-like solution.
• 02-19-2011
Mozza314
Quote:

Originally Posted by Elysia
The only thing I can say is avoid atoi (use boost::lexical_cast instead) and I would prefer lambdas over boost::bind (error messages are horrible as usual).
Otherwise it looks like a very nice C++-like solution.

Ah yes boost::lexical_cast would be better. I'm not very familiar with C++0x's lambda functions, can they be used in that program without changing NewtonsMethod() ?
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last