牛顿法求方程!

#include <stdio.h>#include <stdlib.h>
#include <math.h>

int function(double ,double *,double *);
int newton(double *,double,int);

void main()
{
  int l=60;
  double eps=1.e-6;
  double x=1;
  if(!newton(&x,eps,l))
  {
  printf("i can not do it!!");
  exit(1);
  }
  printf("the root is %.10f\n",x);
}

int function(double x,double *f,double *dy)
{
  *f=x*x*x-x*x-1;
  *dy=3*x*x-2*x;
}

int newton(double *x,double eps,int l)
{
  double x1,f,dy;
A:function(*x,&f,&dy);
  if(dy==0)
  {
  l=0;
  return(0);
  }
  x1=*x-f/dy;
  if(fabs(*x-x1)>eps||fabs(f)>eps)
  {
  l-=1;
  *x=x1;
  if(l==0)
  return(1);
   
  goto A;
  }
  *x=x1;
  return(1);
}

你可能感兴趣的:(c,职场,休闲,方程,牛顿法)