Fitting data to a model to estimate parameters using lsqcurvefit: Unrecognized function or variable error
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
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
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".
Respuestas (1)
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)
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)
disp('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)])
0 comentarios
Ver también
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!