Is there a function to find parameter(s) that cause(s) a nonlinear function's solution to be within a range?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
I would like to know if there is a Matlab function that allows optimization of some input parameter X to cause the output of some nonlinear function MyFunc(X) to be constrained within a range. I also would like to be able to specify that X should be within some range. Essentially, I would like a function TheOptimizationFunc that does something like this:
X = TheOptimizationFunc( @(x)MyFunc(x), minOut, maxOut, minX, maxX )
MyFunc(X) < maxOutput % Should return TRUE
MyFunc(X) > minOutput % Should return TRUE
X < maxX % Should return TRUE
X > minX % Should return TRUE
I have looked at fmincon(), but I am not trying to minimize MyFunc(X); I am instead trying to constrain MyFunc(X) to be within a range, or as close as possible to the specified range if no solution exists within that range.
1 Comment
Walter Roberson
on 9 Mar 2017
What can be said about the nonlinear function? Is it monotonic over the restricted X range? Is it continuous? Does it have a single hill or valley in the right range?
Answers (2)
John D'Errico
on 9 Mar 2017
0 votes
Um, no. At least not simply. This is not really what fmincon does, as you have figured out.
There will generally be infinitely many solutions to your problem, whereas fmincon looks for an optimal solution. As you are posing the question, any solution that satisfies the goal will be as valid as any other solution.
You could think of your question in perhaps a different way. What is the locus of parameters X (in whatever n-dimensional space X lives in) that will yield the desired result, that MyFunc(X) satisfies the constraints. In general, if MyFunc is a nonlinear function, then the locus will probably be some nonlinear region of the parameter space. In fact, you could solve for the locus X such that MyFunc just attains the boundaries. To do that of course, we would need to understand the function.
Walter Roberson
on 9 Mar 2017
Suppose,
f(x) = x.^2; %nonlinear function
f_lb = 8; %lower bound permitted for f(x)
f_ub = 14; %upper bound permitted for f(x)
x_lb = 0; %lower bound permitted for x
x_ub = 1000; %upper bound permitted for x
g = @(x) (f(x)-max(f(x),f_lb)).^2+(f(x)-min(f(x),f_ub)).^2;
guess = rand() * (x_ub - x_lb) + x_lb; %some starting point
A = []; b = []; Aeq = []; beq = [];
good_x = fmincon(g, guess, A, b, Aeq, beq, x_lb, x_ub);
This will not stop at the first feasible value, but it will come up with a feasible value.
The code for g could be written more efficiently with a brief helper function,
function y = gh(x, f, f_lb, f_ub)
fx = f(x);
y = (fx-max(fx,f_lb)).^2+(fx-min(fx,f_ub)).^2;
and
g = @(x) gh(x, f, f_lb, f_ub);
The line
y = (fx-max(fx,f_lb)).^2+(fx-min(fx,f_ub)).^2;
could also be written as
y = (fx<f_lb) .* (f_lb-fx).^2 + (fx>f_ub) .* (fx-f_ub).^2;
which is to say "if the value is less than the lower bound, calculate the square of the distance to the lower bound; if it is above the upper bound, calculate the square of the distance to the upper bound; if it is in range, then 0".
Once within bounds, it will "bounce" a few times looking to see if there are places it can decrease. You could reduce that by using
options = optimoptions(@fmincon, 'ObjectiveLimit', eps);
and passing the options after the rest of the parameters I show. (Note: eps might not be good enough if the nonlinear function produces values close to the limits. Do not use 0: the ObjectiveLimit does not kick in until the function value goes below the limit.)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!