Oops...I forgot to mention...I have to write a code to solve for this
particular problem...can someone give me a head start? I'm confused.
Appreciate any help
(Please don't top-post - it makes life difficult for people)
This is fairly off-topic here (hence the [OT] I've added in the subject
line), but since you've basically been given the algorithm to solve the
problem, I'll give you the benefit of the doubt and assume that you're
having trouble expressing it in code.
What you seem to need is some advice about problem-solving. The code to
solve this is not especially difficult and you're not required to understand
the algorithm in detail (they've given you everything you need - you don't
even have to differentiate the function).
The way to solve problems is to break them up into pieces - divide and
conquer. Your first step is generally to start with:
int main()
{
return 0;
}
If you can't get this far, then this problem isn't what you're having
difficulty with - you need a book on C++. More importantly, you need to read
it.
The next step is to look at the algorithm you've been given: you start from
an initial value of x and keep iterating it until (hopefully) it converges
to one of the values you're looking for. Ok, so we write a function which
looks like:
double specific_newton_raphson(double x)
{
....
}
This takes an initial value of x (a double) and returns our final
approximation to the root (also a double).
Given that one of the roots is positive and one is negative, you might come
up with the idea of trying 1 and -1 as the initial approximations to find
each of the roots (this might not be right, but it's worth a try and in this
case it works - if it didn't, you could draw the function (perhaps) and make
a more educated guess). So we change our main function as follows:
int main()
{
std::cout << specific_newton_raphson(1.0) << '\n';
std::cout << specific_newton_raphson(-1.0) << '\n';
return 0;
}
This outputs the results of running the iteration starting from 1 and -1,
respectively. So far, so good. Now we actually need to do the iterating
(i.e. fill in the body of the specific_newton_raphson function). I'm not
going to do that bit for you, but I will give you an idea (a pretty good
idea, actually) of how you might go about it:
double specific_newton_raphson(double x)
{
const double EPSILON = 0.0001;
double oldX;
do
{
// store the current value of x in oldX and update x as per the
question
} while(fabs(x - oldX) > EPSILON); // continue until two successive
iterations are close enough together
return x;
}
Hope this helps,
Stu
P.S. You'll need to include iostream and cmath to get this to work.