我设计的算法来定义在区间[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]中的函数。
该算法必须找到该函数的局部最大值,但它的最大回报总是为零。不明白为什么。我的解决方案的逻辑可能是什么问题?,这个问题可以通过这个简单的算法来解决,或者通过回溯来进行一些优化是必要的?感谢您的回应。
'int A = 4.875;'?哎呀:) – sarnold 2011-04-07 00:42:09
yeap一个简单的错误...但是无关紧要..变量A可以取任何值 – franvergara66 2011-04-07 00:56:40
在某些时候,你会考虑减少'funtion _()',所以你不要初始化结果为0,然后再次设置它。但编译器/优化器也可能会这样做。 – 2011-04-07 01:51:46