473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

newton-raphson problem

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();
}


Jul 19 '05 #1
2 16134
"moi" <ra****************@blueyonder.co.uk> wrote...
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);

???
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();
}

Jul 19 '05 #2
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.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: hills | last post by:
Please reply to hdgbyi@public.guangzhou.gd.cn. Thank you! The limitation of the Photon Hypothesis According to the electromagnetic theory of light, its energy is related to the amplitude...
80
by: Neal | last post by:
Been searching around, and found http://www.w3.org/WAI/ER/IG/ert/iso639.htm which is great, as I've been looking for a guide to what codes are acceptable. I see stuff like lang="en-us" - that...
34
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream>...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
1
by: Luca | last post by:
Hi guys, I am trying to find a way to solve equations using Netwon Rap. method inside Visual Studio. Is there any body that is aware of that? Thanks in advance Luca
7
by: sptutx | last post by:
Write a C program that uses Newton's Method to solve an equation in one variable. Try solving x^x = ln2 the deriviative of x^x is x^x(lnx + 1). The 'ln' function in C is log(), and the...
0
by: capes | last post by:
have functions in my cell model calling CVODE for each time step. When I build the application I get a number of link errors: \cardiacsimulator.o Release\ccardiaccell.o Release\ccelldata.o...
92
by: ureuffyrtu955 | last post by:
Python is a good programming language, but "Python" is not a good name. First, python also means snake, Monty Python. If we search "python" in google, emule, many results are not programming...
30
by: luvraghu | last post by:
Hi, Can anyone please give me a hint/logic to divide a number with any number without using '/' '+' '*' '-'. Thank You. Regards, New
2
by: ilovecolours | last post by:
hi everyone can u please help me to explain newtons method to minimize logarithmic ( y=a- b (ln x) and parabolic equation? and pleass post its program also.. thank you in advance...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.