Curve fit autocatalytic model
5 views (last 30 days)
Show older comments
Hi,
I want to curve fit a custom equation to my dataset.
I have a dataset for cure kinetics and want to curve fit the autocatalytic model y = k*x^m*(1-x)^n to evaluate the rate constant k and m and n values. I am using cftool but I always get the error message that there are complex numbers calculated. How can I solve that issue?
Regards
1 Comment
Torsten
on 15 Apr 2024
If 0 < x < 1 for all your data points, you can not get complex numbers during the fitting process.
Answers (1)
Star Strider
on 15 Apr 2024
if ‘x’ is greater than 1 and ‘n’ is not an integer, the result will be complex.
Example —
k = rand
m = rand
n = rand
x = 2*rand
y = k*x^m*(1-x)^n
I am not certain what your data are, however it will likely be necessary to constrain the parameters. If ‘n’ (and possibly ‘m’) must be an integer in your model, and since your model is nonlinear, the only option I know of is to use the genetic algorithm in a mixed integer optimisation problem, such as described in Minimize a Nonlinear Function with Integer Constraints in the documentation. (I am familiar with ga, however I have not needed to do mixed integer problems very often, so I would have to refresh my memory about it. I can help if this is what you want to do.) There may be other options for this sort of problem in MATLAB, however I cannot find them in the documentation.
.
2 Comments
Star Strider
on 15 Apr 2024
It would help to have your data.
If ‘x’ is always less than 1 and no integer (or other) constraints are involved, perhaps something like this —
yfcn = @(b,x) b(1).*x.^b(2).*(1-x).^b(3); % Objective Function
x = sort(rand(10,1)); % Create Independent Variable Data
y = rand(10,1); % Create Dependent Variable Data
mdl = fitnlm(x, y, yfcn, rand(3,1)) % Nonlinear Regression
B = mdl.Coefficients.Estimate; % Get Coefficients
xv = linspace(min(x), max(x), numel(x)*10).'; % High-Resolution 'x' Vector For Plot
mdlfit = yfcn(B,xv); % Simulate Model
figure
plot(x, y, '.')
hold on
plot(xv, mdlfit, '-r')
hold off
grid
xlabel('X')
ylabel('Y')
title('Model Fit to Data')
If you need to constrain the parameters, use lsqcurvefit instead of fitnlm. If you need the respective confidence intervals, getting the confidence interfals for the parameters and the fit is then a bit more difficult, however not impossible. See the documentation on nlparci and nlpredci respectively for those details. (They should work with the lsqcurvefit results and outputs.)
.
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!