Fitting a custom function to my data

2 ビュー (過去 30 日間)
Michael
Michael 2022 年 8 月 23 日
編集済み: Michael 2022 年 8 月 24 日
For each value of t, I have a measured datapoint T_measured. Now, I have a custom function ils(x1,x2,a,b,t) with two variables (x1,x2), two constant coefficients (a,b) and time array (t), that models T_model
function T_model = ils(x1,x2,a,b,t)
alpha = x1/x2; Fo = alpha*t/a^2;
x1 = 1./(4*Fo); g1 = 0.25*expint(x1)/pi;
T_model = b*g1/(x1);
end
One solution, that I´ve tried (lsqcurvefit) did not work (I get an error:
"Error using lsqncommon ... Objective function is returning undefined values at initial point. lsqcurvefit cannot continue. Error in lsqcurvefit (line 295) lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,optimgetFlag,caller,..."
Here is my attempt:
% Coefficients
a = 75/1000;
b = 98.2;
% Initital guess
x1 = 2.0;
x2 = 0.5*10^6;
x0 = [a b];
F = @(x,t) ils(x0(1),x0(2),a,b,t);
[x0,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,t,T_measured);
T_model = ils(x0(1),x0(2),rb,t,q);
The goal would be to get the best fitting values for the parameters x1 and x2. The result should then look something like this:
Fig. 1: red: my measured data T_measured(t), green: the fitted function T_model(t)
Thank you very much for your help! :)

採用された回答

Image Analyst
Image Analyst 2022 年 8 月 23 日
T_model is not being returned from your function. You need to assign T_model to something before you exit the ils() function.
And check if it's nan before you return it and set it to zero or something if that is the case.
  2 件のコメント
Michael
Michael 2022 年 8 月 24 日
Sorry, I changed some variable names for this question to make things easier to read and forgot to change one variabl name. I edited it. The problem remains.
Michael
Michael 2022 年 8 月 24 日
編集済み: Michael 2022 年 8 月 24 日
In fact, I have one value within the array that is NaN.
I added this line of code at the end of my function, and now everything works fine! Thanks a lot! :)
T_model(isnan(T_model))=0;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by