Problems fitting couple differential equations to experimental data

7 次查看(过去 30 天)
I'm trying to fit experimental data with a system of differential equations. So far, I've followed what was done here, as it closely resembles what I'm trying to accomplish: https://www.mathworks.com/matlabcentral/answers/254566-how-do-i-fit-coupled-differential-equations-to-experimental-data
The equations are:
dP(t)/dt = kn*(10-M(t))^nc + km*M(t) - ka*P(t)^2
dM(t)/dt = kp*(10 - M(t))*P(t)
P(0) = M(0) = 0
My data consists of time, t, and fiber mass, M(t), so essentially I want to solve for M(t) to fit the data. If it helps, the M(t) can generally be fit by a sigmoidal function, but I'm using these equations because I would like to obtain the constants kn, nc, km, ka, and kp. I know there are a lot of parameters which makes fitting harder... Here is the code:
function [x] = knfit(t,f)
% t = time
% f = raw data
function S = prefun(B,t)
% variables: x(1) = P = P(t) x(2) = M = M(t)
% parameters:
% B(1) = kn
% B(2) = nc
% B(3) = km
% B(4) = ka
% B(5) = kp;
x0 = [0 0];
[~,Sv] = ode45(@DiffEq,t,x0);
function dS = DiffEq(t,x)
dS(1,:) = B(1)*(10-x(2))^B(2) + B(3)*x(2) - B(4)*x(1)^2;
dS(2,:) = B(5)*(10-x(2))*x(1);
end
S = Sv(:,1);
end
objfcn = @(B,t) prefun(B,t);
B0 = [0.001, 2, 0.0001, 1.1, 0.00002]; % initial values
x = lsqcurvefit(objfcn,B0,t,f);
end
The errors that I get right now are:
Matrix dimensions must agree.
Error in lsqcurvefit/objective (line 279)
F = F - YDATA;
I'm super confused because the x and y data definitely are the same dimensions. Does B0 have to be something different?
I'm pretty new at this so go easy on me! :) Thanks!

回答(2 个)

Alex Sha
Alex Sha 2020-4-6
Hi, if possible, upload your data (t, P and M) please

Star Strider
Star Strider 2020-4-6
Running your code with random data ran without error (R2020a):
t = (0:20)';
f = rand(size(t));
function [x] = knfit(t,f)
% t = time
% f = raw data
function S = prefun(B,t)
% variables: x(1) = P = P(t) x(2) = M = M(t)
% parameters:
% B(1) = kn
% B(2) = nc
% B(3) = km
% B(4) = ka
% B(5) = kp;
x0 = [0 0];
[~,Sv] = ode45(@DiffEq,t,x0);
function dS = DiffEq(t,x)
dS(1,:) = B(1)*(10-x(2))^B(2) + B(3)*x(2) - B(4)*x(1)^2;
dS(2,:) = B(5)*(10-x(2))*x(1);
end
S = Sv(:,1);
end
objfcn = @(B,t) prefun(B,t);
B0 = [0.001, 2, 0.0001, 1.1, 0.00002]; % initial values
x = lsqcurvefit(objfcn,B0,t,f);
end
x = knfit(t,f)
I am not exactly certain what the problem could be, other than it is possible that ode45 encountered a singularity (infinite value) and stopped integrating. The vectors would then not be the same lengths, however that would likely throw a different error.

类别

Help CenterFile Exchange 中查找有关 Least Squares 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by