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 件のコメント

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.)
Thank you for the answer. Do i need to give the variables the initial condition values before i define k?
kgln=0.01;
kglc=0.01;
Sorry i am new to Matlab
Torsten
Torsten 2023 年 3 月 7 日
編集済み: Torsten 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.
Thank you
Amy
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);
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)
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

カテゴリ

ヘルプ センター および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 3 月 7 日

回答済み:

2025 年 4 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by