📄 0618.c
字号:
/*黄金分割法求极值 */
/*求 minf(x)=3x^2-21.6x-1 在〔0,10〕内极小值,精度不超过1;0.1;0.01;*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
float fun(float t)
{ float y;
y=3*t*t-21.6*t-1;
return(y);
}
main()
{
float a,b,e,t1,t2,f1,f2;
int count=0;
a=0;
b=10;
printf("input e\n");
scanf("%f",&e);
t1=a+0.382*(b-a);
t2=a+0.618*(b-a);
f1=fun(t1);
f2=fun(t2);
while(fabs(a-b)>e)
{
if(f1>f2)
{
a=t1;
b=b;
t1=t2;
t2=a+0.618*(b-a);
f2=fun(t2);
count++;
}
else
{
a=a;
b=t2;
t2=t1;
t1=a+0.382*(b-a);
f1=fun(t1);
count++;
}
}
printf("output:x=%f,min=%f",(a+b)/2,fun((a+b)/2));
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -