1

I have a system of 2 ODEs and I want to fit this system to some data in order to estimate some model parameters.

There are two data sets. I want to fit the model to these two data sets simultaneously and the parameters should be the same when fitted for both data. The data I have is given as the total over a certain period. For example in data1 the first element is the total of state A during the time period from 2-4 hours. So, for each tiem interval I have the total over that time period in each data set. Because of this while I am simultaneously fitting to the two data sets I also have to fit through out the 4 time intervals (2-4,5-7,8-10,11-13).

If the data was given only for a single continuous time period such as time=2:13 I know how to estimate the parameters. But now I am not sure if the way I have implemented this is correct. This is the code that I have written.

%data

t=[2 3 4;5 6 7;8 9 10; 11 12 13];

data1=[5.399537437;6.762059387;7.34552533;7.675700967];
data2=[0.153279989125067;7.395870359956480;3.519156383296502;2.553056632299227];
%initial guesses
k0=[0.001;0.001;0.001];
lb=[0,0.0002,0];
ub=Inf*ones(1,3);
x={t,data1,data2};

[fittedVal,fval]=fmincon(@(k)ssq(k,x),k0,[],[],[],[],lb,ub)

function error=ssq(k,x)
    time=x{1,1};
    data1=x{1,2};
    data2=x{1,3};
    error=0;
    for it=1:4
        fitted=model(k,time(it,:));
        error=error+(sum((fitted-data1(it)).^2))+(sum((fitted-data2(it)).^2));
    end
end

function output= model(k,time)
    b0=[0;3.13];%initial conditions
    [time,values] = ode45(@Equations,time,b0);

function s=Equations(t,y)

    s= zeros(2,1);
    rep=0.204;
    ag=0.3368;
    carrying=10^9;
    carrying2=1*10^8;

    s(1)=(rep*y(1))*(1-(y(1)/carrying))-k(2)*ag*y(1)*(1-(y(2)/carrying2))+k(3)*y(2);
    s(2)=k(2)*ag*y(1)*(1-(y(2)/carrying2))+k(1)*y(2)-k(3)*y(2);

end
output=values(:,2)+values(:,1);

end

To fit for all for data points I am using a for loop. Is this the correct way to go about it?

In this I am not sure if I have to accumulate the error within the for loop.

Also, because these time points are related that is when I am starting the time period from 5-7, the data from 2-4 should have an impact, I want to change the initial conditions for the ode solver ode45 with the final value of the model results of the previous time point being as the initial conditions to the next time period. How can I do this because at the moment at each for loop iteration I have b0=[0;3.13]

sam_rox
  • 739
  • 3
  • 14
  • 29

0 Answers0