newton-raphson problem

M

moi

Can someone please help with this problem im having. i have to use the
newton-raphson technique to find the root of a function, in this case X^2 -
1. basically, the program has to read in values of x0, tolerance, and a
boolean as to whether the approximate or exact df/dx is to be used. and its
specified that the function names and their signatures have to be as they
are below. thats where im getting all buggered up really. i totally have
no idea what functions are meant to be returning what and where im going
wrong. and where my "void main()" should be and what should be in it. i
think the scope of my functions is buggering me up too but as you can see im
in a pickle.

any help would be most gratefully recieved

-------------------------


// newton-raphson.cpp
// determines the root of a function f(x) using the
// Newton-Raphson method.

#include<iostream.h> // c++ I/O
#include<math.h> // standard mathematical functions
#include<conio.h> // getche()




double Function(double x){
return (pow(x, 2) - 1);
}

double FirstDerivative(double x){
return 2*x;
}

double ApproximateFirstDerivative(double x, const double h = 1e-03){
return ((pow(x + h, 2) - pow(x - h, 2))/2);
}

double FirstOrderNewtonRaphson(double x0, bool exactDerivative, int
max_iterations, int& iterations_to_converge, bool& converged, double
tolerance){

double xold, xnew = 0;

cout << "Please enter the value of x at which the root is required: ";
cin >> x0;

cout << "Please specify whether the exact derivative is to be used [1 =
yes, 0 = no]: ";
cin >> exactDerivative;

cout << "Please enter the max. number of iterations: ";
cin >> max_iterations;

cout << "Please enter the tolerance (%): ";
cin >> tolerance;

if (exactDerivative == 1)
{
for (int i = 0; i < 500; i++)
{
xnew = xold + Function(xold)/FirstDerivative(xold);
xold = xnew;
if (fabs((xnew-x0/x0)*100) < tolerance)
{
converged = 1;
iterations_to_converge = i;
break;
}
}
else if (exactDerivative == 0)
for (int i = 0; i < 500; i++)
{
xnew = xold + Function(xold)/ApproximateFirstDerivative(xold);
xold = xnew;
if (fabs((xnew-x0/x0)*100) < tolerance)
{
converged = 1;
iterations_to_converge = i;
break;
}
}
}

cout << "The answer is: " << xnew << endl;
if (converged == 0)
{
cout << "The function failed to converge." << endl;
else
cout << "The function converged after " << iterations_to_converge << "
iterations." << endl;
}
}

void main(){
FirstOrderNewtonRaphson();
}
 
V

Victor Bazarov

moi said:
Can someone please help with this problem im having. i have to use the
newton-raphson technique to find the root of a function, in this case X^2 -
1. basically, the program has to read in values of x0, tolerance, and a
boolean as to whether the approximate or exact df/dx is to be used. and its
specified that the function names and their signatures have to be as they
are below. thats where im getting all buggered up really. i totally have
no idea what functions are meant to be returning what and where im going
wrong. and where my "void main()" should be and what should be in it. i
think the scope of my functions is buggering me up too but as you can see im
in a pickle.

any help would be most gratefully recieved

-------------------------


// newton-raphson.cpp
// determines the root of a function f(x) using the
// Newton-Raphson method.

#include<iostream.h> // c++ I/O
#include<math.h> // standard mathematical functions
#include<conio.h> // getche()




double Function(double x){
return (pow(x, 2) - 1);
}

double FirstDerivative(double x){
return 2*x;
}

double ApproximateFirstDerivative(double x, const double h = 1e-03){
return ((pow(x + h, 2) - pow(x - h, 2))/2);
}

double FirstOrderNewtonRaphson(double x0, bool exactDerivative, int
max_iterations, int& iterations_to_converge, bool& converged, double
tolerance){

double xold, xnew = 0;

cout << "Please enter the value of x at which the root is required: ";
cin >> x0;

cout << "Please specify whether the exact derivative is to be used [1 =
yes, 0 = no]: ";
cin >> exactDerivative;

cout << "Please enter the max. number of iterations: ";
cin >> max_iterations;

cout << "Please enter the tolerance (%): ";
cin >> tolerance;

if (exactDerivative == 1)
{
for (int i = 0; i < 500; i++)
{
xnew = xold + Function(xold)/FirstDerivative(xold);

Shouldn't this be

xnew = xold - Function(xold)/FirstDerivative(xold);

???
xold = xnew;
if (fabs((xnew-x0/x0)*100) < tolerance)
{
converged = 1;
iterations_to_converge = i;
break;
}
}
else if (exactDerivative == 0)
for (int i = 0; i < 500; i++)
{
xnew = xold + Function(xold)/ApproximateFirstDerivative(xold);

Shouldn't this be

xnew = xold - Function(xold)/ApproximateFirstDerivative(xold);

???
 
R

Rajeev Ayyagari

Hi,

Most of the bugs in the program aren't language-related.
double ApproximateFirstDerivative(double x, const double h = 1e-03){
return ((pow(x + h, 2) - pow(x - h, 2))/2);
}

There's a bug in the formula used in the above function.
if (exactDerivative == 1)
{
for (int i = 0; i < 500; i++)
{
xnew = xold + Function(xold)/FirstDerivative(xold);

Your Newton-Raphson formula is incorrect.
else if (exactDerivative == 0)
for (int i = 0; i < 500; i++)
{
xnew = xold + Function(xold)/ApproximateFirstDerivative(xold);

Newton-Raphson formula incorrect here too.
void main(){
FirstOrderNewtonRaphson();
}

main() can't return void; it has to return int.

HTH,
Rajeev.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top