C 中求函数局部最大值的问题

发布于 2024-10-31 04:33:23 字数 1034 浏览 0 评论 0原文

我正在设计一种算法来定义一个简单的方法,能够找到在区间 [a, b] 中给定的函数 f (x) 的局部最大值

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.141592653
float funtion_(float a, float x){

float result=0;
result = a * (sin (PI*x));
    return result;
}

int main (){
double A = 4.875; //average of the digits of the identification card
double a = 0.0, b =1.0; //maximum and minimum values of the interval [a, b]
double h=0;
double N;
double Max, x;
double sin_;

double inf;
printf ("input the minux value: ");
scanf ("%lf", &inf);
printf ("input the N value: ");

scanf ("%lf", &N);

h= (b-a)/N;
printf("h = %lf\n", h);

x=a-h;
Max = -inf;

do {
x = x+h;
sin_ = funtion_(A, x);
if (sin_>=Max){
    Max = sin_;
    }
}while (x==b);

printf ("Maximum value: %lf.5", Max);
return 0;
}

该算法实现函数 f (x) = A * sin (pi * x ),其中 A 是我的 ID 的数字的平均值,并且 inf 变量被分配的数字足够大于区间 [a, b] = [0.1] 中函数达到的值。

该算法必须找到函数的局部最大值,但最大值的返回值始终为零。不明白为什么。我的解决方案的逻辑可能是什么?这个问题可以通过这个简单的算法来解决,还是需要通过回溯进行一些优化?感谢您的回复。

I'm designing an algorithm to define a simple method able to find the local maximum of a function f (x) given in an interval [a, b]

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.141592653
float funtion_(float a, float x){

float result=0;
result = a * (sin (PI*x));
    return result;
}

int main (){
double A = 4.875; //average of the digits of the identification card
double a = 0.0, b =1.0; //maximum and minimum values of the interval [a, b]
double h=0;
double N;
double Max, x;
double sin_;

double inf;
printf ("input the minux value: ");
scanf ("%lf", &inf);
printf ("input the N value: ");

scanf ("%lf", &N);

h= (b-a)/N;
printf("h = %lf\n", h);

x=a-h;
Max = -inf;

do {
x = x+h;
sin_ = funtion_(A, x);
if (sin_>=Max){
    Max = sin_;
    }
}while (x==b);

printf ("Maximum value: %lf.5", Max);
return 0;
}

The algorithm implements the function f (x) = A * sin (pi * x), where A is the average of the digits of my ID, and inf variable is assigned a number sufficiently greater than the values ​​reached by the function in the interval [a, b] = [0.1].

The algorithm must find the local maximum of the function but it is the maximum returns always zero. do not understand why. What problem may be the logic of my solution?, this problem can be solved by this simple algorithm or some optimization by backtracking is necessary ? Thanks for your responses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疏忽 2024-11-07 04:33:23

这段代码有几个问题;可能最明显的是:

int a = 0, b = 1;
float Max, x;
/* ... */
do {
/* ... */
} while (x == b);

您无法比较 intfloat 是否相等。由于运气不好,它可能偶尔会工作一次:),但你不能指望这段代码能够可靠地工作。

我强烈建议将所有 int 变量更改为 double,将所有 float 变量更改为 double,并将所有 float 变量更改为 double,并将所有 < code>scanf(3) 和 printf(3) 调用进行匹配。虽然您可以在一个程序中组合不同的原始数字类型,甚至在一个表达式或语句中,但执行中的细微差别将需要您花费数小时才能发现。

此外,比较浮点格式是否相等几乎从来都不是一个好主意。相反,将两个数字之间的差异epsilon值进行比较:

if (fabs(a-b) < 0.001)
    /* consider them equal */

您可能需要缩放您的epsilon,以便它与您的问题的规模相匹配;由于 float 实际上只支持大约七位数的精度,因此这种比较效果不佳:

if (fabsf(123456789 - 123456789.1) < 0.5)
    /* oops! fabsf(3) used to force float */
    /* and float can't tell the difference */

您可能想找到一个关于 数值分析。 (顺便说一句,这是我在学校最喜欢的课程之一。:)

更新

问题的核心是你的while(x == b)。我修复了这个问题和一些小问题,这段代码似乎可以工作:
#包括
#包括
#包括
#定义PI 3.141592653
浮点函数_(浮点a,浮点x)
{

    float result = 0;
    result = a * (sin(PI * x));
    return result;
}

int main()
{
    float A = 4.875;      //average of the digits of the identification card
    float a = 0.0, b = 1.0;   //maximum and minimum values of the interval [a, b]
    float h = 0;
    float N;
    float Max, x;
    float sin_;

    float inf;
    printf("\ninput the inf value: ");
    scanf("%f", &inf);
    printf("\ninput the N value: ");

    scanf("%f", &N);

    h = (b - a) / N;

    x = a - h;
    Max = -inf;

    do {
            x = x + h;
            sin_ = funtion_(A, x);
            if (sin_ >= Max) {
                    Max = sin_;
                printf("\n new Max: %f found at A: %f x: %f\n", Max, A, x);

            }
    } while (x < b);

    printf("Maximum value: %.5f\n", Max);
    return 0;
}

使用一些小输入运行该程序:

$ ./localmax 

input the inf value: 1

input the N value: 10

 new Max: 0.000000 found at A: 4.875000 x: 0.000000

 new Max: 1.506458 found at A: 4.875000 x: 0.100000

 new Max: 2.865453 found at A: 4.875000 x: 0.200000

 new Max: 3.943958 found at A: 4.875000 x: 0.300000

 new Max: 4.636401 found at A: 4.875000 x: 0.400000

 new Max: 4.875000 found at A: 4.875000 x: 0.500000
Maximum value: 4.87500
$ 

Several problems with this code; probably the most glaring is:

int a = 0, b = 1;
float Max, x;
/* ... */
do {
/* ... */
} while (x == b);

You cannot compare an int and a float for equality. It might work once in a great while due to dumb luck :) but you cannot expect this code to function reliably.

I strongly recommend changing all your int variables to double, all your float variables to double, and all the scanf(3) and printf(3) calls to match. While you can combine different primitive number types in one program, and even in one expression or statement, subtle differences in execution will take you hours to discover.

Furthermore, comparing floating point formats for equality is almost never a good idea. Instead, compare the difference between two numbers to a epsilon value:

if (fabs(a-b) < 0.001)
    /* consider them equal */

You might want to scale your epsilon so that it matches the scale of your problem; since float really only supports about seven digits of precision, this comparison wouldn't work well:

if (fabsf(123456789 - 123456789.1) < 0.5)
    /* oops! fabsf(3) used to force float */
    /* and float can't tell the difference */

You might want to find a good introduction to numerical analysis. (Incidentally, one of my favorite classes back in school. :)

update

The core of the problem is your while(x == b). I fixed that and a few smaller problems, and this code seems to work:
#include
#include
#include
#define PI 3.141592653
float funtion_(float a, float x)
{

    float result = 0;
    result = a * (sin(PI * x));
    return result;
}

int main()
{
    float A = 4.875;      //average of the digits of the identification card
    float a = 0.0, b = 1.0;   //maximum and minimum values of the interval [a, b]
    float h = 0;
    float N;
    float Max, x;
    float sin_;

    float inf;
    printf("\ninput the inf value: ");
    scanf("%f", &inf);
    printf("\ninput the N value: ");

    scanf("%f", &N);

    h = (b - a) / N;

    x = a - h;
    Max = -inf;

    do {
            x = x + h;
            sin_ = funtion_(A, x);
            if (sin_ >= Max) {
                    Max = sin_;
                printf("\n new Max: %f found at A: %f x: %f\n", Max, A, x);

            }
    } while (x < b);

    printf("Maximum value: %.5f\n", Max);
    return 0;
}

Running this program with some small inputs:

$ ./localmax 

input the inf value: 1

input the N value: 10

 new Max: 0.000000 found at A: 4.875000 x: 0.000000

 new Max: 1.506458 found at A: 4.875000 x: 0.100000

 new Max: 2.865453 found at A: 4.875000 x: 0.200000

 new Max: 3.943958 found at A: 4.875000 x: 0.300000

 new Max: 4.636401 found at A: 4.875000 x: 0.400000

 new Max: 4.875000 found at A: 4.875000 x: 0.500000
Maximum value: 4.87500
$ 
听闻余生 2024-11-07 04:33:23

您正在使用整数算术进行计算,特别是 h 的初始化。所以在语句中:

h = (b-a) / N;

abN都是整数,因此表达式被计算为整数表达式,然后 转换为浮点数以分配给 h。您可能会发现 h 的值为零。尝试在计算 h 之后添加以下行:

printf("h = %f\n", h);

通过使用浮点计算修复该问题后,您需要修复 while 循环。条件 x = b 绝对不是您想要的(我注意到在格式编辑之前它最初是 x == b ,但这也不正确)。

You are doing your calculations, in particular the initialisation of h, with integer arithmetic. So in the statement:

h = (b-a) / N;

a, b, and N are all integers so the expression is evaluated as an integer expression, and then converted to a float for assignment to h. You will probably find that the value of h is zero. Try adding the following line after the calculation of h:

printf("h = %f\n", h);

After you've fixed that by doing the calculations with floating point, you need to fix your while loop. The condition x = b is definitely not what you want (I noticed it was originally x == b before your formatting edit, but that's not right either).

旧竹 2024-11-07 04:33:23

while 条件应该是: while(x <= b)

Should the while condition be: while(x <= b)

伤感在游骋 2024-11-07 04:33:23

而(x=b);

没有办法退出循环。 b 始终为 1。

while (x = b);

There is no way to exit the loop. b is always 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文