Newton raphson ,have a problem

  • Thread starter ggeo1
  • Start date
  • Tags
    Newton
In summary, the user is trying to use a Newton algorithm to find the root of a function. The problem is that it isn't making the loop and of course not giving a result.
  • #1
ggeo1
63
0
Hello , i have a Newton raphson algorithm.
The problem is that it isn't making the loop and of course not giving a result.

I tried two cases.
One with " if ((f1>eps) && (fd1>eps)) {
while ((error>eps) "

and one instead of the above " while ((error>eps) && (fd1>eps) && (f1>eps)) "

but nothing works.

Any suggestions?

Code:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>



using namespace std;

double function(double); // declare of function
double fderivative(double);//declare of function
double error=10.0;//initial value to error in order to compare with tolerance
double eps=1e-6; //my error tolerance

int main()
{

    double x1,x2,f1,f2,fd1,error;

    cout << "Give x1 , x(-4,-3) : "<<endl;
    cin >>x1;

    int i=0;

   f1=function(x1);
   f2=function(x2);
   fd1=fderivative(x1);


   cout << "The function f(x1)= " << f1 << endl;
   cout << "The function f'(x1)= " << fd1 <<endl;


    while ((error>eps) && (fd1>eps) && (f1>eps))

     {

         x2=x1 - (f1/fd1);
         x1=x2;

         error = fabs((x1-x2)/x1);
         i++;

        cout << "\n f(x2) is : " << f2 << "\t and the root is : " << x2<<endl;

     }

     cout << "\nThe root is : " <<x2 <<"\tand the number of iterations was :" << i <<endl;

  return 0;
}

    double function(double x) // my function

    {
        double y;
        y =exp(x)-sin(x);
        return y;
    }


double fderivative(double x)
{
    double y;
    y=exp(x)-cos(x);
    return y;
}
 
Technology news on Phys.org
  • #2
By "not making the loop" I assume that you mean the while loop isn't executing at all. What are the values of f1 and fd1 before the loop starts? Apparently one or both of those is smaller than eps. That's the only reason your loop wouldn't execute (since error has an initial value of 10.0).
 
  • #3
Hello ,
I changed to this :
while ((error>eps) && (fabs(fd1)>eps) && (fabs(f1)>eps))
But still the same.
If i enter initial value " -4 " it gives :

Code:
The function f(x1)= -0.738487
The function f'(x1)= 0.671959

The root is : 2.07353e-317      and the number of iterations was :0
 
  • #4
I don't see anything that would cause your while loop not to execute at least once, but something must be causing this.

Up where you display f(x1) and f'(x1), add in two more lines to display eps and error.

One thing I noticed that is not the source of your present problem, but will cause problems later, is that you're calculating f(x1) and f'(x1) before the loop starts. These values will need to be calculated once per loop iteration, otherwise you will always be using the same values for them.

Since you have changed your code, please include all of it.

One other minor thing: you could shorten your two evaluation functions by doing this.
Code:
double function(double x) // my function
{
     return exp(x)-sin(x);
}
 
  • #5
I can't understand why its not working.

Anyway,i will see it again these days and if i will figure sth i will post.

As for the advices ,thank you very much.

One other minor thing: you could shorten your two evaluation functions by doing this.

I prefer to have a value y to return.It seems better (for me).
 
  • #6
ggeo1 - look at the top of the while loop:

Code:
         x2=x1 - (f1/fd1);
         x1=x2;


Your updating the swap variable (x1 = x2) right after the Newton update - your compare afterwards will compute an error of zero.

Try moving the second statement to the end of the while loop.
 
  • #7
Hello ,thans for helping.

Your updating the swap variable (x1 = x2) right after the Newton update - your compare afterwards will compute an error of zero

I can't understand you.I give the x1 ,then compute the x2=x1 -f1/fd1 and then update x1-x2 in order to execute the loop.

Try moving the second statement to the end of the while loop.
I tried it but still the same.(But i disagree with that like i said above)
 
  • #8
Analise how your variables are changing, add "cout << x1; cout << x2;" before the code quoted by TheoMcCloskey, inside, and after. Or learn how to use debugger, but I think you were already told that (and obviously ignored).
 
  • #9
do a diagostic print out of your error term. You may find a problem there.

for the error, I would just compute the difference of x1 and x2, not sure why you divide by x1.
 
  • #10
Analise how your variables are changing, add "cout << x1; cout << x2;" before the code quoted by TheoMcCloskey, inside, and after. Or learn how to use debugger, but I think you were already told that (and obviously ignored).

I did as you say but it doesn't print anything.
I didn't ignored to use the debugger,i forgot to say that the debugger says "program exited normally" and it doesn't show anything else.

do a diagostic print out of your error term. You may find a problem there

You mean "cout <<error" ?Because it doesn't print anything too as far as the loop doesn't execute.

for the error, I would just compute the difference of x1 and x2, not sure why you divide by x1.

The error is the relative error that's why i have like this.
 
  • #11
ggeo1 said:
I didn't ignored to use the debugger,i forgot to say that the debugger says "program exited normally" and it doesn't show anything else.
You need to set one or more breakpoints in your program so that it stops temporarily. When the program stops, the debugger should show you the values of all your variables.
ggeo1 said:
You mean "cout <<error" ?Because it doesn't print anything too as far as the loop doesn't execute.
Since your loop isn't executing, that's why it's important to learn to use the debugger and/or put in extra cout statements above the start of the while loop.
ggeo1 said:
The error is the relative error that's why i have like this.
Which doesn't make any sense. You don't want the relative error. What you want is |f(x2)|.
 
  • #12
Ok i will check it and i will be back tomorrow.

Thanks!
 
  • #13
Also, for future reference, problems such as these should be posted in the Homework & Coursework section, under Engineering & Computer Science.
 
  • #14
Hello ,
Finally the solution find . A guy helped me and my new code now seems below.
The thing that was changed was the while loop .

The problem was that i had f1,fd1,f2 outside the loop.But it didn't work again ,so i used " do - while" and then it worked!

So,there are two solutions .
One , using

Code:
   do {
...} while ((error>eps) && (fabs(fd1)>eps) && (fabs(f1)>eps));

and the other as the code below.

I
You need to set one or more breakpoints in your program so that it stops temporarily. When the program stops, the debugger should show you the values of all your variables.

I tried to set break points (you mean use the command "break") but the debugger said "program exits normally"
Anyway , when i find time i will learn how to use the debugger.

Thank you all for your help.I learned a few things more.
 
  • #15
ggeo1 said:
I tried to set break points (you mean use the command "break") but the debugger said "program exits normally"
Anyway , when i find time i will learn how to use the debugger.
No, debugger break points have nothing to do with the C/C++ break statement. When you set a debugger breakpoint and start the debugger, it will stop at the line of code where you set the breakpoint. You can then find out the values of variables, single-step through your code, and find out all sorts of things.

You should make it a priority to learn how to use whatever debugger you have available. If you have more programming assignments, any time you spend learning to use the debugger will be more than offset by the time you save in trying to get your programs to run.

Had you known how to use a debugger, you probably would have figured out why your while loop was behaving as it was.
 
  • #16
Ok ,thanks for the advice.
I will search for a manual.
 

Related to Newton raphson ,have a problem

1. What is the Newton-Raphson method?

The Newton-Raphson method is an iterative numerical method used to find the roots of a given function. It is also known as the Newton's method and is based on the idea of approximating a root by using the tangent line of the function at a given point.

2. How does the Newton-Raphson method work?

The Newton-Raphson method starts with an initial guess for the root of the function. It then uses the slope of the tangent line at that point to find a better approximation of the root. This process is repeated until the desired level of accuracy is achieved.

3. What are the advantages of using the Newton-Raphson method?

The Newton-Raphson method is a fast and efficient way to find the roots of a function. It also has a high convergence rate, meaning it can find the root with a small number of iterations. Additionally, it can be applied to a wide range of functions.

4. What are the limitations of the Newton-Raphson method?

The Newton-Raphson method may fail to converge if the initial guess is far from the actual root or if there are multiple roots close together. It also requires knowledge of the derivative of the function, which may not always be easy to obtain.

5. How is the Newton-Raphson method used to solve problems?

The Newton-Raphson method is commonly used in various fields such as engineering, physics, and economics to find solutions to equations or systems of equations. It is particularly useful in finding the critical points of a function, which are important in optimization problems.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
23
Views
6K
Replies
10
Views
1K
Back
Top