Fitting data to a model to estimate parameters using lsqcurvefit: Unrecognized function or variable error

4 visualizaciones (últimos 30 días)
I am trying to fit experimental data (time vs concentration) to a kinetic model using lsqcurvefit function.
t=[0 10 20 30 50 70 90 110];
GLCexp= [5698 5400 4612 3811 3514 3400 2825 2406];
% hold on
plot(t,GLCexp,'ro')
title('Data points')
% hold off
%Define the parameters in terms of one variable k:
k(1)=kgln;
k(2)=kglc;
%Define the curve as a function of the parameters k and the data t:
%GLN0=15016;
%GLCmodel=(GLN0*kgln*exp(-kgln*t))/(kglc-kgln) - (GLN0*kgln*exp(-kglc*t))/(kglc-kgln);
GLCmodel=@(k,t)(15016*k(1)*exp(-k(1)*t))/(k(2)-k(1)) - (15016*k(1)*exp(-k(2)*t))/(k(2)-k(1));
%Set initial point k0
k0 = [0.01 0.01];
%Run the solver and plot the resulting fit
[k,resnorm,residual,exitflag,output] = lsqcurvefit(GLCmodel,k0,t,GLCexp)
hold on
plot(t,GLCmodel(k,t))
hold off
When I click run it gives this error:
Unrecognized function or variable 'kgln'.
Error in datdemoGLC (line 9)
k(1)=kgln;
  6 comentarios
Amy
Amy el 12 de Abr. de 2025
How I got the error for Unrecognized function or variable 'M_observed'.
Error in semilinearleastsquaremodel (line 17)
optParams = lsqcurvefit(modelFunc, initialParams, M_data, M_observed);
Torsten
Torsten el 12 de Abr. de 2025
From the error message it seems that an array with name "M_observed" is not defined in the part of your code where you call "lsqcurvefit".

Iniciar sesión para comentar.

Respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 13 de Abr. de 2025
It looks like that you overlooked or mistyped one '-' in the model formulation of GLCmodel. Here is the complete solution:
t=[0 10 20 30 50 70 90 110];
GLCexp= [5698 5400 4612 3811 3514 3400 2825 2406];
GLCmodel=@(k,t)(15016*k(1)*exp(-k(1)*t))/(k(2)-k(1)) + (15016*k(1)*exp(-k(2)*t))/(k(2)-k(1));
%Set initial values for k0:
k0 = [.02 -.02];
% Run the solver and plot the resulting fit
[k,resnorm,residual,exitflag,output] = lsqcurvefit(GLCmodel,k0,t,GLCexp)
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
k = 1×2
0.0029 0.0184
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
resnorm = 3.6663e+05
residual = 1×8
-0.7059 -263.1343 47.0779 439.3891 83.0787 -292.5278 -91.6579 35.1723
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
exitflag = 3
output = struct with fields:
firstorderopt: 98.4302 iterations: 20 funcCount: 63 cgiterations: 0 algorithm: 'trust-region-reflective' stepsize: 1.3700e-07 message: 'Local minimum possible....' bestfeasible: [] constrviolation: []
figure
plot(t,GLCexp,'rd', 'MarkerFaceColor', 'y', 'MarkerSize', 9)
title('Data points')
hold on
plot(t,GLCmodel(k,t), 'b-', 'LineWidth',2)
hold off
grid on
legend('Data', 'Fit Model')
xlabel('Time')
ylabel('GLCexp')
fprintf('Found Fit Model Coefficients are: k = [%1.5f %1.5f] \n', k)
Found Fit Model Coefficients are: k = [0.00293 0.01838]
disp('Found Fit model is: ')
Found Fit model is:
fprintf('%1.5f*exp(-%1.5f*t)/%1.5f + %1.5f*exp(-%1.5f*t)/%1.5f \n',...
[15016*k(1), k(1), k(2)-k(1), 15016*k(1), k(1), k(2)-k(1)])
43.99815*exp(-0.00293*t)/0.01545 + 43.99815*exp(-0.00293*t)/0.01545

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by