//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
@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().
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?
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.
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..
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