初学者 求助:用弦截法求方程x*x*x*x+4*x*x*x-3*x*x+5*x+6=0的根(要求使用函数嵌套调用)
我用的是Dev-c++,编写程序如下:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double f(double x)
{
double y;
y=x*x*x*x+4*x*x*x-3*x*x+5*x+6;
return(y);
}
double point(double x1, double x2)
{
double x;
double y1;
double y2;
y1=f(x1);
y2=f(x2);
x=(x1*y2-x2*y1)/(y2-y1);
return(x);
}
double root(double x1, double x2)
{
double x;
double y;
double y1;
y1=f(x1);
do
{
x=point(x1, x2);
y=f(x);
if(y*y1>0)
{
x1=x;
y1=y;
}
else
x2=x;
}
while(fabs(y)>=1e-9);
return(x);
}
int main(int argc, char *argv[])
{
double x1;
double x2;
double r;
double y1;
double y2;
do
{
printf("请输入两个数:\n");
scanf("%lf%lf", &x1, &x2);
y1=f(x1);
y2=f(x2);
}while((y1*y2)>=0);
r=root(x1, x2);
printf("该方程的根为: r=%lf\n");
system("PAUSE");
return 0;
}
输入10 和-2,运行结果不正确,为:该方程的根为:r=-44686712925511037000000000000000000000000000000000000000000000000
请各位大侠帮忙分析一下我哪里出错了,不胜感激!!!