2011-04-07 48 views
4

我设计的算法来定义在区间[a,b]上问题找到一个函数的局部最大值用C

#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)的局部最大值的简单方法

该算法实现函数f(x)= A * sin(pi * x),其中A是我的ID的数字的平均值,并且inf变量被赋予一个数值,该数值远大于区间[a,b] = [0.1]中的函数。

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

+0

'int A = 4.875;'?哎呀:) – sarnold 2011-04-07 00:42:09

+0

yeap一个简单的错误...但是无关紧要..变量A可以取任何值 – franvergara66 2011-04-07 00:56:40

+0

在某些时候,你会考虑减少'funtion _()',所以你不要初始化结果为0,然后再次设置它。但编译器/优化器也可能会这样做。 – 2011-04-07 01:51:46

回答

3

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

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

你不能比较的int和平等float。它可能会工作一次,因为愚蠢的运气:)但你不能指望这个代码可靠地运作。

我强烈建议您更改所有int变量double,所有float变量double,所有的scanf(3)printf(3)调用相匹配。虽然你可以在一个程序中结合了不同的原始数字类型,甚至在一个表达式或语句中,执行中的细微差别将花费你几个小时的时间来发现。

此外,比较浮点格式的相等性几乎不是一个好主意。相反,比较差异两个数字之间的小量值:

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

,使之与问题的严重程度相匹配,您可能想规模您的小量;因为float真的只支持大约七个位的精度,这种比较就不能很好的工作:

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

您可能希望找到一个很好的介绍numerical analysis。 (顺便说一句,早在学校我最喜欢的课程之一。:)

更新

问题的核心是你的while(x == b)。我固定的和一些较小的问题,而这个代码似乎工作: 的#include 的#include 的#include 的#define PI 3.141592653 浮动funtion_(浮起,浮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 
$ 
+0

,但将变量的所有值更改为加倍,甚至仍然给我零局部最大值。什么可能做错了。 – franvergara66 2011-04-07 01:08:16

0

你正在做你的计算,特别是用整数运算初始化h。因此,在语句:

h = (b-a)/N; 

ab,和N均为整数所以表达式作为一个整数表达式求值,并然后转换为float用于分配给h。您可能会发现h的值为零。请尝试h计算后加入如下一行:

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

您已经解决了之后做浮点计算,你需要修复您的while循环。条件x = b绝对不是你想要的(我注意到它在格式编辑之前最初是x == b,但那也不正确)。

+0

是的你是对的这是一个错误的代码 – franvergara66 2011-04-07 00:49:37

+0

代码是固定的,但仍然给我的本地最大值负值inf – franvergara66 2011-04-07 00:55:41

+0

你还没有解决'x == b'的问题。这不是正确的循环终止条件。你将不得不解决这个问题。 (您的循环只运行一次。) – 2011-04-07 01:20:34

0

如若而条件是:当(X < = B)

0

而(X = B);

无法退出循环。 b总是1.

+0

代码已修复,但仍然给我了解您的想法的局部最大值负值inf – franvergara66 2011-04-07 00:57:15