⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 最优化方法-0.618求最大值.c

📁 0.618计算最大值 学最优化的时候写的菜程序
💻 C
字号:
//calculator the bigest value of funtion by using  0.618 method
//write by peidright on 2006.10.4.19.10
//version 0.10
#include"stdio.h"
#include"iostream.h"
#include"math.h"
double s=0.000001;
// the function
double f6181(double x){
 double y,z,u;
 z=1-x;
 u=30*x;
 y=(-1)*sin(x)*sin(x)*sin(x)*sin(x)*sin(x)*sin(x)*tan(z)*exp(u);

return y;}
//the method
double f6182(double x,double y){
 double l1=x+0.382*(y-x);
 double l2=x+0.618*(y-x);
loop:
 if(f6181(l1)>=f6181(l2)){
  if((y-l1)<=s){
   //printf("%f",l2);
   return l2;
  }
  else {
   x=l1;l1=l2;
      l2=x+0.618*(y-x);
          goto loop;
  }
  
 }
 else {
  if((l2-x)<=s){
        //printf("%f",l2);
  return l1;
  //exit(0);
  }
  else {
      y=l2;
   l2=l1;
   l1=x+0.382*(y-x);
  goto loop;
  }

 

 }

return 2;}
double test(double x,double y){
if(f6181(x)<=f6181(y))printf(" x is better than y");
    else printf(" y is better than x");

return 0;}

int main(){
 double y;
 y=f6182(0.0,1.0);
 test(y,0.5);
 printf("%f",y);
return 0;}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -