这分也太少了。
/***二分法***/
#include
#include
#include
float f(float x)
{
return exp(x)+10*x-2;
}
float Separate(float a,float b,float eps)
{
float f(float);
float c,fa,fb,fc;
if (a>b)
{c=a;a=b;b=c;}
fa=f(a);
fb=f(b);
c=(b+a)/2;
fc=f(c);
/*printf("a=%f b=%f mid=%f fa=%f fb=%f fc=%f\n",a,b,c,fa,fb,fc);*/
if (b-a
if (fa*fb>0)
{
printf("error:f(a)*f(b)>0");
getch();
return -1;
}
if (fabs(fc)
if (fa*fc<0)
return Separate(a,c,eps);
else
return Separate(c,b,eps);
}
int main()
{
float x;
float Separate(float a,float b,float eps);
x=Separate(0,1,0.5e-3);
printf("%f\n",x);
getch();
return 0;
}
/***迭代法***/
#include
#include
#include
float f(float x)
{
return (2-exp(x))/10;
}
float Iter(float x,float eps)
{
float xnew;
float f(float);
int itern=0;
while(itern<1000) /*最大迭代次数*/
{
xnew=f(x);
printf("itern=%d\tx=%f\n",itern,xnew);
if (fabs(xnew-x)<=eps)
return xnew;
x=xnew;
itern++;
}
printf("Divergence\n");
return xnew;
}
int main()
{
float x;
float Iter(float x,float eps);
x=Iter(0,0.5e-3);
printf("%f\n",x);
getch();
return 0;
}
/***牛顿迭代法***/
#include
#include
#include
float f(float x)
{
return exp(x)+10*x-2;
}
float df(float x)
{
return exp(x)+10;
}
float Newton(float x,float eps)
{
int itern=0;
float xnew;
float f(float);
float df(float);
while(itern<1000) /*最大迭代次数*/
{
xnew=x-f(x)/df(x);
printf("itern=%d\tx=%f\n",itern,xnew);
if (fabs(xnew-x)<=eps)
return xnew;
x=xnew;
itern++;
}
printf("Divergence\n");
return xnew;
}
int main()
{
float x;
float Newton(float x,float eps);
x=Newton(0,0.5e-3);
printf("%f\n",x);
getch();
return 0;
}
这分也太少了。
/***二分法***/
#include
#include
#include
float
f(float
x)
{
return
exp(x)+10*x-2;
}
float
Separate(float
a,float
b,float
eps)
{
float
f(float);
float
c,fa,fb,fc;
if
(a>b)
{c=a;a=b;b=c;}
fa=f(a);
fb=f(b);
c=(b+a)/2;
fc=f(c);
/*printf("a=%f
b=%f
mid=%f
fa=%f
fb=%f
fc=%f\n",a,b,c,fa,fb,fc);*/
if
(b-a
c;
if
(fa*fb>0)
{
printf("error:f(a)*f(b)>0");
getch();
return
-1;
}
if
(fabs(fc)
c;
if
(fa*fc<0)
return
Separate(a,c,eps);
else
return
Separate(c,b,eps);
}
int
main()
{
float
x;
float
Separate(float
a,float
b,float
eps);
x=Separate(0,1,0.5e-3);
printf("%f\n",x);
getch();
return
0;
}
/***迭代法***/
#include
#include
#include
float
f(float
x)
{
return
(2-exp(x))/10;
}
float
Iter(float
x,float
eps)
{
float
xnew;
float
f(float);
int
itern=0;
while(itern<1000)
/*最大迭代次数*/
{
xnew=f(x);
printf("itern=%d\tx=%f\n",itern,xnew);
if
(fabs(xnew-x)<=eps)
return
xnew;
x=xnew;
itern++;
}
printf("Divergence\n");
return
xnew;
}
int
main()
{
float
x;
float
Iter(float
x,float
eps);
x=Iter(0,0.5e-3);
printf("%f\n",x);
getch();
return
0;
}
/***牛顿迭代法***/
#include
#include
#include
float
f(float
x)
{
return
exp(x)+10*x-2;
}
float
df(float
x)
{
return
exp(x)+10;
}
float
Newton(float
x,float
eps)
{
int
itern=0;
float
xnew;
float
f(float);
float
df(float);
while(itern<1000)
/*最大迭代次数*/
{
xnew=x-f(x)/df(x);
printf("itern=%d\tx=%f\n",itern,xnew);
if
(fabs(xnew-x)<=eps)
return
xnew;
x=xnew;
itern++;
}
printf("Divergence\n");
return
xnew;
}
int
main()
{
float
x;
float
Newton(float
x,float
eps);
x=Newton(0,0.5e-3);
printf("%f\n",x);
getch();
return
0;
}
(2)
#include
#include
#define
N
0.0005
void
main()
{
double
x1,x2;
x1=0;
x2=(2-exp(x1))/10;
while(fabs(x2-x1)>=N)
{
x1=x2;
x2=(2-exp(x1))/10;
}
printf("x=%f\n",x2);
}