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

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

 Respuesta aceptada

Matt J
Matt J el 4 de Jul. de 2014
Editada: Matt J el 4 de Jul. de 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 comentarios

Kemal commented:
Hi Matt, first thank you so much for your reply. My code is normally bigger than this and I tried to simplify it and as a result forgot something to put in it. As you mentioned, ODE45 is calling ode_model. New ode_model is below.
function dx_dt= ode_model(t,x)
global point_1 point_2
if x(1) < point_1
dx_dt= [x(2); var_2] % Here var_2 is a function of x(2)
elseif (point_1 < x(1))&&(x(1) <= point_2)
dx_dt= [x(2); var_3]
elseif (point_2 < x(1))&&(x(1) <= point_3)
dx_dt= [x(2); var_4]
else
dx_dt= [x(2); var_5]
end
Here x consist of two variables. x(1) is used in if loop and x(2) is used to calculate dx_dt. var_2 is a function of x(2) (equations are so long that's why I didn't add here).
I'll try to handle global variables but still don't know how to optimize this problem. You mentioned that fmincon is confused to find solution. What can I use instead of it or what do I need to change in my code?
Thank you,
You may need to use the Global Optimization Toolbox.
Thank you, this solves my problem.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 4 de Jul. de 2014

Comentada:

el 9 de Jul. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by