c语言的问题,求x^3-5x^2+16x-80的根,我用的弦截法,但是出错#include#includefloat f(float x){float y;y=x*(x*x-5*x+16)-80;return y;}float xpoint(float x1,float x2){float root;root=(f(x2)*x1-x2*f(x1))/(f(x2)-f(x1));return root;}void main()

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/09 07:26:01
c语言的问题,求x^3-5x^2+16x-80的根,我用的弦截法,但是出错#include#includefloat f(float x){float y;y=x*(x*x-5*x+16)-80;return y;}float xpoint(float x1,float x2){float root;root=(f(x2)*x1-x2*f(x1))/(f(x2)-f(x1));return root;}void main()

c语言的问题,求x^3-5x^2+16x-80的根,我用的弦截法,但是出错#include#includefloat f(float x){float y;y=x*(x*x-5*x+16)-80;return y;}float xpoint(float x1,float x2){float root;root=(f(x2)*x1-x2*f(x1))/(f(x2)-f(x1));return root;}void main()
c语言的问题,求x^3-5x^2+16x-80的根,我用的弦截法,但是出错
#include
#include
float f(float x)
{float y;
y=x*(x*x-5*x+16)-80;
return y;}
float xpoint(float x1,float x2)
{float root;
root=(f(x2)*x1-x2*f(x1))/(f(x2)-f(x1));
return root;}
void main()
{float a,b,c;
scanf("%f,%f",&a,&b);
for(;f(a)*f(b)>0;scanf("%f,%f",&a,&b));
for(c=f(xpoint(a,b));fabs(c)>0.00000001;)
{if (f(xpoint(a,b)*f(a)>0))
{a=xpoint(a,b);
c=f(xpoint(a,b));}
else
{b=xpoint(a,b);
c=f(xpoint(a,b));}};
printf("%f",xpoint(a,b));
}
当我输入3,6时什么也不输出,也不让我继续输入,这个方程在大于零时只有一个根是5.但是我输入2,7就能输出5.

c语言的问题,求x^3-5x^2+16x-80的根,我用的弦截法,但是出错#include#includefloat f(float x){float y;y=x*(x*x-5*x+16)-80;return y;}float xpoint(float x1,float x2){float root;root=(f(x2)*x1-x2*f(x1))/(f(x2)-f(x1));return root;}void main()

再次解答,原因就是fabs(c)<0.00000001总是不能满足.

代码:

这次如果循环超过1亿次我就退出了,其实这个时候已经得到结果5了.

你再用我的代码试试!


#include<stdio.h>
#include<math.h>

float f(float x)
{
return x*(x*x-5.0*x+16)-80;
}

float xpoint(float x1,float x2)
{
return (f(x2)*x1-x2*f(x1))/(f(x2)-f(x1));
}

void main()
{
float a,b,c,d;
int i=0;
printf("pls input a b\n");
scanf("%f,%f",&a,&b);
printf("a %f b %f\n",a,b);
for(;(f(a)*f(b))>0;printf("result %.2f > 0.pls reinput a b\n",f(a)*f(b) ),scanf("%f,%f",&a,&b));

while(1)
{
d = xpoint(a,b);
c=f(d);

if( fabs(c)< 0.00000001 )
break;

if (c*f(a)<0)
b=d;
else if(c*f(b) <0)
a=d;
else
{
printf("nearly get.\n");
break;
}

if( i++ > 100000000 )
{
printf("calc too much times,exit...,now d is %f,c is %f\n",d,c);
exit(0);
}
}
printf("loop %d times get %f",i,d);
}