[黄金分割比] 黄金分割法求单峰函数最值

    xiaoxiao2021-12-14  22

    论文:杨思雨--美,无处不在——浅谈“黄金分割”和信息学的联系

    #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; const double eps=1e-4; const double phi=(sqrt(5.0)-1)/2; double A,B,C; double L,R; int cnt=0; inline double F(double x){ cnt++; return A*x*x+B*x+C; } int main(){ freopen("t.in","r",stdin); freopen("t.out","w",stdout); scanf("%lf%lf%lf%lf",&A,&B,&C,&R); L=0; int left=0,right=0; double x1,x2,f1,f2; while (R-L>eps){ if (!left) x1=R-(R-L)*phi,f1=F(x1); if (!right) x2=L+(R-L)*phi,f2=F(x2); if (f1<f2){ L=x1; x1=x2; f1=f2; left=1; right=0; }else{ R=x2; x2=x1; f2=f1; right=1; left=0; } } printf("%.3lf\n",L); freopen("c.out","w",stdout); printf("%d\n",cnt); return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-968576.html

    最新回复(0)