Dear community,
I ran into some problems with NonlinearModelFit when trying to fit experimental data. The fit does not reach a satisfying closeness and I get a few wierd error messages. After days of online search I decided to ask here.
I'll take you through my code:
At first, the data series for time and heatflow are imported (the file is attached to this post), the initial heat flow and time, and the equation to which I want to fit are defined
rawdata = SetPrecision[Import["Raw_heatflow_data.txt"], 20];
time = rawdata[[1]];
heatflow = rawdata[[2]];
ListPlot[Thread[{rawdata[[1]], rawdata[[2]]}]]
t0 = time[[1]];
q0 = heatflow[[1]];
data = Thread[{time, heatflow}];
model = (q0 - (a + b*(t - t0)))*Exp[-(t - t0)/tau] + (a + b*(t - t0));
The precision of all numbers can be verified here as 20. This will be important to an error message below.
Precision[model]
Precision[time]
Precision[heatflow]
When doing the fit, I applied any options that I found online which might potentially resolve the issues:
fit = NonlinearModelFit[
data, {model, {10^-6`20 < a < 5*10^-3`20, -1.1*10^-9`20 < b < -1*10^-19`20,
10`20 < tau < 1200`20}}, {{a, 1*10^-6`20}, {b, -1*10^-9`20}, {tau, 650`20}}, t,
WorkingPrecision -> 20, MaxIterations -> 100, AccuracyGoal -> 15, PrecisionGoal -> 15,
ConfidenceLevel -> 95/100, Tolerance -> 10^-50, StepMonitor :> Print["Step to a=", a, ", b=", b, " and T=", tau]];
The resulting fit is somewhat close to the data, but when a colleague did the same fitting in Matlab, he got a much closer, almost perfect fit. Are there any options in Mathematica to improve the accuracy of the fit? I'm thinking of options like a smaller iteration step size or limiting the fit residuals to a lower value.
Also, do you have recommendations what I should do about AccuracyGoal, PrecisionGoal, ConfidenceLevel and Tolerance, as I don't completely understand what they are doing in the fitting process?
Besides these general questions, there have been some more issues with my code. As visible through the step monitor, b reaches a value of -4.96648*10^-9 at the last step which is below the lower constraint. Hence, it is not followed. Is there a way to strictly enforce the constraints at every step?
Also, two error messages pop up:
NonlinearModelFit::precw: The precision of the data and model function (MachinePrecision) is less than the specified WorkingPrecision (20).
Why is the precision of my data and model reset to MachinePrecision, and not 20 anymore, as above? How can I prevent this issue? I even wrote the `20 for all the starting values and constraints, and it still does not resolve the error...
NonlinearModelFit::sdir: Search direction has become too small.
What does this error mean and how does it affect my calculation?
Best regards
Roland
Attachments: