0

This is my code:

void Newton(double a,double b,double eps, double (*func)(double,double))
{
    double root;
    double x,y;
    x=a; y=b; root=y-func(y,0.5)/((func(y,0.5)-func(x,0.5))/(y-x));
    double i;
    int n=(15-0.5)*10+1;
    for(i=0;i<n;i++)
    {
        while(fabs(y-x)>eps)
        {
            x=y;
            y=root;
            root=y-func(y,((double)i/10+0.6))/((func(y,((double)i/10+0.6))-func(x,((double)i/10+0.6)))/(y-x));
        }
        printf("The root computed by the Newton's method for w=%lf is %lf\n",((double)i/10+0.5),root);
    }
}

It is only the function computing and printing roots calculated by the Newton's method of some arbitrary equation which is stated in the function 'func'. The result printed is always the same. It should differ as long as the factors in the equation change. My function is cos(x)-w*t, where w are numbers {0.5,0.6,...,15.0}. a and b are my initial arguments for searching the root, eps is some arbitrarily small number which is the accuracy here.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 1
    Show how you call it. – Eugene Sh. Mar 09 '16 at 21:58
  • 1
    What are the values you are putting in for `a`, `b`, and `eps`? – callyalater Mar 09 '16 at 22:08
  • Could you also post your actual function that is being passed in. `cos(x)-w*t` seems to take 3 parameters, but only two are provided. Is `t` supposed to be `x`? If your function uses `t` but it is set to 0, then you will always get the same answer. If `t` and `w` are constants, then the root will always be a shifted offset of just `cos(x)`. There are so many unknowns in your code that it is hard to know where to begin. If you could include a complete, compilable example, that would help determine the problem. – callyalater Mar 09 '16 at 22:30

0 Answers0