c語言二分法切割法牛頓法求根演算法
阿新 • • 發佈:2019-01-04
c語言實現的二分法,切割法,牛頓法求根演算法
//二分法
double fun(double m,double n)
{
double r;int i=0;
if (quest(m)*quest(n)>0)
printf ("There are no roots in this area\n");
while (fabs(n-m)>esp)
{
r=(m+n)/2;
if (quest(r)*quest(n)<=0)
m=r;
else
n=r;
i++;
printf("第%d次運算結果:\t%f\n",i,r);
}
printf("運算次數為 i=%d\n",i);
return r;
}
//牛頓法
double niudunfa(double x)
{
int i=0;
double x1=x;
do{
if (f1(x)<esp) break;
x=x1;
x1=x-f(x)/f1(x);
i++;
printf ("第%d次運算結果:\t%f\n",i,x1);
}
while(fabs(f(x1)-f(x))>esp);
printf("運算次數為 i=%d\n",i);
return (x1);
}
//切割法
double qiegefa(double x0,double x1)
{
double x;
int i=0;
while(fabs(x1-x0)>esp)
{
x=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
x0=x1;
x1=x;
i++;
printf ("第%d次運算結果:\t%f\n",i,x1);
}
printf("運算次數為 i=%d\n",i);
return (x1);
}