|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
#include <stdio.h>
#include <math.h>
#define e 0.00001
#define tt 0.5
double f(double x)
{ double y=sin(x);
return(y);
}
finding(double *p1,double *p2)
{ double x1=4,x2,x3,t,f1,f2,f3,h=tt;
int n=0;
x2=x1+h;f1=f(x1);f2=f(x2);
if (f2>f1) {h=-h;t=x2;x2=x1;x1=t;}
do
{ x3=x2+h;h=2*h;f3=f(x3);n=n+1;}
while(f3<f2);
if(x1>x3) {t=x1;x1=x3;x3=t;}
*p1=x1;*p2=x3;
n=n+1;
return(n);
}
double twice(double *p)
{ double
a,b,x1,x2,f1,f2,x3,c1,c2,f3,x4,f4;
int n=0;
finding(&a,&b);
x1=a;x3=b;
do
{ x2=(x1+x3)/2;
f1=f(x1);f2=f(x2);n=n+1;f3=f(x3);
c1=(f3-f1)/(x3-x1);
c2=(((f2-f1)/(x2-x1))-c1)/(x2-x3);
x4=(x1+x3-c1)/c2/2;
f4=f(x4);
n=n+1;
if (x4>x2)
if (f2>=f4)
{x1=x2;x2=x4;
else
x3=x4;
}
else if (f2>=f4) {x3=x2;x2=x4;else x1=x4;}
}
while (fabs((f2-f4)/f2)<e);
*p=x2;
return(n);
}
void main()
{ double a,b,x,min,n1,n2;
n1=finding(&a,&b);
n2=twice(&x);
min=f(x);
printf("\n The area is %f to %f.",a,b);
printf("\n The min is %f and the result is %f.",x,min);
}
|
|