Solving an optimization problem in fmincon with nonlinear constraints
Show older comments
Hi All,
I have the following dynamical systems.
Equation 1:
Equation 2:

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, upon optimization. For the sake of understanding, let us assume eq(1) gives experimental values and equation 2 are the predicted values.
The objective function defined has a cost function that minimizes the difference between state variables ϕ and
, by optimizing parameter
which are the control variables.
which are the control variables. I'm trying to solve this as a parameter estimation problem with non-linear equality constraints .The non-linear constraints have been written by discretizing equation (2) using trapezoidal collocation.
I have defined function [c ceq] = defects(
) that accepts as input argument
and the
and d
values necessary for setting up the constraints are obtained by calling a nested function defined within defects` .This nested function accepts input arguments ode15s(@(t,
) fun(t,
,
)`.
) that accepts as input argument
and the
)`.I've implemented the above , a few iterations are carried out by fmincon and there is a dimension error that pops up. I am not sure why this occurs after a few iterations and not at the first iteration itself.
Error using cellfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 1 in dimension 1. Input #4 has size 51
Error in cse_03_19_20/defects (line 117)
f = cellfun(@predicted,num2cell(t),num2cell(phi_tilde,2),num2cell(Dhat_mat,2),'UniformOutput',false);
Error in barrier
Error in fmincon (line 824)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in cse_03_19_20 (line 9)
Dhat= fmincon(@objfun,Dhat0,[],[],[],[],[],[],@defects)
Could someone help me in finding out what leads to dimension error after a few iterations? Please find the code attached
EDIT: I've updated the file attached and also used dbstop if error as suggestd below
Now the error that occurs is
Matrix dimensions must agree.
Error in cse_03_19_20/objfun (line 31)
f = (phi - phi_tilde).*(phi - phi_tilde);
Error in barrier
Error in fmincon (line 824)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in cse_03_19_20 (line 10)
Dhat = fmincon(@objfun,Dhat0,[],[],[],[],[],[],[], opts)
But the same code gives optimal solution when lsqnonlin is used instead of fmincon.
Any suggestions on why fmincon fails to give optimal solution will be helpful
20 Comments
Deepa Maheshvare
on 27 Mar 2020
Ameer Hamza
on 27 Mar 2020
I tried running your code. I didn't get any such error, but the fmicon was also not able to reach an optimal solution. To track down the cause of this error, you can type this in the command window
dbstop if error
and then run your code. This will tell MATLAB to halt execution whenever the error occurs and show you the line of code. You can then check the values of the variable and try to run the line manually to see what is causing the error.
Deepa Maheshvare
on 27 Mar 2020
Ameer Hamza
on 27 Mar 2020
I tried the code with both fmincon and lsqnonlin without any error. Both give comparable solutions. Have you attached the exactly same code you are running?
fmincon:
Dhat =
1.0e+03 *
4.9760
4.8535
5.3041
5.0159
5.1509
4.3784
4.7378
7.9296
5.0528
lsqnonlin:
Dhat =
1.0e+03 *
5.0608
4.9864
5.0414
5.1345
5.1632
5.0217
4.5965
3.7518
2.2228
Deepa Maheshvare
on 27 Mar 2020
Deepa Maheshvare
on 27 Mar 2020
Edited: Deepa Maheshvare
on 27 Mar 2020
Deepa Maheshvare
on 28 Mar 2020
Ameer Hamza
on 28 Mar 2020
@Deepa, i again got the same result with fmincom and lsqnonlin. However, Walter also reported the same error as you. I wonder if this related to the version of MATLAB. I tried it on R2020a.
Walter Roberson
on 28 Mar 2020
I agree, I do not see the problem in R2020a, but do see it in R2019b.
Deepa Maheshvare
on 28 Mar 2020
Deepa Maheshvare
on 28 Mar 2020
Ameer Hamza
on 28 Mar 2020
Deepa, as Walter mentioned that it works fine for R2020a but fails for R2019b. This shows that Mathworks has improved the fmincon function with their new release. I am not sure how to solve the problem in R2019b, but as Walter mentioned, when Dhat becomes large, the gradient estimation fails. So you somehow need to reformulate the problem to avoid the infinities.
Walter Roberson
on 28 Mar 2020
Same problem in R2018b.
Deepa Maheshvare
on 28 Mar 2020
Deepa Maheshvare
on 28 Mar 2020
Deepa Maheshvare
on 28 Mar 2020
Edited: Deepa Maheshvare
on 29 Mar 2020
Ameer Hamza
on 28 Mar 2020
fmincon and lsqnonlin use diifferent optimization algorithm. You can calculate the value of objective function at both solutions to see if there is a significant difference.
Deepa Maheshvare
on 29 Mar 2020
Ameer Hamza
on 29 Mar 2020
Theoretically, you can write down your objective function and analytically find its gradient by using a little bit of knowledge about multivariate calculus linear algebra. However, I don't think it will significantly improve the performance since fmincon and lsqnonlin are already given the solution of the same order of magnitude.
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

