Advertisement
Arnab_Manna

Newton Rapson.ccccc

Sep 24th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1.  #include<stdio.h>
  2.  #include<stdlib.h>
  3.  #include<math.h>
  4.  
  5.  double f(double x)
  6.  {
  7.  return 2.3*x*sin(x)+10.3*x-1.5/x; //x*x*x-2;
  8.  }
  9.  
  10.  double f1(double x)
  11.  {
  12.  return 2.3*sin(x)+2.3*x*cos(x)+10.3+1.5/(x*x);
  13.  }
  14.  
  15.  void GetRootRange(double *L, double *U)
  16.  {
  17.  double y;
  18.  for(y=-999999.0l;y<999999.0l;y=y+1.5)
  19.  {
  20.  if(f(y)*f(y+1.5)<0.0)
  21.  {
  22.  *L=y;*U=y+1.5;
  23.  return;
  24.  }
  25.  }
  26.  }
  27.  
  28.  
  29.  double NR(double x0, double err)
  30.  {
  31.  double x;
  32.  int k=0;
  33.  while(1)
  34.  {
  35.  if (f1(x0)!=0.0)
  36.  x = x0 - f(x0)/f1(x0);
  37.  else
  38.  {
  39.  printf("\nDiscontinued at %lf",x0);
  40.  exit(0);
  41.  }
  42.  printf("\n%d\t%10.5f\t%10.5f",k,x0,x);
  43.  k++;
  44.   if(fabs(x-x0)<err)
  45.  return x;
  46.  x0 = x;
  47.  }
  48.  }
  49.  
  50.  int main()
  51.  {
  52.  const double err = 1.0E-10;
  53.  double x0,x1;
  54.  //printf("\nEnter initial guess:");
  55.  //scanf("%lf",&x0);
  56.  GetRootRange(&x0,&x1);
  57.   x0=(x0+x1)/2.0;
  58.  printf("\nSoulution:%lf",NR(x0,err));
  59.  
  60. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement