float fun (float x1,float x2)
{return (x1*x1+x2*x2-1.2*x1*x2+2*x1-4*x2+9);
}
# include <stdio.h>
# include <math.h>
void main ()
{float x1,x2,e,a,df1,df2,F,f0;
int k=0;
printf ("the function is f(x1,x2)=x1*x1+x2*x2-1.2*x1*x2+2*x1-4*x2+9\n");
printf ("please input x1=");scanf ("%f",&x1);
printf ("please input x2=");scanf ("%f",&x2);
printf ("please input e=");scanf ("%f",&e);
printf ("k (x1,x2) (df1,df2) f0 a \n");
loop: df1=2*x1-1.2*x2+2; df2=2*x2-1.2*x1-4;
F=sqrt (df1*df1+df2*df2); f0=fun (x1,x2);
if (F<e)
{k=k+1;
printf ("%d (%0.5f,%0.5f) (%0.5f,%0.5f) %0.5f %0.5f \n",k,x1,x2,df1,df2,f0,a);}
else
{a=F*(2*df1-4*df2+2*x1*df1+2*x2*df2-1.2*x2*df1-1.2*x1*df2)/(2*df1*df1+2*df2*df2-2.4*df1*df2);
x1=x1-a*df1/F;x2=x2-a*df2/F;k=k+1;
printf ("%2d (%0.5f,%0.5f) (%0.5f,%0.5f) %0.5f %0.5f \n",k,x1,x2,df1,df2,f0,a);
goto loop;}
printf ("\n the best point of the valua is xm1=%f xm2=%f\n",x1,x2);
printf ("\n the best valua is fp=%f",f0);
getch ();
}
机械有刷设计 求F=x1*x1+x2*x2-1.2*x1*x2+2*x1-4*x2+9的最优解是什么 用C语言程序编写油画程序
追答说实话,看了半天,不觉得你算法会有什么问题
你说有什么问题呢?
估计唯一有问题就是你要把include 放到float fun 上面。只能说有的编译器支持你随便到处include,有的不支持。
回答楼下的:getch()是等待你输入一个字符。这样在windows下面的那个黑窗口不会一下关掉,你可以看到输出结果。等你按任意一个键黑窗口就关掉了。