MATLAB Answers

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)
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;
theta_0=deg2rad(5);
k12=0.1;
theta_2=deg2rad(5);
Fm=10;
Fg=0.1;
l=30*10^(-3);
tspan = [0 5];
theta0 = [deg2rad(10) 0];
function dthetadt = odefcn(t,theta,k,I,theta_0,theta_2,Fm,Fg,l,n)
dthetadt = zeros(2,1);
dthetadt(1) = theta(2);
dthetadt(2) = (1/I)*(-k(1)*(theta(1)-theta_0)-k(2)*(theta(1)-theta_2)+Fm*l*sin(theta(1))-Fg*l*cos(theta(1)));
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.
Thanks in advance.

  0 Comments

Sign in to comment.

Accepted Answer

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

  7 Comments

Show 4 older comments
Johan Poccard
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];
theta0 = [deg2rad(linspace(5,-5,n)) zeros(1,n)];
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);
function dthetadt = odefcn(t,theta,k,Fm,I,Fg,l,n)
dthetadt = zeros(2*n,1);
clear vars i
dthetadt(1) = theta(n+1);
dthetadt(n+1) = (1/I)*(-k(1)*(theta(1))-k(2)*(theta(1)-theta(2))+Fm*l*sin(theta(1))-Fg*l*cos(theta(1)));
for i=2:n-1
dthetadt(i) = theta(n+i);
dthetadt(n+i) = (1/I)*(-k(i)*(theta(i)-theta(i-1))-k(i+1)*(theta(i)-theta(i+1))+Fm*l*sin(theta(i))-Fg*l*cos(theta(i)));
end
dthetadt(n) = theta(2*n);
dthetadt(2*n) = (1/I)*(-k(n)*(theta(n)-theta(n-1))-k(n+1)*(theta(n))+Fm*l*sin(theta(n))-Fg*l*cos(theta(n)));
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
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

Sign in to comment.

More Answers (0)


Translated by