# Export fitted data of FindFit

I want to export the data of a FindFit in a .txt-file to do some postprocessing.

My fit is:

fit6 = FindFit[data, {Subscript[model, JCA], 1 < \[Alpha] < 4,1*10^-5 < Subscript[\[Lambda], th] < 1*10^-3,1*10^-5 < Subscript[\[Lambda], vis] <1*10^-3}, {{Subscript[\[Lambda], vis], 1*10^-6}, {Subscript[\[Lambda], th],1*10^-6}, \[Alpha]}, f, MaxIterations -> 100000]


I get something like this:

The blue curves are my experimental data and the red curve is the fitted function.

And now I want to export the calculated data (x,y-values) of the fitted red curve as a .txt-file. How do I get these values?

EDIT: I add my model and corresponding parameters and FindFit

some parameters:

T0 = 21;
P0 = 104500;
HR = 60;
kappla = 0.026;
T = T0 + 273.16;
Pr = 0.71;
R = 287.031;
Rvp = 461.521;
Pvp = 0.0658*T^3 - 53.7558*T^2 + 14703.8127*T - 1345485.0465;
\[Eta] = -5.95238*10^-11*T^2 + 2.71368*10^-14*T^3 +
7.72488*10^-8*T;  (*Viskosität von Luft in [Pa/s]*)
Cp = 4168.8*(-6.46128*10^-11*T^3 + 1.69194*10^-7*T^2 -
7.55179*10^-5*T + 0.249679);
Cv = Cp - R;
\[Gamma] = Cp/Cv;
\[Rho]0 = P0/(R*T) - (1/((R - 1)*Rvp))*(HR/100)*(Pvp/T);
c0 = (\[Gamma]*P0/\[Rho]0)^0.5 ;
kair = \[Omega]/c0;
Z0 = \[Rho]0*c0;
\[Omega] := 2*Pi*f;
d = 24*10^-3;
\[Sigma] = 70000;
\[Phi] = 0.989;


model:

Reff = \[Rho]0 \[Alpha] (1 + ((\[Phi] \[Sigma]) Sqrt[
1 + (I (4 \[Omega] \[Rho]0 \[Eta] \[Alpha]^2))/(\[Sigma]^2 \
\[Phi]^2
\!$$\*SubsuperscriptBox[\(\[Lambda]$$, $$vis$$, $$2$$]\))])/(
I (\[Omega] \[Rho]0 \[Alpha])));
Keff = (P0 \[Gamma])/(\[Gamma] - (\[Gamma] - 1)/(
1 + (8 \[Eta] Sqrt[1 + (I (\[Omega] Pr \[Rho]0
\!$$\*SubsuperscriptBox[\(\[Lambda]$$, $$th$$, $$2$$]\)))/(
16 \[Eta])])/(I (\[Omega] Pr
\!$$\*SubsuperscriptBox[\(\[Lambda]$$, $$th$$, $$2$$]\) \[Rho]0))));
kw = \[Omega] Sqrt[Reff/Keff];
Zc = Sqrt[Reff Keff];
Zs = -((I Zc Cot[kw d])/Z0);
r = (Zs - 1)/(Zs + 1);
model = abs1 = 1 - Abs[r]^2;


Fit:

fit1 = FindFit[
data, {model, 1 < \[Alpha] < 4,
1*10^-5 < Subscript[\[Lambda], th] < 1*10^-3,
1*10^-5 < Subscript[\[Lambda], vis] <
1*10^-3}, {{Subscript[\[Lambda], vis],
1*10^-6}, {Subscript[\[Lambda], th], 1*10^-6}, \[Alpha]}, f,
MaxIterations -> 100000]
Show[Plot[
Labeled[Subscript[model, Daimler] /. fit1, "Daimler"], {f, 200,
8000}, PlotRange -> {{200, 7500}, {0, 1}}, PlotStyle -> Red],
ListPlot[Labeled[data, "import"],
PlotRange -> {{200, 7500}, {0, 1}}]]


Extraction of my data (x,y):

{00143.7500 0000.0793 00487.5000 0000.2002 00831.2500 0000.5153 01175.0000 0000.8803 01518.7500 0000.9933 01862.5000 0000.9361 02206.2500 0000.8575 02550.0000 0000.8051 02893.7500 0000.7677 03237.5000 0000.7691 03581.2500 0000.7717 03925.0000 0000.7945 04268.7500 0000.8263 04612.5000 0000.8464 04956.2500 0000.8688 05300.0000 0000.8810 05643.7500 0000.8906 05987.5000 0000.9019 06331.2500 0000.9152}

• You might want to use NonlinearModelFit[] instead, so that getting the required data is convenient: nlm = NonlinearModelFit[data, model, parameters, x]; Export["filename.dat", Transpose[{data[[All, 1]], data[[All, 2]], nlm["PredictedResponse"], nlm["FitResiduals"]}], "Table"]. – J. M. will be back soon Mar 20 '18 at 13:42
• I tried your suggestion with nlm, but unfortunately mma was not possible to solve the model as with FindFit, even though according to the documentation it should solve in the same way. Is it possible to "catch" the calculated values of Findfit in a table and export them? – JinFins Mar 21 '18 at 8:52
• NonlinearModelFit[] uses FindFit[] internally, so it doesn't make sense that one works and the other does not. Did you remember to include constraints and starting values? Also, since you included neither your raw data nor your model in your question, I have no way of knowing whether you used it right. – J. M. will be back soon Mar 21 '18 at 8:59
• Of course it doesn't make sense. Maybe you have more insight to my problem right now by adding model, parameter and data. I tried the same constraints and starting values in nlm. – JinFins Mar 21 '18 at 9:24
• My data are about absorption (-) over frequency (1/s). That means, first value 00143.7500 = frequency = 143.75 1/s and second value is corresponding absorption 0000.0793 = absorption = 0.0793; next freqeuncy value is 487.5 and corresponding absorption is 0.2002. The format with all the 0 is due to Excel export. – JinFins Mar 21 '18 at 15:14

Below are the data in a Mathematica friendly format

data = {{143.75, 0.0793}, {487.5, 0.2002}, {831.25, 0.5153}, {1175., 0.8803}, {1518.75, 0.9933}, {1862.5, 0.9361}, {2206.25, 0.8575}, {2550., 0.8051}, {2893.75, 0.7677}, {3237.5, 0.7691}, {3581.25, 0.7717}, {3925., 0.7945}, {4268.75, 0.8263}, {4612.5, 0.8464}, {4956.25, 0.8688}, {5300., 0.881}, {5643.75, 0.8906}, {5987.5, 0.9019}, {6331.25, 0.9152}}


The following plot is provided to establish that there were no errors introduced during data import:

After evaluating FindFit (with a few adjustments due to the way I recoded the various consts etc not really important) the output I obtained was the following:

The data and the fitted data are displayed below:

Now, since model/.fit1 evaluates to an expression containing f, one way to retrieve the fitted values is to evaluate the following:

Block[{y},
(*get the x-coordinates (freqs)*)
With[{x = Part[data, All, 1]},
(*replace all occurrences of f in the*fitted*model with the x's*)
y = model /. fit1 /. Transpose[{Thread[f -> x]}];
(*retrieve output*)
Transpose[{x, y}]
]
]


The following plot is produced using the Show from the question by replacing the first Plot with ListPlot:

• First, thanks for make some kind of overview of my problem! I just have a few questions: 1. Did you use other constraints and/or starting values than mine? 2. I agree with the nice fit between the experimental data and the model and the resulting values, but where exactly do I have to implement the last code with "Block[..]" in my code? 3. Is it then possible to export the fitted values? – JinFins Mar 21 '18 at 16:43
• 0. don't mention it! 1. no, I just made ω explicitly dependent on f (and all associated symbols, too); similarly for Reff (dependent on f and α); I wanted to have expressions with explicit dependencies because that is easier to debug 2. you can evaluate it after FindFit evaluates; assuming all goes well it should produce a list of frequency-absorption pairs 3. Evaluate Export["fitted.txt",%,"Table"] after the Block. – user42582 Mar 21 '18 at 16:56
• Thanks again! But could it be that there is missing a bracket after "Block{..}"?-> Block[{...? and unfortunately there is the following error message: "With: With called with 1 argument; 2 or more arguments are expected". Is there missing something after "[..]Thread[f->x]}];" and the next "];" and a second message: "Transpose: The first two levels of {412,y} cannot be transposed" – JinFins Mar 21 '18 at 17:48
• please check the updated Block in the answer; I think it should do the trick, this time... (it was missing the left bracket in Block and the comma after the right curly brace in With; also, the last Transpose contained x which was out of scope (With)) – user42582 Mar 21 '18 at 17:52
• Great! Thats exactly the tool I need! Thank you very much for your effort! – JinFins Mar 22 '18 at 10:59

Can you try this.

fitData = Transpose@{data[[All, 1]], fit6@data[[All, 1]]};

Export["newDataName.txt", fitData]

• Unfortunately that does not work. Is the expression "data" for the genereated data from FindFit or shoud this be a link to my exported data which I also called "data". I ask just to ensure that there is no permutation from mma of my defined imported "data" and some data which are calculated via FindFit. – JinFins Mar 21 '18 at 8:57
• data is imported data. Replace txt by dat – OkkesDulgerci Mar 21 '18 at 13:09

This is just an extension of @J.M. 's comment...

What @J.M. showed is how to get predictions (and standard errors) at each data point. If you don't have too many data points or if those data points are not relatively uniformly dispersed across the range of the predictor values, then you can make predictions on a "grid" of uniformly spaced values (using the first example in NonlinearModelFit):

(* Run regression *)
data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {6, 4}, {7, 5}};
nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x];

(* Make predictions *)
{xmin, xmax} = MinMax[data];
n = 100; (* Number of uniformly spaced predictions *)
xx = xmin + (xmax - xmin) Range[0., n]/n;
meanPrediction = nlm[x] /. x -> xx;
mpb = nlm["MeanPredictionBands"] /. x -> xx;
spb = nlm["SinglePredictionBands"] /. x -> xx;

(* Combine into a single expression *)
results = Transpose[{xx, meanPrediction, mpb[[1]], mpb[[2]], spb[[1]],  spb[[2]]}];

(* Export *)
Export["predictions.dat", results, "Table"]


This gets you the predictions and the mean and single prediction bands:

Show[ListPlot[results[[All, {1, #}]] & /@ {2, 3, 4, 5, 6},
PlotStyle -> {Green, Blue, Blue, Red, Red}, Joined -> True],
ListPlot[data]]


• Thanks for your explanation. This might be an interesting thing, as far as NLM gives the same result as FindFit, which it should do of course, but in my case it does not. – JinFins Mar 21 '18 at 9:00