Fit an Ordinary Differential Equation (ODE) using fmincon

7 vues (au cours des 30 derniers jours)
Jonas Hilpert
Jonas Hilpert le 19 Nov 2018
Commenté : Jonas Hilpert le 19 Nov 2018
I have a system of 2 ODE's (ode45) and want to fit them to measured data. For that i have 4 variables i want to optimate.
This is my code:
% ode45
tspan=linspace(0,3785,3785);
ic=[300 300];
x=ones(1,4);
f = @(t,T) [(x(1)*(T(2)-T(1))+x(2))/100; x(3)*(T(1)-T(2))+x(4)*(300-T(2))];
[t,T]= ode45(f,tspan,ic);
% ydata from excel
filename='Messung_1.xlsx';
sheet =12;
Range1='C6:C3790';
Range2='D6:C3790';
t_Kl=xlsread(filename,sheet,Range1)+273.15;
t_Nl=xlsread(filename,sheet,Range2)+273.15;
ydata=[t_Kl t_Nl];
% objective function
objective= @(x) sum(((T(:,1)-ydata(:,1))./ydata(:,1)).^2)+sum(((T(:,2)-ydata(:,2))./ydata(:,2)).^2);
x0=ones(1,4);
pbest=fmincon(objective,x0);
When i try to solve this: Initial point is a local minimum that satisfies the constraints.
Can someone help me please?
  2 commentaires
Torsten
Torsten le 19 Nov 2018
Modifié(e) : Torsten le 19 Nov 2018
Please show the complete code you are using - not only some snippets.
Your objective function as written does not depend on x. Thus it always returns the same value to "fmincon". This means that the initial point is a local minimum that satisfies the constraints.
Jonas Hilpert
Jonas Hilpert le 19 Nov 2018
allright i edited my question. How can i make my objective function to be depended on x?

Connectez-vous pour commenter.

Réponse acceptée

Torsten
Torsten le 19 Nov 2018
Modifié(e) : Torsten le 19 Nov 2018
function main
% ydata from excel
filename = 'Messung_1.xlsx';
sheet = 12;
Range1 = 'C6:C3790';
Range2 = 'D6:C3790';
t_Kl = xlsread(filename,sheet,Range1)+273.15;
t_Nl = xlsread(filename,sheet,Range2)+273.15;
ydata = [t_Kl t_Nl];
p0 = ones(1,4);
pbest = fmincon(@(p)objective(p,ydata),p0);
end
function diff = objective(p,ydata)
tspan = linspace(0,3785,3785);
ic = [300 300];
f = @(t,T,p) [(p(1)*(T(2)-T(1))+p(2))/100; p(3)*(T(1)-T(2))+p(4)*(300-T(2))];
[t,T] = ode45(@(t,y)f(t,y,p),tspan,ic);
diff = sum(((T(:,1)-ydata(:,1))./ydata(:,1)).^2)+sum(((T(:,2)-ydata(:,2))./ydata(:,2)).^2);
end

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by