标题: c语言弦截法&函数嵌套 [打印本页]

作者: liuda    时间: 2015-1-22 02:15
标题: c语言弦截法&函数嵌套
#include<stdio.h>

#include<math.h>
//要求使用函数嵌套
//这道题属于非线性方程的数值解法:弦截法
//数值分析上讲的,跟程序书里讲的不太一样,程序书里还要求f(x1)*f(x2)<0
float f(float x)
{
return(x*x*x-5*x*x+16*x-80);
}
float xp(float x1,float x2)
{
return((x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)));
}
float root(float x1,float x2)
{float x,y;
do
{
x=xp(x1,x2);
y=f(x);
if(f(x)*f(x1)>0)
        x1=x;
else if(f(x)*f(x2)>0)
        x2=x;
}while(fabs(y)>=1e-6);
return(x);
}

void main()
{float x1,x2;
   do
   {
   scanf("%f%f",&x1,&x2);
   }
   while(f(x1)*f(x2)>0);
   printf("root=%f\n",root(x1,x2));

}






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1