我很难解决从这个问题开始的问题。我需要创建一个程序,在一个区间内找到函数f(x)的最大值,小于或等于x小于或等于b,从x = a开始,步长为delta x。我应该对该程序做更多的工作,但首先我真的需要获得上面列出的基础编码。老实说,我不知道如何去做,如果有人能帮助我,我将不胜感激。
答案 0 :(得分:0)
如果您知道该函数,则可以得到导数,然后将其等于零。然后解决它以获得它的最大点。
示例:
f(x)=pow(x,2) + 2 * x
derivative is 2*x + 2
so its maximum can be found by:
2*x + 2 = 0
x = -2 / 2 ==> -1
x= -1 point is highest/ lowest part of the function. Then -inf and +inf are other minimum/maximum parts.
或者你将整个区域划分为一百万个部分并检查每个部分,因为你永远无法知道该函数在另一个山之后会上升/下降的位置。
答案 1 :(得分:0)
一般来说,
// initial value for this should be less than any value of f(x)
float max_fx = NEGATIVE_INFINITY;
// this syntax
// for (initialization statment; continuation test; change of state)
// does the {...} block some number of times until the continuation test fails
// and does the change of state after each run through
for (float x = a; x <= b; x = x + delta)
{
// first time the loop runs, x will equal a
// next time, x will equal a + delta
// then a + delta + delta
// etc
// compute the value
const float fx = f (x);
if (fx > max_fx) // note any fx will be greater than NEGATIVE_INFINITY
max_fx = fx;
}
答案 2 :(得分:0)
假设你知道这个函数,只需取导数并找到它的零点。然后将这些数字与f(a)和f(b)进行比较,看哪哪个最大。这是区间[a,b]的绝对最大值。
用于寻找多项式的导数:
d / dx [x ^ n] = n * x ^(n-1)和
d / dx [f(x)+ g(x)] = d / dx [f(x)] + d / dx [g(x)]和
d / dx [cf(x)] = c * d / dx [f(x)]
示例:
d / dx [3 * x ^ 4 - 2.6 * x ^ 3 + 0.97 * x ^ 1.5 + x - 5 * x ^ 0.3 + 2] =
(3 * 4)* x ^(4-1) - (2.6 * 3)* x ^(3-1)+(0.97 * 1.5)* x ^(1.5-1)+ 1 - (5 * 0.3)* x ^(0.3-1)+ 0 =
12 * x ^ 3 - 7.8 * x ^ 2 + 1.455 * x ^ 0.5 + 1 - 1.5 * x ^ -0.7