# Solve optimization problem that iterates on parameters with constraints defined with numerical solutions to differential equations depending on those parameters

4 views (last 30 days)
Johan Poccard on 19 May 2020
Commented: Johan Poccard on 24 May 2020
I want to search for an optimum in a problem where the constraints are numerical solutions of differential equations (these equations don't have analytical solutions) but the parameters I want to iterate on are parameters that appear in the differential equation so that the solver would need to:
• compute the numerical solution to the differential equations with current parameters
• check if my constraints on the numerical solutions are verified and compute
• if they are not, change parameters and compute the numerical solutions again to see if it gets closer
• once they are, try to minimize my objective function (which is also a function of these parameters) which to complicate even more is the maximum value in a vector
I don't have a lot of code to show because I don't know where to begin but basically I would like to do this :
objective: minimize (max(k))
constraints:
I=1;
k01=0.1;
k12=0.1;
Fm=10;
Fg=0.1;
l=30*10^(-3);
tspan = [0 5];
end
[t,theta] = ode45(@(t,theta) odefcn(t,theta,k), tspan, theta0);
theta(t_final)-theta_objective < tol
I guess come optimization toolboxes offer this possibility but I can't find one.

Alan Weiss on 20 May 2020
Perhaps the example Fit an Ordinary Differential Equation (ODE) can help.
Alan Weiss
MATLAB mathematical toolbox documentation

Johan Poccard on 23 May 2020
Ok so I think I followed the example and adapted it accurately to my application but now I get the error 'LSQCURVEFIT requires all values returned by functions to be of data type double' which I don't understand since objective_fun returns pos which is similar to the one in the example.
Here's my code. I use an anonymous function objective_fun to pass extra parameters into the real objective function which is paramfun. paramfun itself calls dthetadt to comptute the diiferential equation system.
n=5;
k=[0.1 0.1 0.1 0.1 0.1 0.1];
tspan = [0 5];
Fm=10;
objective_fun=@(x,tspan)paramfun(x,tspan,n,theta0);
param0=[k, Fm];
xdata=5;
ydata=[0.4239 0.1086 0.0213 -0.1879 -0.3618];
[pbest] = lsqcurvefit(@(x,tspan)objective_fun,param0,xdata,ydata);
clear vars i
for i=2:n-1
end
end
function pos= paramfun(x,tspan,n,theta0)
k=x(1:n+1);
Fm=x(n+2);
Fg=0.1;
l=30*10^(-3);
I=1;
[~,pos]=ode45(@(t,theta) odefcn(t,theta,k,Fm,I,Fg,l,n),tspan,theta0);
end
Alan Weiss on 24 May 2020
When I run
tt = objective_fun(param0,tspan)
I get a value tt of size 53-by-10. The size of ydata is 1-by-5. Clearly, these are not the same. Take a look at the documentation of lsqcurvefit to see what your objective function should return, compared to ydata.
My guess is that you forgot that you have two values of time, 0 and 5, and you are giving data only for time 5.
Alan Weiss
MATLAB mathematical toolbox documentation
Johan Poccard on 24 May 2020
Thank you !