Thread: Newton's Method

  1. #16
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    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..

  2. #17
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by hottiefee View Post
    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();
    Last edited by nimitzhunter; 02-14-2011 at 08:15 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #18
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    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.

  4. #19
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Use fixed along with setpricision.
    Code:
    cout<< fixed << setprecison(6).
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #20
    Registered User hottiefee's Avatar
    Join Date
    Oct 2010
    Location
    TN, USA
    Posts
    32
    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

  6. #21
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    No problem.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  7. #22
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by nimitzhunter View Post
    Use fixed along with setpricision.
    Code:
    cout<< fixed << setprecison(6).
    I believe you will also need the library,
    Code:
    #include <iomanip>
    to use setprecision.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    #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++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    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;
    }

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What? You invented your own sqrt or something?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by Elysia View Post
    What? You invented your own sqrt or something?
    Using Newton's Method, yes i did.

  12. #27
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Elysia View Post
    #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.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  13. #28
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    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;
    }

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Elysia View Post
    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() ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newton's Method for Systems of Nonlinear Equations
    By Delia in forum C Programming
    Replies: 1
    Last Post: 11-10-2010, 07:17 PM
  2. Packet Container Class
    By ChaoticXSinZ in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2010, 12:07 AM
  3. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  4. Cubic Root by Newton's Iterative Method
    By amirahasanen1 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2005, 02:05 PM
  5. Newton's method
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 10-19-2004, 11:02 AM

Tags for this Thread