How can I use fmincon objective function with different inputs and ODE

13 views (last 30 days)
Hi,
I am new at optimization on Matlab and need help about how to use fmincon. I know fmincon can find min x at f(x) but I have a little bit different problem to solve.
My code is below. I defined one array with two variable for fmincon (points). The thing that I want fmincon to do is by changing points in given internal find the minimum objective function.
Something is wrong and it gives the initial points as a solution and this message.
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the function tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
No active inequalities.
Maybe I could not explain what I mean. If you are confused please let me know so I can add new explanations :)
Thank you,
%------ main.m ------------------------------
global point_1 point_2
options = optimset('Algorithm','sqp');
A = [-1 0;0 -1;1 1];
b = [-900;-1500;8000];
points_initial = [1000;2000];
[points,feval] = fmincon(@ode_fun,points_initial,A,b);
%-----------------------------------------------------
%-----------------------------------------------------
function obj = ode_fun(points)
initial_condition = [0;1];
tspan = 0:0.1:180;
global point_1 point_2
point_1 = points(1);
point_2 = points(2);
[t,X] = ode45(@train_model,tspan,initial_condition);
obj = obj_fun_calc(t,X(:,2),X(:,1));
return
%------------------------------------------------------
function dx_dt= ode_model(t,x)
global point_1 point_2
if var1 < point_1
dx_dt= [...;...];
elseif (point_1 < var1)&&(var1 <= point_2)
dx_dt= [...;...];
elseif (point_2 < var1)&&(var1 <= point_3)
dx_dt= [...;...];
else
dx_dt= [...;...];
end
%-----------------------------------------------
function system = obj_fun_calc(t,x2,x1);
for i=1:length(t)
global point_1 point_2
if x2(i) <= point_1
sys(i) = ...;
elseif (point_1 < x2(i)) && (x2(i) <= point_2)
sys(i) = ...;
elseif (point_2 < x2(i)) && (x2(i) <= point_3)
sys(i) = 0.01;
else
sys(i) = 0.01;
end
system(i) = sys(i)*1500;
end

Accepted Answer

Matt J
Matt J on 4 Jul 2014
Edited: Matt J on 4 Jul 2014
Your skeleton code is missing too much information to say anything with certainty. ode45 is calling a function called training_model which isn't shown, though I'm guessing it's supposed to be the same as ode_model. ode_model refers to variables var1 and point_3 which are never defined. We cannot see the dependence of dx_dt on anything...
My primary guess as to the problem is that your function is piecewise constant as a function of the unknowns point_1 and point_2, because they only affect the function through if...else branching conditions. This is something you could easily verify by plotting ode_fun vs one of the unknowns and looking for flat regions. Piece-wise constant functions have local minima almost everywhere (and have undefined gradient everywhere else) so it makes sense that fmincon thinks your initial point is optimal.
The use of global variables looks very dangerous as well. You should get familiar with the better alternatives for attaching fixed data to functions described here,
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!