Using lsqcurvefit with ode45
4 views (last 30 days)
Show older comments
I am sure the mistakes I am making are trivial but I don't have a lot of Matlab experience. I am trying to fit the Gompertz equation to a data collected. I think my method is okay but am unsure. The error that I keep getting that I can't figure out why is, YDATA sizes are not equal. I have multiple other equations to fit but figure once I get the first the others will be easier. The data and calling statements
xdata = [0.0,9.0,20.0,32.0,43.0,54.0,65.0,76.0,82.0,87.0,93.0,98.0,107.0,114.0];
ydata = [228,299.18073738057,581.00354144283938,649.02400311257861,682.67522625884793,930.00499604295692,1218.69669597973279,1459.15756988933526,1899.88106764995837,2168.3622263782222 2560.85489811167236,2712.11916225644291,2925.15397096989545,3489.88898173570487];
[B,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@Gompertz1,B0,ydata,xdata);
Then the function % dV/dt=(a*V)*log(b/(V+c)); % Variables: x(1) = V, % Parameters: a= B(1), b = B(2), c = B(3)
function G = Gompertz1(B, t)
x0 = rand(1,1);
[T,Vx] = ode45(@DifEq, t, x0);
function dvdt = DifEq(t, x)
dvdt(1)=(B(1)*x(1))*log(B(2)/(x(1)+B(3)));
return
end
G = Vx(:,1);
0 Comments
Answers (1)
Star Strider
on 21 Nov 2017
I cannot figure out what you are doing. This runs, although the fit to the curve is not good and the parameters (and therefore the fit) are complex. (I added ‘B(4)’ as the initial condition as a parameter to be estimated.)
I leave it to you to sort those problems.
function G = Gompertz1(B, ydata)
x0 = B(4);
[T,Vx] = ode45(@DifEq, ydata, x0);
function dvdt = DifEq(ydata, x)
dvdt(1)=(B(1)*x(1))*log(B(2)/(x(1)+B(3)));
% return
end
G = Vx(:,1);
end
xdata = [0.0,9.0,20.0,32.0,43.0,54.0,65.0,76.0,82.0,87.0,93.0,98.0,107.0,114.0];
ydata = [228,299.18073738057,581.00354144283938,649.02400311257861,682.67522625884793,930.00499604295692,1218.69669597973279,1459.15756988933526,1899.88106764995837,2168.3622263782222 2560.85489811167236,2712.11916225644291,2925.15397096989545,3489.88898173570487];
B0 = rand(4, 1);
[B,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@Gompertz1,B0,ydata(:),xdata(:));
Parameters = B
DifEqn = @(t,x) (B(1)*x(1))*log(B(2)/(x(1)+B(3)));
[T,Vx] = ode45(DifEqn, ydata, B0(4));
figure(1)
plot(ydata, xdata, 'pg')
hold on
plot(T, Vx, '-r')
hold off
grid
2 Comments
Star Strider
on 21 Nov 2017
My pleasure.
The ODE solvers produce column data results, so the data you fit also have to be column data.
I do not understand the function you are integrating, so be certain you have implemented it correctly. It does not look like the Generalised Logistic Differential Equation (link).
I have no idea what causes the complex parameters, so first sort that out. The initial parameter estimates vector is probably the first place to look. The lsqcurvefit function allows you to constrain the parameters, so that could be an option if necessary.
Beyond that, since you are not integrating w.r.t. time, use your independent variable as the ‘tspan’ argument.
Those are my only suggestions.
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!