| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 651 人关注过本帖
标题:【求助】黄金分割求极值,编译通不过,求教!
只看楼主 加入收藏
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
结帖率:91.67%
收藏
已结贴  问题点数:10 回复次数:5 
【求助】黄金分割求极值,编译通不过,求教!
我将某统计语言的黄金分割求最小值的函数复制过来,然后试试,居然无法编译通过,请高手指教!
代码:
#include <stdlib.h>
#include <float.h>
#include <stdio.h>
#include <math.h>


double minfun(double x)
{
  return(sin(x) + cos(x));
}


double fmin(double ax, double bx, double f(double, void *),  void *info, double tol)
{
    /*  c is the squared inverse of the golden ratio */
    const double c = (3. - sqrt(5.)) * .5;

    /* Local variables */
    double a, b, d, e, p, q, r, u, v, w, x;
    double t2, fu, fv, fw, fx, xm, eps, tol1, tol3;

/*  eps is approximately the square root of the relative machine precision. */
    eps = DBL_EPSILON;
    tol1 = eps + 1.;/* the smallest 1.000... > 1 */
    eps = sqrt(eps);

    a = ax;
    b = bx;
    v = a + c * (b - a);
    w = v;
    x = v;
    d = 0.;/* -Wall */
    e = 0.;
    fx = (*f)(x, info);
    fv = fx;
    fw = fx;
    tol3 = tol / 3.;

/*  main loop starts here ----------------------------------- */

    for(;;) {
    xm = (a + b) * .5;
    tol1 = eps * fabs(x) + tol3;
    t2 = tol1 * 2.;

    /* check stopping criterion */

    if (fabs(x - xm) <= t2 - (b - a) * .5) break;
    p = 0.;
    q = 0.;
    r = 0.;
    if (fabs(e) > tol1) { /* fit parabola */

        r = (x - w) * (fx - fv);
        q = (x - v) * (fx - fw);
        p = (x - v) * q - (x - w) * r;
        q = (q - r) * 2.;
        if (q > 0.) p = -p; else q = -q;
        r = e;
        e = d;
    }

    if (fabs(p) >= fabs(q * .5 * r) ||
        p <= q * (a - x) || p >= q * (b - x)) { /* a golden-section step */

        if (x < xm) e = b - x; else e = a - x;
        d = c * e;
    }
    else { /* a parabolic-interpolation step */

        d = p / q;
        u = x + d;

        /* f must not be evaluated too close to ax or bx */

        if (u - a < t2 || b - u < t2) {
        d = tol1;
        if (x >= xm) d = -d;
        }
    }

    /* f must not be evaluated too close to x */

    if (fabs(d) >= tol1)
        u = x + d;
    else if (d > 0.)
        u = x + tol1;
    else
        u = x - tol1;

    fu = (*f)(u, info);

    /*  update  a, b, v, w, and x */

    if (fu <= fx) {
        if (u < x) b = x; else a = x;
        v = w;    w = x;   x = u;
        fv = fw; fw = fx; fx = fu;
    } else {
        if (u < x) a = u; else b = u;
        if (fu <= fw || w == x) {
        v = w; fv = fw;
        w = u; fw = fu;
        } else if (fu <= fv || v == x || v == w) {
        v = u; fv = fu;
        }
    }
    }
    /* end of main loop */

    return x;
}



void main()
{
    double minfun();
    eps = 1e-10;
    fx = fmin(0.0,  2.0,minfun,eps);
    printf("%f\n", fx);
    return;
}
用gcc编译出现的错误如下:
test.c:14:8: error: conflicting types for 'fmin'
test.c: In function 'main':
test.c:120:2: error: 'eps' undeclared (first use in this function)
test.c:120:2: note: each undeclared identifier is reported only once for each function it appears in
test.c:121:2: error: 'fx' undeclared (first use in this function)
test.c:121:2: warning: passing argument 3 of 'fmin' from incompatible pointer type [enabled by default]
test.c:14:8: note: expected 'double (*)(double,  void *)' but argument is of type 'double (*)(double)'
test.c:121:2: error: too few arguments to function 'fmin'
test.c:14:8: note: declared here

第一次使用函数作为另一个函数的变量,不怎么懂,敬请指教!

搜索更多相关主题的帖子: 黄金分割 include golden double return 
2015-08-21 15:45
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:5 
C标准库中有个函数叫fmin
#include <math.h>
double fmin(double x, double y);
而你的代码中又重新定义了一个冲突了

 error: 'eps' undeclared (first use in this function) --------- 这种错误就不用说了吧
2015-08-21 15:52
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
收藏
得分:0 
把函数fmin改成了ffmin,主函数也改了
void main()
{
    double minfun();
    double eps = 1e-10;
    double fx = ffmin(0.0,  2.0,minfun,eps);
    printf("%f\n", fx);
    return;
}
编译依旧有问题
test.c: In function 'main':
test.c:120:2: warning: passing argument 3 of 'ffmin' from incompatible pointer type [enabled by default]
test.c:13:8: note: expected 'double (*)(double,  void *)' but argument is of type 'double (*)(double)'
test.c:120:2: error: incompatible type for argument 4 of 'ffmin'
test.c:13:8: note: expected 'void *' but argument is of type 'double'
test.c:120:2: error: too few arguments to function 'ffmin'
test.c:13:8: note: declared here
关键是不懂 void * 和 void *info

2015-08-21 15:58
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
收藏
得分:0 
瞎改了改,居然通过了!
2015-08-21 16:03
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
收藏
得分:0 
不过真不知道void *info的作用,谁能给解释一下?
2015-08-21 16:04
jklqwe111
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:35
帖 子:336
专家分:1135
注 册:2014-4-13
收藏
得分:5 
double fmin(double ax, double bx, double f(double, void *),  void *info, double tol)此句中double f(double, void *)是什么,它能做函数的参数吗
2015-08-21 18:54
快速回复:【求助】黄金分割求极值,编译通不过,求教!
数据加载中...
 
   



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

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