This is a follow-up to my previous question here
I've the following system of equations for studying information flow in the below graph,
$$ \frac{d \phi}{dt} = -M^TDM\phi + \text{noise effects} \hspace{1cm} (1)$$
Here, M is the incidence matrix of the graph
$\phi$ is a vector with variables [ A B C D E F].
I've solved the above odes to obtain the time series data of variables A,B,C,D,E,F.
Using the time-series data obtained from the above step, I'd like to do determine $\tilde{D}$ for the following system
$$ \frac{d \phi}{dt} = -M^T\tilde{D}M\phi \hspace{1cm} (2)$$
Note: The entries in the diagonal elements of $\tilde{D}$ are the edge weights.
In summary: Equation (1) (with noise effects) is solved using prior values of the diagonal matrix, D and the time series profiles of variables in each node are obtained. I want to determine a modified D i.e $\tilde{D}$ that can generate the same time series profile that was generated while solving equation (1).
Based on the solution provided in my previous post, I want to solve this as an optimization problem of the form $$\mathsf{K} = \int_{0}^{t_{f}} ||\phi(t) - \hat{\phi}(t)||^{2} dt$$
$$\tilde{D}, \hat{\phi}(0) = \text{argmin} \ \mathsf{K}(\tilde{D},\hat{\phi}(0)) = \text{argmin} \ \int_{0}^{t_{f}} ||\phi(t) - \exp{(-M^{T} \tilde{D} M t)} \hat{\phi}(0)||^{2} dt$$
I'd like to solve this optimization problem using fmincon in MATLAB.
The constraints will be the dynamical system presented in equation 1 above. I read through some of the procedures given in the literature and I want to use the trapezoidal rule to approximate dynamical constraints. However, I am not sure how to specify the constraints as non-linear equality constraints in MATLAB. Also, $\phi$ is a vector and I'd like to know if there is an easy way to express the constraints using the trapezoidal rule, i.e in a matrix form.
I'd also like to know if the integral form of the objective function should also be approximated using trapezoidal rule. Is it required to specify upper and lower bounds apart from the objective and equality constraints?
Any suggestions on how to proceed will be really helpful.
If there are examples for solving these kinds of problems, links to those will be useful.
EDIT: Template of implementation algorithm suggested by whpowell96
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