28 views (last 30 days)

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

Alan Weiss
on 24 Mar 2020

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

Alan Weiss
on 24 Mar 2020

You must be able to determine exactly what your control variables are. If those variables you mentioned are not control variables, then you should not be using them in your nonlinear constraint function except possibly as extra parameters (as in Passing Extra Parameters).

Please do not attach detailed documentation. Just say what are the control variables. If you cannot explain concisely exactly what are your control variables, then I will not be able to help.

Alan Weiss

MATLAB mathematical toolbox documentation

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/512266-how-to-set-up-parameter-estimation-in-fmincon#comment_814551

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/512266-how-to-set-up-parameter-estimation-in-fmincon#comment_814551

Sign in to comment.