CSDN博客

img hello1979cn

牛顿法求方程

发表于2008/9/29 17:11:00  289人阅读

#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);
}
0 0

相关博文

我的热门文章

img
取 消
img