# Curve fit autocatalytic model

5 views (last 30 days)
Maximilian on 15 Apr 2024
Commented: Torsten on 15 Apr 2024
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
Torsten on 15 Apr 2024
If 0 < x < 1 for all your data points, you can not get complex numbers during the fitting process.

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
k = 0.4334
m = rand
m = 0.9353
n = rand
n = 0.1388
x = 2*rand
x = 1.4803
y = k*x^m*(1-x)^n
y = 0.5122 + 0.2386i
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.
.
Maximilian on 15 Apr 2024
Hi Star Strider,
To elaborate on this:
The data illustrates the cure kinetics of a polymer ( y = degree of cure x = time)
So x is not larger than 1 but n is not an integer. The sum of m and n is supposed to be 2.
I aim to create curve fits to different curing temepratures. The idea is to use m and n to create the best curve fit possible. If the cftool doesn't work, I am happy to use any alternative that would work.
I hope that helps
Regards
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
mdl =
Nonlinear regression model: y ~ b1*x^b2*(1 - x)^b3 Estimated Coefficients: Estimate SE tStat pValue ________ _______ _______ ________ b1 0.63668 0.32806 1.9408 0.093428 b2 0.10331 0.21397 0.4828 0.64397 b3 0.042752 0.29176 0.14654 0.88763 Number of observations: 10, Error degrees of freedom: 7 Root Mean Squared Error: 0.29 R-Squared: 0.0573, Adjusted R-Squared -0.212 F-statistic vs. zero model: 11.8, p-value = 0.00399
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.)
.

### 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!