An object oriented approach (Newton-Raphson method)

Pages: 12
#include <cstdlib>
#include <iostream>

//Where do I go from here?
//Given that f(x)= exp(x)-sin(2x). Use a Newton object to find the minimum of //the function from the class declarations bellow:-

using namespace std;

class Newton{
protected:
double precision;
public:
Newton(double p):precision(p){};
virtual ~Newton(){};
double Find(Function& f, double start){};//Does the actual
//iteration for a particular function f starting from a value start f is //declared as(a reference to) an object of the abstract Function class.

};

class Function{
public:
Function(){}
return f;
}
virtual ~Function(){};
virtual double operator()(double x)=0;//operator() does the function //evaluation for a particular value x and dfdx evaluates its derivative

virtual double dfdx(double x)=0;
};

virtual double operator()(double x){
return exp(x)-sin(2*x);
}
virtual double dfdx(double x){
return (exp(x)-cos(2*x)*2.0;
}


int main()
{



system("PAUSE");
return EXIT_SUCCESS;
}
http://en.wikipedia.org/wiki/Newton's_method
This says that Newton's method is for finding roots, not minimums.

The algorithm would be, I suppose
1
2
3
while(abs( f(start) ) > precision){
   start = start - f(start)/f.dfdx(start);
}
It also says that the minimum or maximum can be found when f2 = f1 - f'(x)/f"(x).
So where does
1
2
3
while(abs( f(start) ) > precision){
   start = start - f(start)/f.dfdx(start);
}
go in the class. Should the function have a return type?

This is how i have gone so far:-
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
56
57
#include <cstdlib>
#include <iostream>

using namespace std;

class Newton{
      protected:
                
      double precision;
      
      public:
      Newton(double p):precision(p){};
      virtual ~Newton(){};
      double Find(Function& f, double start){}
      
};

class Function{
      public:
      Function(){}
      return f;
      }
      virtual ~Function(){};
      virtual double operator()(double x)=0;
      virtual double dfdx(double x)=0;
};

double Newton::Find( Fuction& f, double start){
       while(abs( f(start) ) > precision){
       start = start - f(start)/f.dfdx(start);
       }
       }
virtual double operator()(double x){
      return exp(x)-sin(2x);
      }
virtual double dfdx(double x){
      dfdx = (exp(x)-cos(2*x)*2.0;
      d2fdx2 = exp(x) + sin(2x)*4.0;
      return d2fdx2;
      }
      

int main()
{
    
// In the main fuction, I need a Newton object 
// to find the minimum of the function
// I was suggesting

   double precision = 5; 
   Newton(precision);
   double minimum = precesion-df(precision-1)/dfdx(precesion-1);
   cout<<minimum<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

@hamsterman:
Newton's method can be used to approximate the local min/max of a function by finding the zeros of the derivative function.
@Lane:
You spelled your variable wrong twice on line 52. Also, you need to include cmath to get some of the functions you're using like sin() and cos().
yes. minimum (or a maximum or inflection point) of f is the root of f'. you don't have f'' though.
my code is for Find.
So to initialize find we have:-
1
2
3
4
5
double Newton::Find( Fuction& f, double start){
       while(abs( f(start) ) > precision){
       start = start - f(start)/f.dfdx(start);
       }
       }

do we need a return type here?

what about virtual double operator()(double x)

Here is my implementation for the main to find the minimum. Does it make sense?
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    
// In the main fuction, we need a Newton object 
// to find the minimum of the function
// I was suggesting

       double precision; 
       Newton(5.5);
       double minimum = precision-df(precision-1)/dfdx(precision-1);
       cout<<minimum<<endl;

double in double Newton::Find( Fuction& f, double start){ says that Find should return a double. I hope you can figure out what it is..

Your operator() is ok. Your dfdx, not so much. line 37 doesn't do anything at all. both line 37 and 38 use undeclared variables. there is no reason not to write what you had in the first post: return exp(x)-cos(2*x)*2.0;

your main is not right. it was said that Find() is supposed to do all the work, so maybe call it?
Does it mean that
1
2
3
4
5
double Newton::Find( Fuction& f, double start){
       while(abs( f(start) ) > precision){
       start = start - f(start)/f.dfdx(start);
       }
       }
returns f or f()?

Is it right when I say that
1
2
3
4
       double f; 
       Find( 0.5, 0.2);
       double minimum = precision- dfdx(precision-1)/dfdx*dfdx(precision-1);
       cout<<minimum<<endl;
in the main?
This doesn't make sense does it?
1
2
3
4
double f; 
       Find( 0.5, 0.2);
       double minimum = precision- dfdx(precision-1)/dfdx*dfdx(precision-1);
       cout<<minimum<<endl;
Who remembers Newton-Raphson method?
do you really understand the method itself?
after repeating x = x-f(x)/f'(x) several times, what variable is the root? well, Find has to return it.

your main doesn't make much sense. Find was said to do the whole thing, so why line 3? also it returns a variable, which is the actual root. maybe you should print that?
hamsterman this class is confusing. i understand Newton-Raphson method mathematically but not c++ly (if you understand what i mean). fist of all i want to understand what to put in the main so i can get the minimum.
in the main you put cout << Find(/*arguments*/); and nothing else.
Last edited on
then double Find(Function& f, double start); and virtual double dfdx(double x)will be overloaded
Whoops. Ignore my previous post..

Firstly you have to declare a class derived from Function and implement operator() and dfdx()
1
2
3
4
5
6
7
8
struct F : Function{
    double operator(double x){
        return exp(x)-sin(2*x);
    }
    double dfdx(double x){
        return exp(x)-cos(2*x)*2.0;
    }
}


Then in main,
1
2
3
F func;//create function object
Newton n(/*precision goes here*/);//create newton object
cout << n.Find(func, /*start goes here*/);


You almost have Newton::Find, you just have to figure out, what to return.

Though, seriously, have you been to any lectures at all? This task uses inheritance and polymorphism, while you're having difficulties with more simple things..

Anyway, sorry about the last post.
thats fine what about this one?
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
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

class Newton{
      
      protected:
      double precision;
      
      public:
      Newton(double p):precision(p){};
      virtual ~Newton(){};
      double Find(Function& f, double start);
      
};

class Function: public Newton{
      public:
      Function(){};
      virtual ~Function(){};
      virtual double operator()(double x)=0;
      virtual double dfdx(double x)=0;

double Newton::Find( Fuction& f, double start){
       while(abs( f(start) ) > precision){
       start = start - f(start)/f.dfdx(start);
       }
       return Find();
       }

      virtual double operator()(double x){
      return exp(x)-sin(2*x);
      }
      virtual double dfdx(double x){
      return exp(x)-cos(2*x)*2.0;

      }

};


int main()
{
      F func;
      Newton n(5.0);
      cout << n.Find(func, 2.0)<<endl;

    
    system("PAUSE");
    return EXIT_SUCCESS;
}
My problem is with the function double Find(Function& f, double start);

if i call it
1
2
3
4
5
6
double Newton::Find(Function& f, double start){
      while(abs( f(start) ) > precision){
      start = start - f(start)/f.dfdx(start);
      }
      return start;
      }

The comipler does not recognise it as a member function when it is indeed
Why are you defining Newton::Find inside the Function class ?
i was told to define it that way.
Guys can we trouble shoot this? its now my third day on it. This is what i have so far with your help. However if I compile it, the compiler says "Find" is not a member function even though it is. I totally don't understand why
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
class Newton{
      
      protected:
      double precision;
      
      public:
      Newton(double p):precision(p){};
      virtual ~Newton(){};
      double Find(Function& f, double start);
};

class Function{
      public:
      Functionf(){};
      virtual ~Function(){};
      virtual double operator()(double x)=0;
      virtual double dfdx(double x)=0;
};
      
class f : public Function{
      double operator()(double x){
      return (exp(x)-sin(2*x));
      }
      double dfdx(double x){
      return (-exp(x)-cos(2*x)*2.0);
      }

};

int main()
{
      f Functionf;
      Newton n(5.0);
      cout << n.Find(Function, 2.0)<<endl;

    
      system("PAUSE");
      return EXIT_SUCCESS;
}

      
double Newton:: Find(Function& f, double start){
      while(fabs( f(start) ) > precision){
      start = start - f(start)/f.dfdx(start);
      }
      return start;
      }
Pages: 12