| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 445 人关注过本帖
标题:各位大神看看这个题目错在哪里
只看楼主 加入收藏
liuyinding
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-6-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
各位大神看看这个题目错在哪里
机械优化设计-复合形法C语言编程
目标函数f(x)=25/(x1*x2*x2*x2)
约束条件st:30/(x1*x2*x2*x2)<=0;
            0.0004*x1*x2-0.001<=0;
            2<=x1<=4
            0.5<=x2<=1
以x1=(2.2,0.55) x2=(2.4,0.6) x3=(2.6,0.65) x4=(2.8,0.7)为复合形的初始顶点,用反射变形法求解。
大家来看看这个程序怎么老是运行不出来
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#define ep 0.0001
#define e 0.01
double f(double *p)
{
    double y;
    y=25/(p[0]*pow(p[1],3));
    return y;
}
int cons(double *q)
{
    int n;
    if((30/(q[0]*pow(q[1],3))-50<=0)&&(0.0004*q[0]*q[1]-0.001<=0)&&(q[0]>=2)&&(q[0]<=-4)&&(q[1]>=0.5)&&(q[1]<=1))
    n=1;
    else
    n=0;
    return n;
}
void order(double *p1,double *p2,double *p3,double *p4)
{
    double f1,f2,f3,f4,h[2];
    int i;
    f1=f(p1);
    f2=f(p2);
    f3=f(p3);
    f4=f(p4);
    if(f1<f2)
    for(i=0;i<=1;i++)
    {
        h[i]=p1[i];
        p1[i]=p2[i];
        p2[i]=h[i];
    }
    if(f1<f3)
    for(i=0;i<=1;i++)
    {
        h[i]=p1[i];
        p1[i]=p3[i];
        p3[i]=h[i];
    }
    if(f1<f4)
    for(i=0;i<=1;i++)
    {
        h[i]=p1[i];
        p1[i]=p4[i];
        p4[i]=h[i];
    }
    if(f2<f3)
    for(i=0;i<=1;i++)
    {
        h[i]=p2[i];
        p2[i]=p3[i];
        p3[i]=h[i];
    }
    if(f2<f4)
    for(i=0;i<=1;i++)
    {
        h[i]=p2[i];
        p2[i]=p4[i];
        p4[i]=h[i];
    }
    if(f3<f4)
    for(i=0;i<=1;i++)
    {
        h[i]=p3[i];
        p3[i]=p4[i];
        p4[i]=h[i];
    }
}
    void main()
{
    int i,j=0,tf1,tf2;
    double x1[2]={2.2,0.55},x2[2]={2.4,0.6},x3[2]={2.6,0.65},x4[2]={2.8,0.7},xc[2],xr[2],a[2],b[2],h=1.3;
    loop1: order(x1,x2,x3,x4);
    if((pow((f(x1)-f(x4)),2)+pow((f(x2)-f(x4)),2)+pow((f(x3)-f(x4)),2))/3<ep)
    {
        printf("  \n\n\n  输出最优解及目标函数值:\n");
        printf("\n  x1=%.5f   x2=%.5f    f(x1,x2)=%.5f\n\n ",x4[0],x4[1],f(x4));
    }
    else
    {
        loop2: for(i=0;i<=1;i++)
        xc[i]=(x2[i]+x3[i]+x4[i])/3;
        tf1=cons(xc);
        if(tf1==0)
        {
          for(i=0;i<=1;i++)
          {
            a[i]=x4[i];
            b[i]=xc[i];
          }
          for(i=0;i<=1;i++)
          {
            x1[i]=a[i]+rand()*(b[i]-a[i]);
            x2[i]=a[i]+rand()*(b[i]-a[i]);
            x3[i]=a[i]+rand()*(b[i]-a[i]);
            x4[i]=a[i]+rand()*(b[i]-a[i]);  
          }
          goto loop1;
        }
        else   
        loop3: for(i=0;i<=1;i++)
        xr[i]=xc[i]+h*(xc[i]-x1[i]);
        tf2=cons(xr);
        if(tf2==0)
        {
          loop4 :h=h/2;
          goto loop3;
        }
        else
         if(f(xr)<f(x1))
         {
           for(i=0;i<=1;i++)
           x1[i]=xr[i];
           j++;
           printf("第%d次迭代\n",j);
           for(i=0;i<=1;i++)
           printf("xr%d=%f  ",i,xr[i]);
           goto loop1;
         }
         else
           if(h<pow(10,-10))
           {
             for(i=0;i<=1;i++)
             x1[i]=x2[i];
             goto loop2;
           }
           else
           goto loop4;
    }
}
搜索更多相关主题的帖子: 看看 include double return 
2013-06-01 12:46
justlxy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:28
专家分:158
注 册:2013-5-14
收藏
得分:4 
没问题啊。
#include "math.h"
 #include "stdio.h"
 #include "stdlib.h"
 #define ep 0.0001
 #define e 0.01
 double f(double *p)
 {
     double y;
     y=25/(p[0]*pow(p[1],3));
     return y;
 }
 int cons(double *q)
 {
     int n;
     if((30/(q[0]*pow(q[1],3))-50<=0)&&(0.0004*q[0]*q[1]-0.001<=0)&&(q[0]>=2)&&(q[0]<=-4)&&(q[1]>=0.5)&&(q[1]<=1))
     n=1;
     else
     n=0;
     return n;
 }
 void order(double *p1,double *p2,double *p3,double *p4)
 {
     double f1,f2,f3,f4,h[2];
     int i;
     f1=f(p1);
     f2=f(p2);
     f3=f(p3);
     f4=f(p4);
     if(f1<f2)
     for(i=0;i<=1;i++)
     {
         h[i]=p1[i];
         p1[i]=p2[i];
         p2[i]=h[i];
     }
     if(f1<f3)
     for(i=0;i<=1;i++)
     {
         h[i]=p1[i];
         p1[i]=p3[i];
         p3[i]=h[i];
     }
     if(f1<f4)
     for(i=0;i<=1;i++)
     {
         h[i]=p1[i];
         p1[i]=p4[i];
         p4[i]=h[i];
     }
     if(f2<f3)
     for(i=0;i<=1;i++)
     {
         h[i]=p2[i];
         p2[i]=p3[i];
         p3[i]=h[i];
     }
     if(f2<f4)
     for(i=0;i<=1;i++)
     {
         h[i]=p2[i];
         p2[i]=p4[i];
         p4[i]=h[i];
     }
     if(f3<f4)
     for(i=0;i<=1;i++)
     {
         h[i]=p3[i];
         p3[i]=p4[i];
         p4[i]=h[i];
     }
 }
     void main()
 {
     int i,j=0,tf1,tf2;
     double x1[2]={2.2,0.55},x2[2]={2.4,0.6},x3[2]={2.6,0.65},x4[2]={2.8,0.7},xc[2],xr[2],a[2],b[2],h=1.3;
     loop1: order(x1,x2,x3,x4);
     if((pow((f(x1)-f(x4)),2)+pow((f(x2)-f(x4)),2)+pow((f(x3)-f(x4)),2))/3<ep)
     {
         printf("  \n\n\n  输出最优解及目标函数值:\n");
         printf("\n  x1=%.5f   x2=%.5f    f(x1,x2)=%.5f\n\n ",x4[0],x4[1],f(x4));
     }
     else
     {
         loop2: for(i=0;i<=1;i++)
         xc[i]=(x2[i]+x3[i]+x4[i])/3;
         tf1=cons(xc);
         if(tf1==0)
         {
           for(i=0;i<=1;i++)
           {
             a[i]=x4[i];
             b[i]=xc[i];
           }
           for(i=0;i<=1;i++)
           {
             x1[i]=a[i]+rand()*(b[i]-a[i]);
             x2[i]=a[i]+rand()*(b[i]-a[i]);
             x3[i]=a[i]+rand()*(b[i]-a[i]);
             x4[i]=a[i]+rand()*(b[i]-a[i]);  
           }
           goto loop1;
         }
         else   
         loop3: for(i=0;i<=1;i++)
         xr[i]=xc[i]+h*(xc[i]-x1[i]);
         tf2=cons(xr);
         if(tf2==0)
         {
           loop4 :h=h/2;
           goto loop3;
         }
         else
          if(f(xr)<f(x1))
          {
            for(i=0;i<=1;i++)
            x1[i]=xr[i];
            j++;
            printf("第%d次迭代\n",j);
            for(i=0;i<=1;i++)
            printf("xr%d=%f  ",i,xr[i]);
            goto loop1;
          }
          else
            if(h<pow(10,-10))
            {
              for(i=0;i<=1;i++)
              x1[i]=x2[i];
              goto loop2;
            }
            else
            goto loop4;
     }
 }
2013-06-01 13:02
Han_FlyB
Rank: 6Rank: 6
等 级:侠之大者
帖 子:143
专家分:424
注 册:2013-3-25
收藏
得分:4 
我看到了好多 goto
2013-06-01 14:12
逆风而前
Rank: 7Rank: 7Rank: 7
来 自:福建
等 级:黑侠
威 望:7
帖 子:193
专家分:567
注 册:2013-2-14
收藏
得分:4 
看到了好多goto
2013-06-07 11:11
我叫沃恩
Rank: 12Rank: 12Rank: 12
来 自:Asia
等 级:贵宾
威 望:10
帖 子:1234
专家分:3865
注 册:2013-3-29
收藏
得分:4 
个人建议:不要用太多goto语句!!

因为我是菜鸟,所以应该被骂! 细节+坚持=成功!
2013-06-07 12:48
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:4 
看到了好多goto心里就冒冷汗。。

学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2013-06-08 02:06
快速回复:各位大神看看这个题目错在哪里
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017857 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved