C 中求函数局部最大值的问题
我正在设计一种算法来定义一个简单的方法,能够找到在区间 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这段代码有几个问题;可能最明显的是:
您无法比较
int
和float
是否相等。由于运气不好,它可能偶尔会工作一次:),但你不能指望这段代码能够可靠地工作。我强烈建议将所有
int
变量更改为double
,将所有float
变量更改为double
,并将所有float
变量更改为double
,并将所有 < code>scanf(3) 和printf(3)
调用进行匹配。虽然您可以在一个程序中组合不同的原始数字类型,甚至在一个表达式或语句中,但执行中的细微差别将需要您花费数小时才能发现。此外,比较浮点格式是否相等几乎从来都不是一个好主意。相反,将两个数字之间的差异与epsilon值进行比较:
您可能需要缩放您的epsilon,以便它与您的问题的规模相匹配;由于
float
实际上只支持大约七位数的精度,因此这种比较效果不佳:您可能想找到一个关于 数值分析。 (顺便说一句,这是我在学校最喜欢的课程之一。:)
更新
问题的核心是你的
while(x == b)
。我修复了这个问题和一些小问题,这段代码似乎可以工作:#包括
#包括
#包括
#定义PI 3.141592653
浮点函数_(浮点a,浮点x)
{
使用一些小输入运行该程序:
Several problems with this code; probably the most glaring is:
You cannot compare an
int
and afloat
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 todouble
, all yourfloat
variables todouble
, and all thescanf(3)
andprintf(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:
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: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)
{
Running this program with some small inputs:
您正在使用整数算术进行计算,特别是
h
的初始化。所以在语句中:a
、b
和N
都是整数,因此表达式被计算为整数表达式,然后 转换为浮点数以分配给h
。您可能会发现h
的值为零。尝试在计算h
之后添加以下行:通过使用浮点计算修复该问题后,您需要修复
while
循环。条件x = b
绝对不是您想要的(我注意到在格式编辑之前它最初是x == b
,但这也不正确)。You are doing your calculations, in particular the initialisation of
h
, with integer arithmetic. So in the statement:a
,b
, andN
are all integers so the expression is evaluated as an integer expression, and then converted to a float for assignment toh
. You will probably find that the value ofh
is zero. Try adding the following line after the calculation ofh
:After you've fixed that by doing the calculations with floating point, you need to fix your
while
loop. The conditionx = b
is definitely not what you want (I noticed it was originallyx == b
before your formatting edit, but that's not right either).while 条件应该是: while(x <= b)
Should the while condition be: while(x <= b)
而(x=b);
没有办法退出循环。 b 始终为 1。
while (x = b);
There is no way to exit the loop. b is always 1.