变尺度法求最小值,跪求大哥大姐们改正
算法源代码:用变尺度法求函数 + 的最小值
#include<stdio.h>
void dpfmethod(float a,float b,float m)
{
static float H={1,0,0,1};
float x;
x=a,x=b;
float c;
c=4*(x-5)*(x-5)+(x-6)*(x-6);
float df;
df=8*(x-5);
df=2*(x-6);
float hdf;
hdf=(H*df+H*df);
hdf=(H*df+H*df);
float namu=(-8*x*hdf+40*hdf-2*hdf*x+12*hdf)/(8*hdf*hdf+2*hdf*hdf);
float G;
G=(-8*namu*hdf);
G=(-2*namu*hdf);
float k=m;
if(df*df+df*df<=k){printf("最小值点是x1=%f,x2=%f\n",x,x);
printf("此时原函数的值为%f\n",c);
getchar();}
else {
x+=namu*hdf;
x+=namu*hdf;
H+=namu*hdf*namu*hdf/(G*namu*hdf+G*namu*hdf)-((H*G+H*G)*G*H+(H*G+H*G)*G*H)/(G*(G*H+G*H)+G*(G*H+G*H));
H+=namu*hdf*namu*hdf/(G*namu*hdf+G*namu*hdf)-((H*G+H*G)*G*H+(H*G+H*G)*G*H)/(G*(G*H+G*H)+G*(G*H+G*H));
H+=namu*hdf*namu*hdf/(G*namu*hdf+G*namu*hdf)-((H*G+H*G)*G*H+(H*G+H*G)*G*H)/(G*(G*H+G*H)+G*(G*H+G*H));
H+=namu*hdf*namu*hdf/(G*namu*hdf+G*namu*hdf)-((H*G+H*G)*G*H+(H*G+H*G)*G*H)/(G*(G*H+G*H)+G*(G*H+G*H));
dpfmethod(x,x,k);
}
}
void main(){
printf("********************* result area *************************\n");
printf("\n");
printf("使用DFP法求函数4(x1-5)^2+(x2-6)^2的最小值\n");
printf("输入x1,x2的初值,以及精度m\n");
float a,b,m;
scanf("%f%f%f",&a,&b,&m);
dpfmethod(a,b,m);
getchar();
}
页:
[1]