Fit an Ordinary Differential Equation (ODE) using fmincon
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Torsten
2018-11-19
编辑:Torsten
2018-11-19
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.
采纳的回答
Torsten
2018-11-19
编辑:Torsten
2018-11-19
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!