用C语言编程二分法求解方程组
#include<stdio.h>
#include<math.h>
#define EQUATION(A, B, m, n, x) (A*pow(x, m) + B*pow(x, n) - 1)/* 定义方程*/
double A, B, m, n;/*方程参数*/
void find_extent(double *x1, double *x2)/*找出根所在的区间*/
{
double i = 0, j = 0;
*x1 = 0;
if(EQUATION(A, B, m, n, 0) > 0)
{
while(EQUATION(A, B, m, n, ++i) > 0 && EQUATION(A, B, m, n, --j) > 0);
*x2 = EQUATION(A, B, m, n, i) <= 0 ? i : j;
}
else
{
while(EQUATION(A, B, m, n, ++i) < 0 && EQUATION(A, B, m, n, --j) < 0);
*x2 = EQUATION(A, B, m, n, i) >= 0 ? i : j;
}
}
/*二分法解方程*/
/*输入参数:根所在区间[x1, x2], 和精度*/
double get_result(double x1, double x2, double uracy)
{
double x = (x1 + x2) / 2;
while(fabs(EQUATION(A, B, m, n, x)) > uracy)
{
if(EQUATION(A, B, m, n, x) * EQUATION(A, B, m, n, x1) > 0)
x1 = x;
else
x2= x;
x = (x1 + x2)/2;
}
return x;
}
int main()
{
double x;
double x1,x2;
scanf("%lf%lf%lf%lf", &A, &B, &m, &n);
find_extent(&x1, &x2);
x = get_result(x1, x2, 0.00001);
printf("%lf", x);
return 0;
}
C语言二分法解方程,急!谢谢!
本题的一个完整的c程序如下,程序在win-tc下调试通过,结果正确。
#include
#include
#include
#include
int n;
double c[16];
double Func(double);
int BisectRoot(double,double,double,double,double *,int,int *);
int main()
{
int i,m;
double a,b,h,eps,*x;
printf("Please input integer k:
");
scanf("%d",&n); /*方程根的个数的预估值*/
printf("Please input %d coefficients one by one:
",n+1);
for(i=0;i<=n;i++)
scanf("%lf",&c[i]);
x = (double*)calloc(n,sizeof(double)); /*开辟内存空间*/
if(x == NULL)
{
printf("内存分配失败
");
exit(1);
}
a = -10; /*区间起始端点*/
b = 10; /*区间终止端点*/
h = 0.1; /*步长*/
eps = 1.e-5; /*要求达到的精度*/
BisectRoot(a,b,h,eps,x,n,&m); /*调用二分法函数*/
printf("The roots between %2.0f and %2.0f are: %d
",a,b,m);
printf("They are:
");
for(i = 0;ib+h/2)||(*m==n))
return(1);
if(fabs(y)0) /*如果对应根乘积大于零,说明该区间内没有根*/
{
y = y1;
z = z1;
continue;
}
while(1) /*如果本while循环执行,说明逐步扫描小区建z和z1间有根*/
{
if(fabs(z1-z)C语言中二分法的编程#include
#include
void main()
{int x1,x2,x,f1,f2;
f1=x1*x1*x1-3*x1*x1-2*x1-6;
f2=x2*x2*x2-3*x2*x2-2*x2-6;
do
{scanf("%d%d",x1,x2);
}while(f1*f2>0);
do
{x=(x1+x2)/2;
f=x*x*x-3*x*x-2*x-6;
if(f*f1<0)
{x2=x;
f2=f;}
if(f*f2<0)
{x1=x;
f1=f;}
}while(fabs(x1-x2)>0.001);
printf("%d",x);}用C语言,运用二分法,求函数零点。
#include<stdio.h>
#include<math.h>
typedef double(*fun)(double xx);//函数指针
#define e 0.000001 //误差
void eff(double a,double b,fun hs)//二分法
{int i=0;
while(fabs(hs(a)-hs(b))>e&&fabs(a-b)>e){i++;
if(hs(a)*hs((b+a)/2)>0){
a=(a+b)/2;
printf("迭代第%d次: %f
",i,a);
}
else {
b=(a+b)/2;
printf("迭代第%d次: %f
",i,b);
}
}
}
double hs1(double xx)//函数f(x)=x^3+x^2-3x-3
{return xx*xx*xx+xx*xx-3*xx-3;}
double hs2(double xx)//函数f(x)=lnx+x
{return log(xx)+xx;}
void main()
{
printf("用二分法求方程x^3+x^2-3x-3=0在1.5附近的根
");
eff(1.0,2.0,hs1);
printf("用二分法求方程lnx+x在0.5附近的根
");
eff(0.0,1.0,hs2);
}C语言用二分法求解的具体程序
double power(double x,int n)
{
int i;
double result=1;
for (i=0;i<n;i++)
result*=x;
return result;
}
main()
{
double lx,rx,x,s;
lx=-2;rx=5;
do
{
x=(lx+rx)/2;
s=power(x,6)+power(x,5)*5+power(x,4)*3+power(x,3)-x*x*7+7*x-20;
if (s>0) rx=x;
else lx=x;
} while (fabs(s)>0.000001);
printf("%f
",x);
}
c语言二分法求根
#include
#include
double fun(double x)
{return 1.0*x*x-10;}
int main()
{double x1,x2;
//此处有两个错误,double 类型的输入输出格式是lf,判断条件是x1x2处的函数值异号而不是同号
if ((scanf("%lf,%lf",&x1,&x2))&&(fun(x1)*fun(x2)<=0)&&(x10)
//if判断后执行的不是一条语句就要用大括号括起来
{x1=(x1+x2)/2 ;continue;}
if(fun((x1+x2)/2)*fun(x2)>0)
{x2=(x1+x2)/2 ;continue;}
}
//循环条件也有问题,当某一个端点的值足够小时就表示找到了,就要退出循环
while (!(fabs(fun(x1))<=1E-6||(fabs(fun(x2))<=1E-6)));
printf("%f %f",x1,x2);
printf("%f %f",fun(x1),fun(x2));}
else
printf("error!
");
return 0;
}