MATLAB: Parameter estimation and curve fitting

curve fittingparameter estimation

I have the parametric system of differential equations and real data and I want to estimate the parameters so that minimize the sum square error between real and expected data. The data:
time y(1) y(2) y(3) y(4)
1 957 43 0 0
2 857 110 33 0
3 557 387 51 5
4 342 496 150 12
5 224 465 280 31
6 123 407 350 120
7 79 291 440 190
8 35 226 385 354
9 29 111 320 540
10 17 80 215 688
11 2 50 150 798
12 0 28 92 880
13 0 25 55 920
14 0 5 37 958
15 0 7 18 975
16 0 1 4 995
17 0 0 2 998
18 0 0 2 998
19 0 0 1 999
20 0 0 0 1000
I made the system of differential equations
function dy=RModel_1(v,t,y)
dy=zeros(4,1);
N=y(1)+y(2)+y(3)+y(4);
dy(1)=-v(1)*y(1)*y(3)/N-v(2)*y(1);
dy(2)=v(1)*y(1)*y(3)/N-v(3)*y(2)-v(4)*y(2)*y(3)/N-v(5)*y(2);
dy(3)=v(2)*y(1)+v(3)*y(2)*y(2)+v(4)*y(2)*y(3)/N-v(6)*y(3);
dy(4)=v(5)*y(2)+v(6)*y(3);
end
and the optimization function of errors
function sse=RModer(v)
y0=[477.0 6.0 21.0 6.0];
[T Y]=ode23s(@(t,y) RModel_1(v,t,y),tdata,y0)
sse=sum((ydata-ys).^2);
end
Finally, I want to call a function such as fminsearch or lsqcurvefit or lsqnonlin to return the estimated parameters.
I understand that my code has mistakes. How can I overcome them?
Thank you

Best Answer

Dear Star Strider,
thank you for your response. Unfortunatelly, I haven't solve my problem yet. I applied your scripts, as you advised me. At first, they responsed but the results weren't logical. After some runs (playing with the initial conditions) there were no results. And now there is this message:
Error using lsqcurvefit (line 248)
Function value and YDATA sizes are not equal.
Error in MonodKinetics1_test (line 11)
[v,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@MonodKinetics1,v0,tdata,Ydata);
The scripts are the following:
The function:
function Y = MonodKinetics1(v, t)
y0 = rand(4,1)*1000;
[T,Yv] = ode45(@DifEq, t, y0);
function dS = DifEq(t, y)
dy=zeros(4,1);
N=y(1)+y(2)+y(3)+y(4);
dy(1)=-v(1)*y(1)*y(3)/N-v(2)*y(1);
dy(2)=v(1)*y(1)*y(3)/N-v(3)*y(2)-v(4)*y(2)*y(3)/N-v(5)*y(2);
dy(3)=v(2)*y(1)+v(3)*y(2)+v(4)*y(2)*y(3)/N-v(6)*y(3);
dy(4)=v(5)*y(2)+v(6)*y(3);
dS = dy;
end
Y = Yv(:,1);
end
and I run script:
load R_data.dat % loading data
format long
tdata=R_data(:,1);
Ydata=R_data(:,2:5);
v0 = rand(6,1);
[v,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@MonodKinetics1,v0,tdata,Ydata);
The data are described adove (first message).
Could you help me more, please?
I apprecciate everydoby who help me to continue with my project.
Thanks