编个C语言程序,用二分法求方程x^3-x^4+4x^2-1=0 在区间[0,1]内的根(精确到0.01)
2个回答
展开全部
#include<stdio.h>
#include<math.h>
int main()
{
double x1=0,x2=1,x3;
double function(double);
while(fabs(x1-x2)<=0.01)
{x3=(x1+x2)/2;
if(function(x1)*function(x2)<=0)
x2=x3;
else
x1=x3;
}
printf("The root of this equation is %f\n",x3);
return 0;
}
double function(double x)
{
return(x*x*x-x*x*x*x+4*x*x-1);
}
#include<math.h>
int main()
{
double x1=0,x2=1,x3;
double function(double);
while(fabs(x1-x2)<=0.01)
{x3=(x1+x2)/2;
if(function(x1)*function(x2)<=0)
x2=x3;
else
x1=x3;
}
printf("The root of this equation is %f\n",x3);
return 0;
}
double function(double x)
{
return(x*x*x-x*x*x*x+4*x*x-1);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
double f(double x)
{
return x*x*x - x*x*x*x + 4*x*x - 1;
}
int main()
{
double a = 0, b = 0, e = 1e-5;
printf("input a b: ");
scanf("%lf%lf", &a, &b);
if (fabs(f(a)) <= e)
{
printf("solution: %lg\n", a);
}
else if (fabs(f(b)) <= e)
{
printf("solution: %lg\n", b);
}
else if (f(a)*f(b) > 0)
{
printf("f(%lg)*f(%lg) > 0 ! need <= 0 !\n", a, b);
}
else
{
while (fabs(b-a) > e)
{
double c = (a+b)/2.0;
if (f(a)* f ( c ) < 0)
b = c;
else
a = c;
}
printf("solution: %lg\n", (a+b)/2.0);
}
return 0;
}
#include <stdlib.h>
#include <math.h>
#include <assert.h>
double f(double x)
{
return x*x*x - x*x*x*x + 4*x*x - 1;
}
int main()
{
double a = 0, b = 0, e = 1e-5;
printf("input a b: ");
scanf("%lf%lf", &a, &b);
if (fabs(f(a)) <= e)
{
printf("solution: %lg\n", a);
}
else if (fabs(f(b)) <= e)
{
printf("solution: %lg\n", b);
}
else if (f(a)*f(b) > 0)
{
printf("f(%lg)*f(%lg) > 0 ! need <= 0 !\n", a, b);
}
else
{
while (fabs(b-a) > e)
{
double c = (a+b)/2.0;
if (f(a)* f ( c ) < 0)
b = c;
else
a = c;
}
printf("solution: %lg\n", (a+b)/2.0);
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询