ERROR: Fitting a custom function to my data

14 visualizzazioni (ultimi 30 giorni)
Michael
Michael il 24 Ago 2022
Risposto: Torsten il 24 Ago 2022
I implemented a function into my script, that models the value of T:
function T_model_MILS = exactmils(lambda,cv,vD,t,rb,q)
cw = 4.15*10^6;
alpha = lambda/cv;
r = rb; % case r = rb
R = r/rb;
vT = vD*cw/cv;
Pe = rb*vT/alpha;
b = (R*Pe/4)^2;
Fo = alpha*t/rb^2;
tau = 4*Fo/R^2;
u = 1./tau;
n = length(u);
aa = besseli(0,2*sqrt(b));
G = zeros(n,1);
for i = 1:n
A = quadgk(@funa,u(i),inf,'AbsTol',1.0e-12);
G(i) = 0.5*A*aa;
end
function y = funa(x)
y = exp(-x - b./x)./x;
end
T_model_MILS = q.*G./(2*pi*lambda);
T_model_MILS(isnan(T_model_MILS))=0; % NaN to zero for fitting alg.
end
The function works fine with preset parameters (green: model, red: my measured dataset). But surely, I want the parameters to be estimated by matlab ...
Now, when I want to fit my function parameters x1,x2 and x3 to my data:
% Initial model parameters
lambda2 = 2;
cv2 = 2*10^6;
vD2 = 1*10^(-7);
x = [lambda2 cv2 vD2];
tic;
%trying out function with set parameters
T2 = exactmils(x(1),x(2),x(3),tsekunden,rb,q);
% fitting to dTLOG-data
F2 = @(x,tsekunden) exactmils(x(1),x(2),x(3),tsekunden,rb,q);
[x,resnorm,~,exitflag,output] = lsqcurvefit(F2,x,tsekunden,dTLOG);
T_modelfit_MILSexact = exactmils(x(1),x(2),x(3),tsekunden,rb,q);
toc;
I get an error ...
__________________________
Solver stopped prematurely.
lsqcurvefit stopped because it exceeded the function evaluation limit,
options.MaxFunctionEvaluations = 3.000000e+02.
Elapsed time is 364.096528 seconds.
___________________________
How can I improve my fitting algorithm? It works fine with easier functions, but the function exactmils() is computationally more intense (elapsed time with set parameters = 0.966603 seconds).
Thank you very much for your help! :)

Risposte (1)

Torsten
Torsten il 24 Ago 2022
Since we don't have data to test your code, we are not able to comment on your question.
Most probably, the line
T_model_MILS(isnan(T_model_MILS))=0; % NaN to zero for fitting alg.
will cause severe problems for an optimizer if there really are NaN values in T_model_MILS.

Categorie

Scopri di più su Get Started with Curve Fitting Toolbox in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by