C语言用二分法求解的具体程序
1个回答
展开全部
你这个方程貌似无解。我找不到含根区间。但是具体程序如下,我换了一个方程:
#include<stdio.h>
#include
<math.h>
float
getvalue(float
x)
{
return
x*x+2*x-3;
}
void
main()
{
int
N=0;
float
a,b,c;
printf("Input
[a,b]
have
root:");
scanf("%f%f",&a,&b);
c=(a+b)/2;
if
(!getvalue(c))
{
printf("%lf\n",c);
}
while(fabs(getvalue(c))>0.000001
&&
fabs(a-b)>0.000001)
{
if
(N>100)
{
printf("Maybe
No
root
in
[%f,%f],you
should
change
a,b!\n",a,b);
break;
}
N++;
if(getvalue(c)*getvalue(b)<0)
{a=c;}
if(getvalue(a)*getvalue(c)<0)
{b=c;}
c=(a+b)/2;
printf("%lf\n",c);
}
printf("root
is
%lf\n",c);
}
#include<stdio.h>
#include
<math.h>
float
getvalue(float
x)
{
return
x*x+2*x-3;
}
void
main()
{
int
N=0;
float
a,b,c;
printf("Input
[a,b]
have
root:");
scanf("%f%f",&a,&b);
c=(a+b)/2;
if
(!getvalue(c))
{
printf("%lf\n",c);
}
while(fabs(getvalue(c))>0.000001
&&
fabs(a-b)>0.000001)
{
if
(N>100)
{
printf("Maybe
No
root
in
[%f,%f],you
should
change
a,b!\n",a,b);
break;
}
N++;
if(getvalue(c)*getvalue(b)<0)
{a=c;}
if(getvalue(a)*getvalue(c)<0)
{b=c;}
c=(a+b)/2;
printf("%lf\n",c);
}
printf("root
is
%lf\n",c);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询