Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Newtons method

Name: Anonymous 2012-02-08 14:56

So guys I have myself another c++ assignment that is over my head as usual, can i get some pointers or help on how to start this one? All the data is below

One of the most powerful methods used to find the roots of an equation of the form, f(x) = 0, is Newton's method. The procedure is to repeatedly compute a new estimate of the root, y, using a previous estimate of the root, x, and the following computation: y = x - f(x)/f'(x), where f'(x) is the derivative of f(x). If this is repeated often enough (each time replacing x with the new estimate, y), and the initial estimate of the root is close enough to the actual root, then the values of x and y will converge to each other and the root. Two methods are used to stop the iteration process: when the absolute value of the difference between x and y is less than some specified tolerance, when the number of iterations reaches a specified maximum number, maxIterations.

In this assignment you will create a C++ program that implements the function described below. We have provided the following files:

    assign4.h
        This file contains the declarations for the function you are to implement (newton) as well as the functions to be used (eff and effPrime), which are defined in effExp.cpp.
    effExp.cpp
        This file contains the definitions of the functions to be used (eff and effPrime) by your newton function.

The function you are to implement finds a root of the given function eff(x) using Newton's method. The given version of eff(x) implements f(x) = x^2e^-x -2 (where e is the base of the natual logarithm) and effPrime(x) implements its derrivative, but your code could be used to find a root of other functions by substituting different implementations of eff and effPrime. Your program should use both of the above techniques to stop iteration (i.e., it should stop when either condition is satisfied).

The function you are to implement is as follows:

    double newton(double x, double tol, int maxIt)
    Finds a root of eff using Newton's method starting at x and stoping when successive approximations are within tol of each other or maxIt iterations have occured.

Sample Data

x = -1.5, tol = 0.0001, maxIt = 10 should result in -0.901201.

thanks guys for any help!

Name: Anonymous 2012-02-08 14:58

Newton's method isn't the most optimum method for finding polynomial roots

Name: op 2012-02-08 14:59

your right, but i dont have a choice :/, whatever the assignment states i have to do, i have to do it

Name: Anonymous 2012-02-08 16:29


float polyRootNewton(vector<float>* coefficients, int degree, float yourGuess)
{
    int i, j=coefficients->size();
    float ans, numerator, denominator;
    numerator=evaluatePoly(coefficients, degree, yourGuess); /* evaluatePoly should be set up elsewhere;
                                                                 it should just plug in yourGuess for the variable
                                                                 and return a float with the evaluation */
    denominator=evaluateDerivative(coefficients, degree, yourGuess);
    ans=numerator/denominator;
    do
    {
        polyRootNewton(coefficients, degree, ans);
    } while (1); //replace 1 with some condition that tests for 'the level of certainty' (i.e., how many times you get the same answer)
    return ans;
}


Probably shit. I'm not going to finish it.

Name: op 2012-02-09 16:13

haha ill take it!

Name: Anonymous 2012-02-09 16:27

A quick search shows you've asked this question on "Yahoo! Answers".

My answer? Read your SICP, really, it shows how to solve this in an example.

Name: Anonymous 2012-02-09 17:41

Name: op 2012-02-10 10:08

so after a bit of research and work i have


double newton(double x, double tol, int maxIt) {
    double lastGuess;
    double deltaG;
    double result;

    do {
    lastGuess = x;
    deltaG = lastGuess-x;
    result = x - (eff)/(effPrime) ;
    x = ((lastGuess + result)/2) ;
    while (fabs(deltaG) > tol);
    return x;
    }

but im getting mass amounts of syntax errors, how do i clean this up?

Name: Anonymous 2012-02-10 10:38

You have a do loop without the while clause at the end.

do
{
    // Stuff
}
while (!opIsAFag);

Name: Anonymous 2012-02-10 10:42

>>9
Scratch that, your code was just so hard to read it obscured the while. Use code tags next time.

Your problem could be that your do loop does not seem to have a closing curly bracket.


    do
    {
        lastGuess = x;
        deltaG = lastGuess-x;
        result = x - (eff)/(effPrime) ;
        x = ((lastGuess + result)/2) ;
    }
    while (fabs(deltaG) > tol);


Try replacing your do loop with that and see if it works.

As an aside, I can tell you're a beginner due to your extremely poor indentation style. I recommend you work on it to avoid mistakes like this in the future.

Name: op 2012-02-10 10:56

haha yeah i am... but it works, thanks!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List