discrete time optimization with non-linear constraints

Technical Source
2 min readDec 24, 2020

Hello,

I have an optimization problem to solve with non-linear constraints. It is a control theory based discrete time model (which i feel fules out using fmincon) over a time horizon say N seconds.

I found a few old posts similar to this, but none I felt had clear answers.

I am going nuts trying to find a way to implement it in matlab. Which Matlab tool would be best suited in this case?

Thanks,

italic EDIT: the system is continuous, but we analyse it in a discrete time domain. Thus, variables have discrete values. There are n_v entities and each of the entity has each parameter described below:

variables: p,v,u; size(p)=size(v)=size(u)=(1,N) vectors

Obj. fn: minimize sum(u(1,:))

ANSWER:

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

I tried to create an example. Hope this helps.

function [x,f,eflag,outpt] = myModelParamsSolverxLast = []; % Last place computeall was called
myf = []; % Use for objective at xLast
myc = []; % Use for nonlinear inequality constraint
myceq = []; % Use for nonlinear equality constraint
N = 10;
x0 = rand(1,2+2*N);
fun = @objfun; % the objective function, nested below
cfun = @constr; % the constraint function, nested below
options = optimoptions('fmincon');
lb = zeros(1,2+2*N);
ub = ones(1,2+2*N);
% Call fmincon
[x,f,eflag,outpt] = fmincon(fun,x0,[],[],[],[],lb,ub,cfun,options);
function y = objfun(x)
if ~isequal(x,xLast) % Check if computation is necessary
[myf,myc,myceq] = myDiscreteTimeModel(x);
xLast = x;
end
% Now compute objective function
y = myf;
end
function [c,ceq] = constr(x)
if ~isequal(x,xLast) % Check if computation is necessary
[myf,myc,myceq] = myDiscreteTimeModel(x);
xLast = x;
end
% Now compute constraint functions
c = myc; % In this case, the computation is trivial
ceq = myceq;
end
function [f,c,ceq] = myDiscreteTimeModel(x)
p = zeros(1,N);
v = p;
u = p;
Ts = 0.01;
p_max = 100;
p_init = 10;
vel_init = 1;
% initial conditions:
p(1)=p_init;
v(1)=vel_init;
p1 = x(1);
p2 = x(2);
v1 = x(3:3+N-1);
v2 = x(3+N:end);
for n = 1:N
t = N*Ts;
% non linear equalities:
s_star= v1(n) * (v1(n)-v2(n));
% where v1 and v2 are parameters of entities 1 and 2 respectively at nth instant.
% * this is the non-linear equality/constraint.
%s_star2 = v1(n) * (v1(n)-v1(n-1));
% Note: we use either s_star or s_star2 in our code
% linear equalities:
p(n+1)= p(n)+ v(n)*t + 0.5*u(n)*t^2;
v(n+1)= v(n)+ u(n)*t;
u(n+1)= (s_star / (p1-p2))^2 ; % p1 and p2 correspond to entity 1 and 2's p parameter
end
% end conditions:
ceq = v(N);
c = p(N)-p_max;
f = sum(u(:));
end
end

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.