Why Isn't My Newton-Raphson Algorithm Looping?

  • Thread starter Thread starter ggeo1
  • Start date Start date
  • Tags Tags
    Newton
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Newton-Raphson algorithm implementation that fails to execute its looping structure, preventing it from yielding results. Participants explore potential issues in the code, including variable initialization and loop conditions, while providing suggestions for debugging and code adjustments.

Discussion Character

  • Technical explanation
  • Debugging assistance
  • Exploratory

Main Points Raised

  • One participant reports that the while loop does not execute, suggesting that the values of f1 and fd1 may be smaller than the error tolerance eps before the loop starts.
  • Another participant modifies the loop condition to include absolute values but still encounters the same issue, indicating that the initial values lead to a root of 2.07353e-317 and zero iterations.
  • A suggestion is made to display the values of eps and error before the loop to help diagnose the problem.
  • Concerns are raised about calculating f1 and fd1 before the loop, as these values should be updated in each iteration.
  • One participant proposes moving the update of x1 to the end of the loop to avoid computing an error of zero.
  • Another participant suggests adding diagnostic print statements to track variable changes, particularly for the error term, and questions the use of relative error in the calculation.
  • A later reply mentions that switching to a do-while loop resolved the issue, indicating that the placement of variable calculations was critical.
  • There is a discussion about the importance of using a debugger effectively, with some participants expressing confusion about how to set breakpoints and analyze variable states during execution.

Areas of Agreement / Disagreement

Participants generally agree that the initial values and placement of calculations are likely causing the loop to not execute. However, there is no consensus on the best approach to resolve the issue, as multiple suggestions are offered without a definitive solution being established.

Contextual Notes

Participants note that the issue may stem from the timing of variable updates and the conditions used in the loop. The discussion highlights the importance of understanding how variable states affect loop execution, but specific mathematical or programming corrections remain unresolved.

Who May Find This Useful

This discussion may be useful for individuals working on numerical methods, particularly those implementing the Newton-Raphson algorithm, as well as those interested in debugging techniques in programming.

ggeo1
Messages
61
Reaction score
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
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).
 
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
 
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);
}
 
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).
 
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.
 
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)
 
Analise how your variables are changing, add "count << x1; count << 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).
 
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 "count << x1; count << 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 "count <<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 "count <<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 count 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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K