# Question:Inconsistent behavior with dsolve/numeric and NLPSolve (sqp)

## Question:Inconsistent behavior with dsolve/numeric and NLPSolve (sqp)

Maple 2023

dsolve/numeric + NLPSolve shows inconsistent behavior. This combination is important for parameter estimation and optimal control. Can anyone fix this? Hopefully, I am not making a mistake.

 > restart:
 >

Test code written by Dr. Venkat Subramanian at UT Austin, 05/31/2023. This code uses CVP approach (piecwise constant) to perform optimal control. NLPSolve combination with dsolve numeric parametric form is buggy and fails for some values of nvar, and works for some values of nvar. Ideally increasing nvar should show convergence with respect to the objective function.

 > restart:
 > Digits:=15; >
 > eqodes:=[diff(ca(t),t)=-(u+u^2/2)*1.0*ca(t),diff(cb(t),t)=1.0*u*ca(t)-0.1*cb(t)]; > soln:=dsolve({op(eqodes),ca(0)=alpha,cb(0)=beta},type=numeric,'parameters'=[alpha,beta,u],compile=true,savebinary=true):
 >
 >
 > ss:=proc(x) interface(warnlevel=0): #if  type(x,numeric) if  type(x,Vector) then local z1,n1,i,c10,c20,dt,u; global soln,nvar; dt:=evalf(1.0/nvar): c10:=1.0:c20:=0.0: for i from 1 to nvar do u:=x[i]: soln('parameters'=[c10,c20,u]): z1:=soln(dt): c10:=subs(z1,ca(t)):c20:=subs(z1,cb(t)): od: -c20;  else 'procname'(args): end if: end proc:
 >
 > nvar:=3;#code works for nvar:=3, but not for nvar:=2 > ic0:=Vector(nvar,[seq(0.1,i=1..nvar)],datatype=float); >
 > ss(ic0); > bl := Vector(nvar,[seq(0.,i=1..nvar)]);bu := Vector(nvar,[seq(5.,i=1..nvar)]);  > infolevel[all]:=1; > Optimization:-NLPSolve(nvar,ss,[],initialpoint=ic0,[bl,bu]);

NLPSolve: calling NLP solver

NLPSolve: using method=sqp

NLPSolve: number of problem variables 3

NLPSolve: number of nonlinear inequality constraints 0

NLPSolve: number of nonlinear equality constraints 0

NLPSolve: number of general linear constraints 0 > nvar:=2;#code works for nvar:=3, but not for nvar:=2 > ic0:=Vector(nvar,[seq(0.1,i=1..nvar)],datatype=float); >
 > ss(ic0); > bl := Vector(nvar,[seq(0.,i=1..nvar)]);bu := Vector(nvar,[seq(5.,i=1..nvar)]);  > infolevel[all]:=1; > Optimization:-NLPSolve(nvar,ss,[],initialpoint=ic0,[bl,bu]);

NLPSolve: calling NLP solver

NLPSolve: using method=sqp

NLPSolve: number of problem variables 2

NLPSolve: number of nonlinear inequality constraints 0

NLPSolve: number of nonlinear equality constraints 0

NLPSolve: number of general linear constraints 0

Error, (in Optimization:-NLPSolve) no improved point could be found

 >
 > nvar:=5;#code works for nvar:=3, but not for nvar:=2 > ic0:=Vector(nvar,[seq(0.1,i=1..nvar)],datatype=float); >
 > ss(ic0); > bl := Vector(nvar,[seq(0.,i=1..nvar)]);bu := Vector(nvar,[seq(5.,i=1..nvar)]);  > infolevel[all]:=1; > Optimization:-NLPSolve(nvar,ss,[],initialpoint=ic0,[bl,bu]);

NLPSolve: calling NLP solver

NLPSolve: using method=sqp

NLPSolve: number of problem variables 5

NLPSolve: number of nonlinear inequality constraints 0

NLPSolve: number of nonlinear equality constraints 0

NLPSolve: number of general linear constraints 0 > 