An object oriented approach (Newton-Raphson method)

Pages: 12
The main problem is that the declaration of Newton class does not see the declaration of Function class. Either forward declare it or just move the whole class before Newton.
Others : line 14, wrong name of the constructor. line 25 you don't need that first minus. line 34 Function is the class Functionf is the object. You can't pass a class name to something..
Thanks alot. Now it makes sense this is what i've got
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
class Function{
      public:
      Functionf(){};
      virtual ~Function(){};
      virtual double operator()(double x)=0;
      virtual double dfdx(double x)=0;
};

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

      
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 Function;
      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;
      }

It runs well but doesn't return the minimum of the function
I assume it returns a root of the function?
Test it first with something like f = x*x+5*x+6; (and f' = 2*x+5) to know that it really works. (you should get 2 or 3)
Then change f = F' where F = exp(x)-sin(2*x), calculate f' = F'' and then you'll get a critical point. I have no idea how you're going to see if it's a minimum or a maximum though..
Thanks hamsterman. all done cheers
Topic archived. No new replies allowed.
Pages: 12