1

I need to find a root of arctan(x-e) using Newton method and prove that exists such "a" for which if |x-e|<a method converges and if |x-e|>a method diverges,then derive the equation to find this "a" and solve it.I wrote a programm,but don't understand how to find this "a".

#include <stdio.h>
#include <math.h>
double f(double x) ;
double fd(double x) ;
double newton(double x,double eps);
#define e 2.71828182845904523
int main() 
{
   double x,eps=1.e-16 ;
   printf("Enter x :") ;
   scanf("%le",&x) ; 
   printf("%le",newton(x,eps)) ;
   
    return 0;
}


double f(double x)
{
   double z ;
   z=atan(x-e);
   return z ; 
}

double fd(double x)
{
   double z ;
   z=1/((x-e)*(x-e)+1);
   return z ; 
}



double newton(double x,double eps)
{
  double x1 ;   
  
  while(1)
  {
    x1=x-f(x)/fd(x) ;
    if(fabs(x1-x)<eps) return x1 ; 
    x=x1 ;
  }
  
  return x1 ; 
}

3 Answers3

0

double x in C mean just avance the stack on the memory. If you don't fill it with 0's it will just take the value memory under x.

One amelioration : double x = 0 or to the value you want. It will fill the 8 words (1 word = 8 bits) to 0x00000000 in stead of some random data like 0x2409caf42 or idk what ever there is

K V
  • 61
  • 8
0

f(x)/df(x) minimize a function. But if you minimize the derivative you find a root f in facte.

df(x)/ddf(x).

  • if you use f(x)/df(x), the most simple way is Gradient descent : x -= 0.001*f(x)/df(x)
K V
  • 61
  • 8
0

prove that exists such a for which if |x-e|<a method converges

Newton's method works when the approximate solution is near the correct solution.

Newton's method fails when the candidate solution diverges or oscillates.

Add divergence tests (example: a becomes a not-a-number or infinity) and a loop iteration limit.

Use a binary search to find the upper bound for a: between a near solution (a = e) and something that diverges (a = e + 100). See below.

Repeat (with code adjustments to a_min, a_max roles and initial values) to find the lower bound for a. Not shown, left for OP to find. (I found the low side a to be in the 1.0 to 1.5 range)

int main() {
  double x, eps = 1.e-16;
  printf("Enter x :\n");
  //scanf("%le", &x);
  x = e*1.0001;
  printf("%le\n", newton(x, eps));

  double a_min = e;
  double a_max = e + 100.0;
  double a;
  while (1) {
    a = a_min + (a_max - a_min)/2;
    if (a == a_min) break;
    else if (a == a_max) break;
    if (a_min >= a_max) break;
    printf("a_min:%20e a_max:%20e a:%20e\n", a_min, a_max, a);
    if (isnan(newton(a, eps))) {
      a_max = a;
    } else {
      a_min = a;
    }
  }
  printf("a high side:%e\n", a);

  return 0;
}

double newton(double x, double eps) {
  double x1;

  int i;
  for (i=10; i>0; i--) {
    //printf("%2d %20e %20e %20e\n", i, x, fd(x), f(x));
    if (isnan(x)) return x;

    x1 = x - f(x) / fd(x);
    if (fabs(x1-x) < eps)
      return x1;
    x = x1;
  }
  return 0.0/0.0;
}

Result

Enter x :
2.718282e+00
a_min:        2.718282e+00 a_max:        1.027183e+02 a:        5.271828e+01
a_min:        2.718282e+00 a_max:        5.271828e+01 a:        2.771828e+01
a_min:        2.718282e+00 a_max:        2.771828e+01 a:        1.521828e+01
a_min:        2.718282e+00 a_max:        1.521828e+01 a:        8.968282e+00
a_min:        2.718282e+00 a_max:        8.968282e+00 a:        5.843282e+00
a_min:        2.718282e+00 a_max:        5.843282e+00 a:        4.280782e+00
a_min:        2.718282e+00 a_max:        4.280782e+00 a:        3.499532e+00
a_min:        3.499532e+00 a_max:        4.280782e+00 a:        3.890157e+00
a_min:        3.890157e+00 a_max:        4.280782e+00 a:        4.085469e+00
a_min:        4.085469e+00 a_max:        4.280782e+00 a:        4.183126e+00
a_min:        4.085469e+00 a_max:        4.183126e+00 a:        4.134297e+00
a_min:        4.085469e+00 a_max:        4.134297e+00 a:        4.109883e+00
...
a_min:        4.104323e+00 a_max:        4.104323e+00 a:        4.104323e+00
a high side:4.104323e+00

derive the equation to find this "a" and solve it.

"Derive" --> dang, more fun to simulate as above to find it.

Consider two x:x_lo, x_hi and newton iteration x_better(x) = x - f(x)/f'(x).

When next_better(x_lo) == next_better(x_hi) and x_lo < x_hi, we are at an oscillation pair.

Rest left for OP. Gotta go.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • In his original question, before to edit it, he did not initialize the "x". I think he did not test it at all before to ask here. – alinsoar Oct 08 '21 at 17:48
  • @alinsoar I etited it because i got surprised from comments about not initializating of x.I just didn't wrote printf,scanf and instead was setting x to value i want to calculate.Didn't know that's a big problem. – Nick_Dowson Oct 08 '21 at 19:37
  • @Nick_Dowson If you consider the answer useful, you should accept it. – alinsoar Oct 08 '21 at 22:05