1
$\begingroup$

How can I export my fitted plot results as a txt file with two column XY without any brackets or graphics comments... I tried the following line, but I didn't get two column...

Export["firtcurve.dat", Flatten /@ fitplot2, "Table", "FieldSeparators" -> ""]

Here my data

data={{0.0058, 103686.}, {0.00635, 99133.2}, {0.00692, 96768.4}, {0.00747, 91096.8}, {0.00803, 91584.4}, {0.00858, 90998}, {0.00913, 93469.2}, {0.0097, 92494.8}, {0.01026, 92280.4}, {0.01081, 92167.6}, {0.01136, 98385.6}, {0.01192, 95647.6}, {0.01248, 99023.2}, {0.01303, 103776}, {0.01358, 100053.}, {0.01413, 99689.2}, {0.01469, 96788.4}, {0.01525, 90226.4}, {0.0158, 88250}, {0.01635, 75089.2}, {0.01691, 70733.2}, {0.01747, 62443.6}, {0.01802, 53762.8}, {0.01857, 46584}, {0.01913, 40684.4}, {0.01969, 34428.}, {0.02024, 32338.5}, {0.0208, 28251.}, {0.02135, 25399.}, {0.02191, 24325.4}, {0.02246, 22329.8}, {0.02302, 20324.6}, {0.02357, 19741.7}, {0.02413, 18336.1}, {0.02468, 18015.7}, {0.02523, 17504}, {0.02579, 17462.}, {0.02635, 16806.}, {0.0269, 16959.3}, {0.02745, 17069.6}, {0.02801, 15096.1}, {0.02856, 15410.6}, {0.02912, 14848.7}, {0.02967, 13276.7}, {0.03023, 12602}, {0.03078, 12084.3}, {0.03134, 11693.2}, {0.03189, 10419.9}, {0.03245, 10220.2}, {0.033, 9399.24}, {0.01437, 83937.6}, {0.01573, 75249.6}, {0.01712, 61748.4}, {0.0185, 50326.8}, {0.01989, 38519.4}, {0.02125, 30555.7}, {0.02262, 24798.4}, {0.02402, 20976.5}, {0.0254, 17888.6}, {0.02677, 16839.2}, {0.02814, 15443.6}, {0.02951, 13593.7}, {0.03089, 12432}, {0.03227, 10577.2}, {0.03363, 9237.3}, {0.035, 7825.44}, {0.03637, 6582}, {0.03775, 5730.63}, {0.03912, 4945.22}, {0.04049, 4257.93}, {0.04187, 3768.53}, {0.04325, 3553.86}, {0.04462, 2954.67}, {0.04599, 2681.2}, {0.04736, 2420.49}, {0.04875, 2063.76}, {0.05012, 1861.36}, {0.05149, 1647.01}, {0.05287, 1530.26}, {0.05424, 1451.87}, {0.05561, 1275.58}, {0.05699, 1202.98}, {0.05836, 1057.94}, {0.05975, 992.04}, {0.06112, 938.712}, {0.06249, 835.932}, {0.06386, 832.986}, {0.06524, 719.376}, {0.06662, 657.75}, {0.06799, 591.407}, {0.06936, 586.143}, {0.07074, 538.37}, {0.07211, 540.723}, {0.07348, 484.061}, {0.07486, 418.047}, {0.07624, 381.774}, {0.07762, 358.336}, {0.07898, 330.041}, {0.08035, 317.571}, {0.08165, 292.333}, {0.05481, 1326.86}, {0.06003, 1021.67}, {0.06533, 747.138}, {0.07058, 558.727}, {0.07592, 438.387}, {0.08116, 328.292}, {0.08632, 251.455}, {0.09165, 200.327}, {0.09693, 162.102}, {0.10209, 132.823}, {0.1073, 103.748}, {0.11267, 81.5521}, {0.11791, 70.8106}, {0.12313, 56.6837}, {0.12837, 49.1627}, {0.13363, 43.4262}, {0.13892, 38.9658}, {0.14415, 34.4442}, {0.1494, 30.7966}, {0.15474, 26.9218}, {0.16001, 25.2626}, {0.16522, 24.965}, {0.17051, 20.3799}, {0.17573, 19.5228}, {0.18094, 16.7343}, {0.18617, 17.8135}, {0.19148, 17.7965}, {0.19673, 17.0199}, {0.20191, 15.4306}, {0.20717, 15.4462}, {0.21245, 13.247}, {0.21772, 13.931}, {0.2229, 13.0009}, {0.22813, 12.8896}, {0.23333, 13.5703}, {0.23857, 11.7486}, {0.24386, 12.1296}, {0.24907, 11.688}, {0.25429, 12.5605}, {0.25949, 11.0021}, {0.26468, 10.4639}, {0.2699, 10.8949}, {0.27517, 10.4332}, {0.28034, 11.0561}, {0.28556, 10.8001}, {0.29082, 10.4613}, {0.29609, 9.0205}, {0.3011, 9.54147}, {0.3059, 11.3875}}

Here my code and fitting function :

mu0 = 4*Pi*10^(-7);
Ms = 0.0926 ;
bH = 2.91*10^8 ;


Heff[q_, l_, Hi_] := Hi*(1 + (l)^2*(q*10^(10))^2)
p[q_, l_, Hi_] := Ms/Heff[q, l, Hi]
Vp[r_] := 4/3*Pi*(r*10^(-9))^3
FF[q_, r_] := 
 9*SphericalBesselJ[1, q*10^(10)*r*10^(-9)]^2/(q*10^(10)*r*10^(-9))^2
RH[q_, l_, Hi_] := (1/4)*p[q, l, Hi]^2*(2 + 1/(1 + p[q, l, Hi])^(0.5))
RM[q_, l_, Hi_] := ((1 + p[q, l, Hi])^(0.5) - 1)/2
f[r_, pp_, R_] := (1/(r*10^(-9)*pp))*
  Exp[-(Log[r*10^(-9)] - Log[R*10^(-9)])^2/(2*pp^2)]

Sigma1[q_, l_, Hi_, pp_, R_, k_, bck_, aa_, bb_] := 
 NIntegrate[
   k*(aa*RM[q, l, Hi] + bb*RH[q, l, Hi]) *FF[q, r]*f[r, pp, R]*9*
    Vp[r]^2, {r, 0.1, 100}] + bck



 model2 = Sigma1[xx, l, 0.5, 0.15, 18.5, k, 10, 0, 1];
    result = Normal[
       NonlinearModelFit[Select[data, First[#] > 0.02 &], 
        model2, {{l, 1.64*10^(-9)}, {k, 5*10^(42)}}, xx]];
    singlefit = 
      NonlinearModelFit[Select[data, First[#] > 0.02 &], 
       model2, {{l, 1.64*10^(-9)}, {k, 5*10^(42)}}, xx];

    singlefit["BestFitParameters"];
    singlefit["EstimatedVariance"];
    singlefit["ParameterErrors"];
    singlefit["ParameterConfidenceIntervals"];
    MatrixForm[singlefit["CorrelationMatrix"]];
    Print[singlefit["ParameterTable"]];

    fitplot2 = Plot[result, {xx, 0.001, 0.3},
       PlotRange -> All,
       ScalingFunctions -> {"Log", "Log"},
       PlotStyle -> {Thickness[0.005], Red}
       ];

    Export["firtcurve.dat", Flatten /@ fitplot2, "Table", 
     "FieldSeparators" -> ""]
    FilePrint["firtcurve.dat"]
$\endgroup$
  • $\begingroup$ When I copy, past and run your code, the variables xx,l and k are undefined $\endgroup$ – FredrikD Nov 5 '18 at 14:42
  • 1
    $\begingroup$ Is it right, that you want to plot your fitting function, and then take those points that form this latter plot? Or you want something else? $\endgroup$ – Alexei Boulbitch Nov 5 '18 at 15:58
  • $\begingroup$ Yes I want to take those points and export in a txt file as two column $\endgroup$ – Bigprophete Nov 5 '18 at 21:36
1
$\begingroup$

In your code, "result" is returned from the function NonlinearModelFit. Instead of applying Normal, you could create a new variable "resultf"

...
resultf= ...NonLinearModelFit[__]..; 
result= Normal@resultf...
...

Now, with the function resultf, you use Table to create your list of values with the appropriate granularity.

...
exportData= Table[{i, resultf[i]}, {i, ...}]; 
Export["  ", exportData, ...]

To me it seems like you want to export the diagram and the values in one go which could explain your comment about graphics etc.

$\endgroup$
  • 1
    $\begingroup$ Yes, Thanks using your comments, I get my txt file with two column $\endgroup$ – Bigprophete Nov 6 '18 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.