目标函数方程:F(x)=ax^4+bx^3+cx^2+dx+e a=1.2, b=-3, c=-7, d=-10, e=229
进退法源程序:
#include
float Flot(float x)
{
float a=1.2, b=-3, c=-7, d=-10, e=229;
float Fx;
Fx=a*x*x*x*x+b*x*x*x+c*x*x+d*x+e;
return Fx;
}
void main()
{
float x0=0,h=1,a,b;
float x1,F1,x2,F2,x3,F3;
printf("请输入初始点x0和区间h:\n");
scanf("%f,%f",&x0,&h); //F(x)=ax^4+bx^3+cx^2+dx+e
} x3=x2+h; F3=Flot(x3); } else { h=-0.25*h; x3=x1;x1=x2;x2=x3; F3=F1;F1=F2;F2=F3; x3=x2+h; F3=Flot(x3); printf("%f,%f\n",x0,h); x1=x0; F1=Flot(x1); x2=x1+h; F2=Flot(x2); //printf("x=%f,F=%f\n",x1,F1); if(F1>F2) { h=2*h;
while(F30) { } else { } a=x3; b=x1; a=x1; b=x3;
} scanf("%f",&a); if(a==1) printf("End\n");
得出a=-2.5,b =10
用于黄金分割法
目标函数方程:F(x)=ax^4+bx^3+cx^2+dx+e a=1.2, b=-3, c=-7, d=-10, e=229
黄金分割法程序流程如下:
4:黄金分割法线框图
5:黄金分割法源程序文件
#include
float Flot(float x)
{
float a=1.2, b=-3, c=-7, d=-10, e=229;
float Fx;
Fx=a*x*x*x*x+b*x*x*x+c*x*x+d*x+e; //F(x)=ax^4+bx^3+cx^2+dx+e return Fx;
}
void main()
{
float a,b,e,c;
float x1,F1,x2,F2;
printf("请输入初始点a,b和精度e:\n");
scanf("%f,%f,%f",&a,&b,&e);
printf("%f,%f,%f\n",a,b,e);
if(a>b)
{ c=a;a=b;b=c;}
x1=a+0.382*(b-a);
F1=Flot(x1);
x2=a+0.618*(b-a);
F2=Flot(x2);
//printf("x=%f,F=%f\n",x1,F1);
while(b-a>e)
{
printf("a=%f,b=%f,x1=%f,x2=%f,F1=%f,F2=%f,b-a=%f\n",a,b,x1,x2,F1,F2,b-a); //printf("x1=%f,F1=%f\n",x1,F1);
//printf("x2=%f,F2=%f\n",x2,F2);
if(F1
{
b=x2;
x2=x1;
F2=F1;
x1=a+0.382*(b-a);
F1=Flot(x1);
}
else
{
a=x1;
x1=x2;
} F1=F2; x2=a+0.618*(b-a); F2=Flot(x2); } } x1=(a+b)/2.0; F1=Flot(x1); printf("x*=%f,F(x*)=%f,e=%f\n",x1,F1,b-a); scanf("%f",&c); if(c==1) printf("End\n");
目标函数方程:F(x)=ax^4+bx^3+cx^2+dx+e a=1.2, b=-3, c=-7, d=-10, e=229
进退法源程序:
#include
float Flot(float x)
{
float a=1.2, b=-3, c=-7, d=-10, e=229;
float Fx;
Fx=a*x*x*x*x+b*x*x*x+c*x*x+d*x+e;
return Fx;
}
void main()
{
float x0=0,h=1,a,b;
float x1,F1,x2,F2,x3,F3;
printf("请输入初始点x0和区间h:\n");
scanf("%f,%f",&x0,&h); //F(x)=ax^4+bx^3+cx^2+dx+e
} x3=x2+h; F3=Flot(x3); } else { h=-0.25*h; x3=x1;x1=x2;x2=x3; F3=F1;F1=F2;F2=F3; x3=x2+h; F3=Flot(x3); printf("%f,%f\n",x0,h); x1=x0; F1=Flot(x1); x2=x1+h; F2=Flot(x2); //printf("x=%f,F=%f\n",x1,F1); if(F1>F2) { h=2*h;
while(F30) { } else { } a=x3; b=x1; a=x1; b=x3;
} scanf("%f",&a); if(a==1) printf("End\n");
得出a=-2.5,b =10
用于黄金分割法
目标函数方程:F(x)=ax^4+bx^3+cx^2+dx+e a=1.2, b=-3, c=-7, d=-10, e=229
黄金分割法程序流程如下:
4:黄金分割法线框图
5:黄金分割法源程序文件
#include
float Flot(float x)
{
float a=1.2, b=-3, c=-7, d=-10, e=229;
float Fx;
Fx=a*x*x*x*x+b*x*x*x+c*x*x+d*x+e; //F(x)=ax^4+bx^3+cx^2+dx+e return Fx;
}
void main()
{
float a,b,e,c;
float x1,F1,x2,F2;
printf("请输入初始点a,b和精度e:\n");
scanf("%f,%f,%f",&a,&b,&e);
printf("%f,%f,%f\n",a,b,e);
if(a>b)
{ c=a;a=b;b=c;}
x1=a+0.382*(b-a);
F1=Flot(x1);
x2=a+0.618*(b-a);
F2=Flot(x2);
//printf("x=%f,F=%f\n",x1,F1);
while(b-a>e)
{
printf("a=%f,b=%f,x1=%f,x2=%f,F1=%f,F2=%f,b-a=%f\n",a,b,x1,x2,F1,F2,b-a); //printf("x1=%f,F1=%f\n",x1,F1);
//printf("x2=%f,F2=%f\n",x2,F2);
if(F1
{
b=x2;
x2=x1;
F2=F1;
x1=a+0.382*(b-a);
F1=Flot(x1);
}
else
{
a=x1;
x1=x2;
} F1=F2; x2=a+0.618*(b-a); F2=Flot(x2); } } x1=(a+b)/2.0; F1=Flot(x1); printf("x*=%f,F(x*)=%f,e=%f\n",x1,F1,b-a); scanf("%f",&c); if(c==1) printf("End\n");