Fit an Ordinary Differential Equation (ODE) using fmincon
7 views (last 30 days)
Show older comments
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 Comments
Torsten
on 19 Nov 2018
Edited: Torsten
on 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.
Accepted Answer
Torsten
on 19 Nov 2018
Edited: Torsten
on 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
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations 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!