C语言中二分法的具体程序是什么呢?
来源:百度知道 编辑:UC知道 时间:2024/05/29 20:12:38
二分法计算函数f(x)=x*x*x*x+2*x*x*x-x-1;
本程序在turbo c或c++下编译:
#include "stdio.h"
#include <math.h>
float f(float x)
{float y;
y=x*x*x*x+2*x*x*x-x-1;
return y;
}
void main()
{float a=0,b=0,h,y,x;
int k,n0;
printf("please input qujian a and b");
scanf("%f%f%d",&a,&b,&n0); /*输入含根区间a,b,循环次数n0 */
for(k=0;k<=n0;k++)
{ x=(a+b)/2;
h=(b-a)/2;
y=f(x);
if(h<10e-6||fabs(y)<10e-6)
{ printf("k=%d,x=%f,y=%f",k,x,y);
break; } /*输出分半次数k,函数的根x,及x对应的函数值.*/
else
{if(f(a)*f(x)<0)
b=x;
else a=x;
}
}
}
#include <stdio.h>
#include <math.h>
//int chashu(int shu,int *a);
main()
{
int chashu(int shu , int p[11] );
int