user2078683
user2078683

Reputation: 71

How to calculate max value of function in range?

I have some function (for example, double function(double value)), and some range (for example, from A to B). I need to calculate max value of function in this range. Are there existed libraries for it? Please, give me advice.

Upvotes: 0

Views: 1339

Answers (4)

808sound
808sound

Reputation: 8404

If the function needs to handle floating-point values, you're going to have to use something like Golden section search. Note that for this specific method, there are significant limitations regarding the functions that can be handled (specifically it must be unimodal). There are some adjustments you can make to the algorithm which extend it to more functions, specifically these modifications will allow it to work for continuous functions.

Upvotes: 2

NominSim
NominSim

Reputation: 8511

If you do not have any indication of what the functions inner workings are (i.e. the function is a black box that accepts an input and produces an output), there is no "easy" way to find the global maximum.

There are an infinite number of points to choose for your input (technically) so "iterating over all possible inputs" is not feasible mathematically.

There are various algorithms that will give you estimated maximum values ina function like this:

The hill climbing algorithm, and the firefly algorithm are two, but there are many more. This is a fairly well documented/studied computer science problem and there is a lot of material online for you to look at. I suggest starting with the hill climbing algorithm, and maybe expanding out to other global optimization algorithms.

Note: These algorithms do not guarantee that the result is the maximum, but provide an estimate of its value.*

Upvotes: 0

Khoa Nghiem
Khoa Nghiem

Reputation: 315

I do not know if there are any librairies in Java for your problem. But I know you can easily do that with MatLab (or Octave for the OpenSource equivalent).

Upvotes: 0

JohnFilleau
JohnFilleau

Reputation: 4288

Is this a continuous function, or a set of discrete values? If discrete values, then you can either iterate over all values, and set max/min flags as 808sound suggests, or you can load all values into an array.

If it's a continuous function, then you can either populate an array with the function's value at discrete inputs, and find the max as above, or if it's differentiable, then you can use basic calculus to find the points at which df(x)/dx are 0. The latter case is a little more abstract, and probably more complicated than you want, though?

A quick google search led me to this: http://code.google.com/p/javacalculus/

But I've never used it myself, so I don't know if that implements the required functionality. It does differential equations, though, so I assume they'd have "baby stuff" like basic differentiation.

Upvotes: 0

Related Questions