C语言 二分法求方程x^2-3x-5=0的近似解#include/*求x^2-3x-5=0在指定区间的解*/#includevoid main(){double x1=1,x2=1,f1=x1*x1-3*x1-5,f2=x2*x2-3*x2-5;printf("请输入指定区间(x1,x2):");scanf_s("%lf,%f",&x1,&x2);aa:double x0=(x
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 10:54:20
C语言 二分法求方程x^2-3x-5=0的近似解#include/*求x^2-3x-5=0在指定区间的解*/#includevoid main(){double x1=1,x2=1,f1=x1*x1-3*x1-5,f2=x2*x2-3*x2-5;printf("请输入指定区间(x1,x2):");scanf_s("%lf,%f",&x1,&x2);aa:double x0=(x
C语言 二分法求方程x^2-3x-5=0的近似解
#include/*求x^2-3x-5=0在指定区间的解*/
#include
void main()
{
double x1=1,x2=1,f1=x1*x1-3*x1-5,f2=x2*x2-3*x2-5;
printf("请输入指定区间(x1,x2):");
scanf_s("%lf,%f",&x1,&x2);
aa:double x0=(x1+x2)/2,f0=x0*x0-3*x0-5;
while (f1*f0>0)
{
f1=f0,x1=x0;
x0=(x1+x2)/2;
f0=x0*x0-3*x0-5;
}
f2=f0,x2=x0;
while (f1*f0>0)
{
f1=f0,x1=x0;
x0=(x1+x2)/2;
f0=x0*x0-3*x0-5;
}
if (fabs(f0)
C语言 二分法求方程x^2-3x-5=0的近似解#include/*求x^2-3x-5=0在指定区间的解*/#includevoid main(){double x1=1,x2=1,f1=x1*x1-3*x1-5,f2=x2*x2-3*x2-5;printf("请输入指定区间(x1,x2):");scanf_s("%lf,%f",&x1,&x2);aa:double x0=(x
#include double func(double x){
return x * (x - 3.0) - 5.0;
}
double binary_search(double x1, double x2, double (*f)(double)){
static const double epsilon = 1.0e-14;
double y1 = f(x1); double y2 = f(x2);
bool b1 = (y1 < 0.0 && y2 > 0.0);
bool b2 = (y1 > 0.0 && y2 < 0.0);
while (b1 || b2)
{
double x = (x1 + x2) / 2;
double y = f(x);
if (fabs(y)