📄 golddis.cpp
字号:
#include <iostream.h>
#include <math.h>
float f(float t);
float goldcompute(float a,float b,float e);
void main()
{
float a,b,e,solution;
cout<<"the desiny function is -2*t*t*t+21*t*t-60*t+50"<<endl
<<"please input the a and b in (a,b)";
cin>>a>>b;
cout<<"please input the value of precision:";
cin>>e;
solution=goldcompute(a,b,e);
cout<<"solution is:"<<solution<<endl;
cout<<"f(x)="<<f(solution)<<endl
<<"end";
}
float f(float t)
{
return -2*t*t*t+21*t*t-60*t+50;
}
float goldcompute(float a,float b,float e)
{
float t1,t2,f1,f2;
t1=a+0.382*(b-a); cout<<"t1:"<<t1<<endl;
t2=a+0.618*(b-a); cout<<"t2:"<<t2<<endl;
f1=f(t1); cout<<f1<<endl;
f2=f(t2); cout<<f2<<endl;
for (;;)
{
if(f1<=f2)
{
if (t2-a<=e)
return t1;
else
{
b=t2;
t2=t1;
t1=b-0.618*(b-a);
f2=f1;
f1=f(t1);
}
}
else
{
if(b-t1<=e)
return t2;
else
{
a=t1;
t1=t2;
t2=a+0.618*(b-a);
f1=f2;
f2=f(t2);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -