How to set up parameter estimation in fmincon

14 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2020 年 3 月 22 日
コメント済み: Deepa Maheshvare 2020 年 3 月 26 日
Hi All,
I have the following dynamical systems. Equation 1 represents the exact dynamics of a system and equation 2 is the approximate dynamics that will give the same time course profiles as equation 1. To get the same time course profiles I have to determine D_hat in equation 2. I'm trying to solve this as a parameter estimation problem.
Dhat0 = %input vector
% fun = @objfun;
% [Dhat,fval] = fminunc(fun, Dhat0)
%% lsqnonlin
Dhat = lsqnonlin(@(Dhat) objfun(Dhat),Dhat0)
function f = objfun(Dhat)
%% Integrator settings
tspan = %tspan
options = odeset('abstol', 1e-10, 'reltol', 1e-9);
%% generate exact solution
phi0 = % initial condition vector
[t, phi] = ode15s(@(t,phi) exact(t,phi), tspan , phi0 ,options);
%% generate approximate solution
[t, phi_tilde] = ode15s(@(t,phi_tilde) approx(t,phi_tilde, Dhat), tspan , phi0 ,options);
%% objective function for fminunc
% diff = (phi - phi_tilde).*(phi - phi_tilde);
% f = sum(diff, 'all')
%% objective function for lsqnonlin
f = phi - phi_tilde
end
Using the above code, I could estimate D_hat in lsqnonlin.
Now, I am trying to solve this as an optimal conrol problem by using the equation (1) as dynamical constraints (non-linear equality constraints)
in fmincon.
I'm trying to set up the problem like the following
nonlcon = @defects;
Dhat= fmincon(@objfun,Dhat0,A,b,Aeq,beq,lb,ub,nonlcon)
For my system, A,b,Aeq,beq,lb,ub = []
But, I am not sure from where to pass these arguments for defects(dt,x,f).
function [c ceq] = defects(dt,x,f)
% ref: https://github.com/MatthewPeterKelly/OptimTraj
% This function computes the defects that are used to enforce the
% continuous dynamics of the system along the trajectory.
%
% INPUTS:
% dt = time step (scalar)
% x = [nState, nTime] = state at each grid-point along the trajectory
% f = [nState, nTime] = dynamics of the state along the trajectory
%
% OUTPUTS:
% defects = [nState, nTime-1] = error in dynamics along the trajectory
% defectsGrad = [nState, nTime-1, nDecVars] = gradient of defects
nTime = size(x,2);
idxLow = 1:(nTime-1);
idxUpp = 2:nTime;
xLow = x(:,idxLow);
xUpp = x(:,idxUpp);
fLow = f(:,idxLow);
fUpp = f(:,idxUpp);
% This is the key line: (Trapazoid Rule)
defects = xUpp-xLow - 0.5*dt*(fLow+fUpp);
ceq = reshape(defects,numel(defects),1);
c = [];
end
end
Any suggestions will be really helpful
  1 件のコメント
Deepa Maheshvare
Deepa Maheshvare 2020 年 3 月 24 日
Any suggestions?

サインインしてコメントする。

回答 (1 件)

Alan Weiss
Alan Weiss 2020 年 3 月 24 日
I'm not sure, but I think what you are asking is how to pass several arguments as control variables, and maybe how to pass extra parameters (those that are not control variables, meaning you don't optimize over those variables).
If all of your variables dt, x, and f are control variables, then as documented you must put all of them in one array, typically called x, but I don't want to confuse it with your x variable, so I'll call the control variable v. Basically, you want v = [dt(:);x(:);f(:)]; In other words, v is a vector whose first component(s) are the dt entries, the next set are the x entries, and the last bits are the f entries. You code would look something like this:
function [c ceq] = defects(v)
J = (length(v) - 1)/2;
% INPUTS:
% dt = time step (scalar)
dt = v(1);
% x = [nState, nTime] = state at each grid-point along the trajectory
x = v(2:(J+1));
% f = [nState, nTime] = dynamics of the state along the trajectory
f = v((J+1):end);
% OUTPUTS:
% defects = [nState, nTime-1] = error in dynamics along the trajectory
% defectsGrad = [nState, nTime-1, nDecVars] = gradient of defects
nTime = size(x,2);
idxLow = 1:(nTime-1);
idxUpp = 2:nTime;
xLow = x(:,idxLow);
xUpp = x(:,idxUpp);
fLow = f(:,idxLow);
fUpp = f(:,idxUpp);
% This is the key line: (Trapazoid Rule)
defects = xUpp-xLow - 0.5*dt*(fLow+fUpp);
ceq = reshape(defects,numel(defects),1);
c = [];
end
end
If I am wrong and you don't want to optimize all of these variables, then see Passing Extra Parameters.
Alan Weiss
MATLAB mathematical toolbox documentation
  6 件のコメント
Deepa Maheshvare
Deepa Maheshvare 2020 年 3 月 26 日
Hi Alan,
My control variables are defined in equation2 in original post (re-written in terms of x below).
Matrix, M is known.I want to determine that minimizes the cost function.
The non-linear constraints have been written by discretizing equation (2) using trapezoidal collocation.
After what you suggested, I have defined function [c ceq] = defects(v) that accepts as input argument v and the x and dx values necessary for setting up the constraints are obtained by calling a nested function defined within defects` .This nested function has accepts input arguments ode15s(@(t,phi_tilde) fun(t,x, )`.
I hope this is right
Deepa Maheshvare
Deepa Maheshvare 2020 年 3 月 26 日
However, I ran into a dimension error and I have opened a new thread here

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeQuadratic Programming and Cone Programming についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by