#include<iostream.h>
#include<math.h>
float f(float x)
{
return(exp(x)-3*x);
}
void main()
{
float a,b,c,d,e;
int k=0;
cout<<"\na0=";
cin>>a;
cout<<f(a);
cout<<"\nb0=";
cin>>b;
cout<<f(b);
cout<<"\nEpsilon=";
cin>>e;
//while(f(a)*f(b)>0)
c=(a+b)/2;
while(1)
{
if(f(a)*f(c)<0)
b=c;
else
a=c;
d=(a+b)/2;
if(fabs((d-c)/d)<=e)
break;
else
c=d;
cout<<"\nk="<<k<<"\t a0="<<a<<"\t b0="<<b<<"\t c="<<c<<"\t f(x)="<<f(c);
k++;
}
cout<<"\nThe result is="<<d;
}
#include<math.h>
float f(float x)
{
return(exp(x)-3*x);
}
void main()
{
float a,b,c,d,e;
int k=0;
cout<<"\na0=";
cin>>a;
cout<<f(a);
cout<<"\nb0=";
cin>>b;
cout<<f(b);
cout<<"\nEpsilon=";
cin>>e;
//while(f(a)*f(b)>0)
c=(a+b)/2;
while(1)
{
if(f(a)*f(c)<0)
b=c;
else
a=c;
d=(a+b)/2;
if(fabs((d-c)/d)<=e)
break;
else
c=d;
cout<<"\nk="<<k<<"\t a0="<<a<<"\t b0="<<b<<"\t c="<<c<<"\t f(x)="<<f(c);
k++;
}
cout<<"\nThe result is="<<d;
}
No comments:
Post a Comment