nonlinear data fitting for a system of ODE using lsqcurvefit (finding unknown parameters)

I have a set of experimental data in staedy state condtion(without time dependent). This experiment has been done at 4 different concentration including 3 compounds. the values of concentration at inlet(x=0) and outlet(x=1) are known. i used ODE15s to solve the differenial equations and find the values at outlet and then compare them with real values to find the unknown kinitic parameters(i followed https://www.mathworks.com/matlabcentral/answers/43439-monod-kinetics-and-curve-fitting#comment_89455). but when i ran the code i got some error as below: Not enough input arguments.
Error in Untitled10>objfun (line 25)
[tSol,YSol]=ode15s(@diffeq,x,Z);
Error in lsqcurvefit (line 202)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in Untitled10 (line 20)
[kfit,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@objfun,K,Cin,yy)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.

3 Comments

clc
clear all
global K
Y0=[0 0 0
293.3 2.141445291 3.688379481
462.8 2.536287882 4.200913729
966.2 2.352145581 6.525317012
1920 2.454642817 11.40142937
];
K=[1 2 1];
dt=0.001;
x=[0:dt:0.02]';
Cin=[0;200;500;1000;2000];
yy=[0 0 0
208.0886316 33.822843 78.56152111
376.5730526 31.98339108 168.7154254
802.9630526 29.05423171 204.8119954
1770 35.44630378 295.906807
];
[kfit,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@objfun,K,Cin,yy)
function [C]=objfun(K,Y0,x)
for i=1:5
Z=Y0(i,:);
[tSol,YSol]=ode15s(@diffeq,x,Z);
C(i,:)=YSol(end,:)
end
end
function dYdt = diffeq(x,Y)
global K
k1=K(1);
k2=K(2);
k3=K(3);
dadt=-k1*Y(1)/(1+k1*Y(1));
dbdt=k1*Y(1)/(1+k1*Y(1))-k2*Y(2);
dcdt=k2*Y(2)-k3*Y(3);
dYdt=[dadt;dbdt;dcdt];
end
@Mojtaba Malayeri — Please post the image you included with your previous (now deleted) Question.
It is essential to understanding what you want to do.
@Star Strider that image was my experimental data, it is included in my code now.

Sign in to comment.

 Accepted Answer

[kfit,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@(K) objfun(K,Y0,x),K,Cin,yy)

23 Comments

thank you for your response, but i got error again:
Error using Untitled10>@(K)objfun(K,x,Y0)
Too many input arguments.
Error in lsqcurvefit (line 202)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in Untitled10 (line 20)
[kfit,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@(K) objfun(K,x,Y0),K,Cin,yy)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Your objective function doesn't seem to have any dependence on Cin. It should look like this
yy_predicted = objfun(K,Cin_data)
becaue this problem is steady-stat, i dont have data at different x between x=0:1 (i have only data of inlet and outlet). but it is done at different concentration; so Y0 is my inlet and yy is my outlet. each number in C_in related to a row of matrix Y0. I hope I have explained it well
Then, shouldn't you have this?
lsqcurvefit(@(a,b) objfun(a,b,x), K,Y0,yy)
sorry, what are a and b here? actually, Y0 should be replaced instead of Cin but it is a vector 5*3. i got erorr
They are the names of inputs to fun(a,b) where fun is the two-argument anonymous function created as follows,
fun = @(a,b) objfun(a,b,x) ;
i replaced this function in main file: [kfit,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@(K,Y0) objfun(K,Y0,x), K,Y0,yy) but i got following message and can not be calculated: Optimization completed because the size of the gradient at the initial point is less than the default value of the optimality tolerance.
This runs for me without error messages:
Y0=[0 0 0
293.3 2.141445291 3.688379481
462.8 2.536287882 4.200913729
966.2 2.352145581 6.525317012
1920 2.454642817 11.40142937
];
K0=[1 2 1]; %<---Change
dt=0.001; %<---Change
x=[0:dt:0.02]';
%Cin=[0;200;500;1000;2000]; %<---Change
yy=[0 0 0
208.0886316 33.822843 78.56152111
376.5730526 31.98339108 168.7154254
802.9630526 29.05423171 204.8119954
1770 35.44630378 295.906807
];
[kfit,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=...
lsqcurvefit(@(a,b) objfun(a,b,x),K0,Y0,yy) %<---Change
function [C]=objfun(K,Y0,x)
for i=1:5
Z=Y0(i,:);
[tSol,YSol]=ode15s(@(a,b)diffeq(a,b,K) ,x,Z); %<---Change
C(i,:)=YSol(end,:);
end
end
function dYdt = diffeq(x,Y,K)
%global K <---Change
k1=K(1);
k2=K(2);
k3=K(3);
dadt=-k1*Y(1)/(1+k1*Y(1));
dbdt=k1*Y(1)/(1+k1*Y(1))-k2*Y(2);
dcdt=k2*Y(2)-k3*Y(3);
dYdt=[dadt;dbdt;dcdt];
end
Yes, it works now. thank you so much. i really appriciate. just my fitting value are negative. how can i add possitive constrain for unknown fitting parameters??
You're welcome, but please Accept-click the answer if it solved your problem. To add positivity constraints:
lowerbounds=[0;0;0];
... = lsqcurvefit(@(a,b) objfun(a,b,x),K0,Y0,yy,lowerbounds);
i replace a lowerbound as follow, but i one of my parameter is negative: [kfit,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=... lsqcurvefit(@(a,b) objfun(a,b,x),K0,Y0,yy,[0;0;0]) another issue is that Rsdnrm =7.1019e+04 that is very large error
You need to make lowerbounds a vector, like in my example.
yes, sorry i made mistake. but Rsdnrm = 2.1754e+05 is reasonable??
Who knows? If you re-run as below, it will solve the exact same optimization problem, but give you a much smaller Rsdnrm,
[kfit,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=...
lsqcurvefit(@(a,b) objfun(a,b,x)/1e5,kfit,Y0,yy/1e5,[0;0;0])
because i have to use these parameters in another model to find the exact result. so it is very important to find the accurate values for these parameters.
How well is it working on simulated Y0,yy data (for which you know the true K)?
the error is very large. i even change the initial gusses for fitting parameters, but the erorr doesn't change.
i checked it, but there are big differenc between them: simulated=[0 0 0 293.2800001 0.000216393 5.849608311 462.78 0.000216393 6.756985175 966.18 0.000216393 8.897246179 1919.98 0.000216393 13.87585578]
real value=[0 0 0 208.0886316 33.822843 78.56152111 376.5730526 31.98339108 168.7154254 802.9630526 29.05423171 204.8119954 1770 35.44630378 295.906807]
i changed every parameters, but the erorr value is fixed at 2.17e+05. i don't know where this issue come from!!!!!!!!!
you mean i have to change my optimization solver instead of lsqcurvefit??
No. The link I gave you gives advice applicable to all solvers.
Thanks for the suggestion. i implimened all case of options, but the issue still persists. very large erorr.

Sign in to comment.

More Answers (0)

Asked:

on 2 Nov 2018

Edited:

on 3 Nov 2018

Community Treasure Hunt

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

Start Hunting!