Fitting uneven data to a function

Hi!
So, I recently asked a question as to how to solve a summation (link) and I think I have the function working but I am having issues fitting it to my unevenly spaced data. I have attached the data file (column 4 is x data and 3 is y data). I am scaling the data as you can see in the figure (by taking an inverse square root of the x data) attached at the bottom. Is this a data problem? Or can I make a good fit by adding something like weights? Or do I need to use a different function altogether?
Function itself is as such:
MATLAB function I wrote to give as an input to the fitting function.
function I_num = CurveFitD(x, t)
%CurveFitD We want to fit the I(t)=f(t) to a custom function.
% For a bounded layer we have an expression which can yield us the
% thickness of the polymer layer (in centimeters). We have to fit the transient to this
% equation to calculate the layer thickness.
% Constants
I_num = zeros(size(t));
% Bounded Cottrell
for i = 1:length(t)
for k = 1:42
I_num(i) = I_num(i) + (-1).^k.*exp(-((k*x(1))^2)./(x(2).*t(i)));
end
I_num(i) = sqrt(x(2)./(pi.*t(i))).*(x(3)./x(1)).*(1+2.*I_num(i));
end
end
I am calling this function using lsqcurvefit as given. As you can see I have an idea of the parameters I want to extract.
%% Nonlinear Curve-Fitting (Data-Fitting 'lsq')
%%
t = E3PS_Whole.CorrectedTimes;
I_exp = E3PS_Whole.WE1CurrentA;
invsqrt = 1./sqrt(t);
options = optimoptions('lsqcurvefit','Algorithm','trust-region-reflective',...
'OptimalityTolerance',1e-12,...
'StepTolerance', 1e-12,...
'FunctionTolerance',1e-12,...
'FiniteDifferenceType',"central");
% x(1)=d; x(2)=D_e; x(3)=deltaQ
lb = [1e-07; 1e-10; 1e-09];
ub = [1e-03; 1e-06; 1e-05];
x_0 = [4e-05; 1e-07; 1e-06];
[x,resnorm,residual,exitflag,output]=lsqcurvefit(@CurveFitD, x_0, t, I_exp, lb, ub, options);
I_num = CurveFitD(x, t);
% I vs E for Experimental and Calculated
figure(4);
plot(invsqrt, I_exp, 'ro', 'LineWidth', 1);
hold on;
plot(invsqrt, I_num, 'b--', 'LineWidth',2);
xlabel('1/{\surd{t}} / s^{-1/2}'); ylabel('I / A');
legend('Experimental', 'Numerical');
Output looks like this.

4 Comments

It is best to demonstrate your code by running in the forum, including the data reading step. When I plot your data, assuming the steps below, I get something that doesn't look like your posted plot.
T=readtable('Data.txt');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
t=T.CorrectedTime_s_;
I_exp= T.WE_1__Current_A_;
invsqrt = 1./sqrt(t);
plot(invsqrt,I_exp,'ro--')
Hashim
Hashim on 1 Sep 2022
Edited: Hashim on 1 Sep 2022
Yep there is a point off in the data around the t=0.05 mark which gives a value of I=8.63647E-7. I don't know the cause of it. I don't yet have the grasp of how to run code here but will try to learn about it.
I don't yet have the grasp of how to run code here but will try to learn about it.
Use the Run button,
Define x(1) = sqrt(D)/d and x(2) = deltaq as your model parameters and write your model in these two parameters.
Using three parameters as you do makes your model overfitted.

Sign in to comment.

Answers (1)

Matt J
Matt J on 1 Sep 2022
Edited: Matt J on 1 Sep 2022
Or do I need to use a different function altogether?
The first thing I notice is that your model is over-parametrized. You are writing it in terms of 3 parameters when in fact there are only 2 degrees of freedom, since the model depends on d and D only through the ratio d^2/D.
Also, one of the parameters is merely a linear scaling constant, and can be eliminated from the iterative fitting process as well if you do the curve fit using fminspleas,
Solving for 1 unknown should be pretty robust, without the need for data weighting, but fminspleas does let you provide weights if desired.

4 Comments

Hi, if I am undesstanding this correctly I only need input the exponential part of my equation? i.e.
As per the documentation here... It's a bit confusing beacuse I don't see the function explicitly defined and attributed to the funlist. How exactly is y being passed to fminpleas?
% Example 1:
% Fit a simple exponential model plus a constant term to data.
% Note that fminspleas deals with the linear parameters, the
% user needs only worry about the nonlinear parameters.
%
% x = rand(100,1);
% y = 4 - 3*exp(2*x) + randn(size(x));
%
% funlist = {1, @(coef,xdata) exp(xdata*coef)};
% % If you have an older release of matlab, use this form:
% % funlist = {1, inline('exp(xdata*coef)','coef','xdata')};
%
% NLPstart = 1;
% options = optimset('disp','iter');
% [INLP,ILP] = fminspleas(funlist,NLPstart,x,y,[],[],[],options)
I would revise your model to,
with unknowns A nd B. The implementation with fminspleas would be,
d0=4e-05;
D0=1e-07;
B0=d0^2/D0;
funlist={@mdl};
[B,A]=fminspleas(funlist,B0,t,I_exp);
function I=mdl(B,t)
t=t(:);
k = 1:42;
out=1+2*sum( (-1).^k.*exp(-k.^2.*B./t) ,2);
end
out=1+2*sum( (-1).^k.*exp(-k.^2.*B./t) ,2);
What does the 2 at the end mean?
Torsten
Torsten on 2 Sep 2022
Edited: Torsten on 2 Sep 2022
S = sum(A,dim) returns the sum along dimension dim. For example, if A is a matrix, then sum(A,2) is a column vector containing the sum of each row.
So it's the sum over k, not over t.

Sign in to comment.

Categories

Products

Asked:

on 1 Sep 2022

Edited:

on 2 Sep 2022

Community Treasure Hunt

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

Start Hunting!