Fitting data to a model to estimate parameters using lsqcurvefit: Unrecognized function or variable error
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Amy
2025년 4월 12일
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
2025년 4월 12일
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".
답변 (1개)
Sulaymon Eshkabilov
2025년 4월 13일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!