I need help using fmincon.

58 views (last 30 days)
Kendall Donahoe
Kendall Donahoe on 7 May 2023
Commented: Rik on 8 May 2023
I'm working on a project where I have to maximize a given function to find the best direction to hit an asteroid at the begining of the simulation.
the function set up is something like this ApophisOpt(exp_time,exp_yield,exp_x,exp_y,exp_z). so this is what I did.
>> f=@(x) -ApophisOpt(0.0,2e12,x(1),x(2),x(3));
>> x=fmincon(f,[1,1,1])
But when I ran it I ended up getting:
x =
1.0e+07 *
2.2209 -1.8149 -0.1287
These numbers would be fine if they weren't 1.0e+07. I'm wondering if I should put nonlinear constraints on when I run fmincon and how to do that or if there is any other way to get the desired answer using fmincon.
  2 Comments
John D'Errico
John D'Errico on 7 May 2023
The MATLAB cystal ball is so foggy. I just cannot see inside your computer or your mind today.
Should you put constraints on it? Possibly. That is probably the case no matter what, but you have not shown that you are even correctly modeling the process. Are your equations complete crapola? Possibly! Died you make an error? Again, the crystal ball says only something vague about a possible inheritance. Worthless crystal balls! This is what I get for buying the cheapest sale crystal ball.
What SHOULD you do?
  1. Verify the code, that is, your objective. Is it correctly (reasonably) modeling the process under study? Test the code!
  2. Look at where fmincon is trying to push things. Think about what that means. Is the objective function well posed? Can you move infinitely far out and see no real gain in the objective? Is it flat? Or is there some infinitely long valley?
  3. Look carefully at the parameters. Are there meaningful constraints one should apply? Probably. But since only you know what those parameters mean, it is only you who can do this.
In fact, since no code is shown, only you can do ANY of the above things.
Rik
Rik on 8 May 2023
@Kendall Donahoe regarding your flag: this is not rudeness, it is advice. You may not like it, but that does not make it rude. If you don't like the tone, try to ignore it and respond to the content.
Have a read here and here. It will greatly improve your chances of getting an answer.

Sign in to comment.

Answers (1)

LeoAiE
LeoAiE on 7 May 2023
It seems like the optimization algorithm is finding a solution that is far away from the expected range of values. You can add nonlinear constraints or bounds to the optimization problem using fmincon to prevent it from finding solutions outside the desired range.
You can set the lower and upper bounds for the variables using the lb and ub arguments in fmincon. For example, if you want to constrain exp_x, exp_y, and exp_z to be within the range [-1, 1]:
f = @(x) -ApophisOpt(0.0, 2e12, x(1), x(2), x(3));
% Lower and upper bounds
lb = [-1, -1, -1];
ub = [1, 1, 1];
% Call fmincon with bounds
x = fmincon(f, [1, 1, 1], [], [], [], [], lb, ub);
If you need to add more complex nonlinear constraints, you can define a function that returns the constraint values and their gradients (if necessary):
function [c, ceq, gradc, gradceq] = myConstraints(x)
% Add your constraints here
% Example: c(1) = x(1)^2 + x(2)^2 + x(3)^2 - 1;
% c(2) = ...;
c = ...;
% No equality constraints
ceq = [];
% If you want to provide gradients (optional)
% gradc = ...;
% gradceq = [];
end
Then, you can pass this function to fmincon:
x = fmincon(f, [1, 1, 1], [], [], [], [], [], [], @myConstraints);
Adjust the bounds or constraints according to the specific requirements of your problem. This should help guide the optimization algorithm to find a solution within the desired range.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!