C语言(二分法)求xsinx-1=0在区间[0,pi/2]精度为0.001

来源:百度知道 编辑:UC知道 时间:2024/05/22 04:02:09
C语言(二分法)求xsinx-1=0在区间[0,pi/2]精度为0.001
紧急希望C语言高手帮忙!!

float root(float x1,float x2)

#include<iostream>
#include<math.h>
using namespace std;

#define PI 3.141592653
double fx(double x);
void main()
{
double sol;
double bottom=0,top=PI/2,mid=0;

do
{
mid=(bottom+top)/2;
if(fx(mid)>0)
top=mid;
else if(fx(mid)<0)
bottom=mid;
}
while( abs(fx(mid))<0.001);

cout<<"x="<<mid<<endl;

}

double fx(double x)
{
return x*sin(x)-1;
}

楼上用的是C++吧?我去弄个C的来 

#include<stdio.h>

#include<math.h>

float f(float x)

{

 float y;

 y=x*sin(x)-1;

 return(y);

 }

float xpoint(float x1,float x2)

 {

  float y;

  y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));

  return(y);