newton raphson method high level design

Hello,

I was asked to create a c++ design of a newton-raphson method using object oriented programming.
I need to make only a design of classes like parameters variable function(declarations) parameter list etc.
The actual question is to read a polynomial from keyboard, initial guess, accuracy of(up to how much decimal digits) and using newton method to solve it. after each time when program solve polynomials ask user to continue if he wants. Each time as it solved i need to write my root in a queue. Than print it i a file.

1)Actually, i decide to have a object called polynomial which will hold the confection and a power of each of X.
2)And object queue which will hold array of solutions and some operations to work with polynomial.

What i ask is how best represent this problem and am i gong in right detection of solving this problem. And also some operations and variable that i need to use in classes?

Remember i only need to write a high-level design which mean no actual coding but only declaration of class and function names and parameter lists, variable etc.

What should i account in my design?
I referred wikipedia for the Newton-Raphson method. You should create a class called 'PolynomialTerm' and 'DerivativeTerm'. Both the class should have data members ' coefficient' and 'power'. Then ask the user to enter the number of terms in the polynomial. Take that number and create two arrays or vectors 'polynomial' and its 'derivative'. The class DerivativeTerm should have a member function which will accept a polynomial term, find its derivative and assign it to its object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class PolynomialTerm
{
    private:
        int power,coefficient,value;
    public:
        void GetCoefficent()
        {
            //Get the coefficient of the polynomial term
            //and assign it to coefficient
        }
        void GetPower()
        {
            //Get the power of the polynomial term
            //and assign it to power
        }
        int Coefficient()
        {
            return coefficient;
        }
        int Power()
        {
            return power;
        }
        int ValueOfTerm()
        {
            //Find value of the term when you
            //want to find value of f(x) for the guess value
            return value;
        }
};
class DerivativeTerm
{
    private:
        int power,coefficient,value;
    public:
        void FindDerivative(PolynomialTerm a)
        {
            if(PolynomialTerm.Power()=0) //Check if the term is a constant, then derivative is zero!
            {
                power=0;
                coefficient=0;
            }
            else
            {
                coefficient=PolynomialTerm.Coefficient()*PolynomialTerm.Power(); //Since f'(x) of x^n is n*x^(n-1)
                power=PolynomialTerm.Power() - 1;                                //
            }
        }
        int ValueOfTerm()
        {
            //Find value of the term when you
            //want to find value of f'(x) for the guess value
            return value;
        }
};


You can even derive the class DerivativeTerm from PolynomialTerm
I hope that helps.
Last edited on
I think you only need 1 Term class

1
2
3
4
5
6
7
8
9
10
11
class Term
{
public:
    Term(...);    // Constructor depends on parsing strategy
    Term getDerivative();
    double getYValue(double xValue);

private:
    double coefficient;
    double power;
};


Coefficient and power need to be doubles rather than ints (You might get away with power being an int depending on what polynomials you allow).

getYValue calculates the y Value of the term at a given x Value

The constructor might take a coefficient and a power or a string representing the term depending on the parsing strategy you take. You should discuss how you plan to parse user input in your high level design.

You also need a Function class

1
2
3
4
5
6
7
8
class Function
{
public:
    Function getDerivative();
    double getYValue(double xValue);
private:
    std::vector<Term> terms;
};


The getDerivative function will return f' of the function object. This function will call getDerivative on each of its terms and add the results to the new function to return.

The getYValue function will return the value of the function at the given X value. This function will call getYValue on each of its terms and sum the results.

Depending on your parsing strategy, the Function class will need a constructor that takes a string representation of the polynomial or an "addTerm" function (either taking a Term object or coefficient and power. That's up to you.
Topic archived. No new replies allowed.