Fitting data to a model to estimate parameters using lsqcurvefit: Unrecognized function or variable error
古いコメントを表示
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 件のコメント
Star Strider
2023 年 3 月 7 日
Of course. Neither of those variables have been defined in the code prior to those assignments.
In any event, see: Parameter Estimation for a System of Differential Equations for one example of how to do this correctly. (I have posted several such examples, this one is likely easiest to understand, and based on the original Monod kinetics and curve fitting.)
Özge Biçer
2023 年 3 月 7 日
You don't need these lines in your code since k is nowhere referenced:
%Define the parameters in terms of one variable k:
k(1)=kgln;
k(2)=kglc;
So you also don't need to set
kgln=0.01;
kglc=0.01;
But in the setting
k0 = [0.01 0.01];
k0(1) and k0(2) must be different because you divide by k(2)-k(1).
And since your model gives a value of 0 at t=0, it is not adequate to reflect your data: they have a value 5698 at t=0.
Özge Biçer
2023 年 3 月 8 日
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 件)
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)])
カテゴリ
ヘルプ センター および 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!
