# data fitting with custom equation

4 views (last 30 days)
Somnath Kale on 26 Jun 2021
Commented: William Rose on 28 Jun 2021
Hi
I am facing some issue in the data fitting with the equation. I tried but somehow I am not able manage it. I wanted to fit to my data
t = 1.00E-07 2.00E-07 5.00E-07 1.00E-06 2.00E-06 5.00E-06 1.00E-05 2.00E-05 5.00E-05 1.00E-04 2.00E-04 5.00E-04 1.00E-03 2.00E-03 5.00E-03
p(t) = 0.0824 0.1515 0.2339 0.3229 0.4505 0.6173 0.7434 0.822 0.9151 0.9795 0.982 0.9829 0.9861 0.9846 0.9856
with equation: (n =2) if possible let me know the code also.
somnath

William Rose on 27 Jun 2021
Here is code that fits w and t1 as you requested. For a starting guess [w,t1]=[1,1], the console output and plot are below.
>> SomnathMain2
Best fit: SumSqError=0.377, w=3.791, t1=0.00000217 That does not look like a great fit, so I changed line 4 of SomnathMain2.m to a different initial guess:
a0=[1;1e-6];
The fit was a lot better from this initial guess - see output below, and note the reduction in SumSqError.
>> SomnathMain2
Best fit: SumSqError=0.020, w=1.036, t1=0.00000330 To find a better fit, I suggest A. try different starting points, to make sure you have not gotten stuck in a local minimu that is not a global minimum, or B. modify the code to fit "A" and/or "n" in addition to "w" and "t1".
Option B. should be pretty obvious, based on the code I have provided. You need to make the necessary adjustments in both SomnathsFunc2.m and SomnathMain2.m.
Option A. is a bit trickier, to do in a programmatic way, but worth doing. I do option A. whenever I fit parameters of a non-trivial model. The basic idea for option A is:
Define a generous physically reasonable range for each parameter (err on the side of too big a range). Then make an array of inital guesses (i.e. each row is a possible value to a0, the initial guess). The values might be chosen to be at 20% and 80% of the range, and you try all the combinations. So if you are fitting two parameters, and the reasonable ranges are a1=[-10,+10] and a2=[0, 1] respectively, then you'll have four starting points, which are
[-9, 0.1; +9, 0.1; -9, 0.9; +9, 0.9];
You do the fit four times. Save the residual error and fitted parameters from each initial guess. After doing the four fits, pick the parameters that gave the best fit. Sometimes I try 10%, 50%, and 90% of the range as my initial guesses. In that case, I have 3^N initial guesses to try, where N=number of fitted parameters.
##### 2 CommentsShowHide 1 older comment
William Rose on 28 Jun 2021
log t0 is the variable of integration in the integral. It does not depend on the data values. You can plot F(log(t0)) versus log(t0), as follows:
Flogt0=@(x,A,w,t1) (A*w/pi)/((x-log(t1))^2+w^2);
A=.9971; w=.242; t1=0.00000549;
x=-10:.1:10;
for i=1:201, y(i)=Flogt0(x(i),A,w,t1); end
plot(x,y,'r.-'); xlabel('log(t_0)'); ylabel('F(log(t_0))'); It makes the plot above.

William Rose on 27 Jun 2021
What is the value of A?
Am I correct to understand that the variable of integration is log t0, and it varies from -Inf to +Inf? Make the substitution Then , and the integral can be written which looks challenging but should be doable with integral(). But my initial attempt yields an error.
Somnath Kale on 27 Jun 2021
A is normalization constant. Lets take A=1. And now we can fit the following equation for n, w and t1 