0
$\begingroup$

I am trying to fit the results of an experiment with microwaves with a sinus-curve but the result is very bad.

Needs["ErrorBarPlots`"];

plot1 = ErrorListPlot[{{{22, 0.13}, ErrorBar[0.1, 0.01]}, {{21.8, 0.11}, 
 ErrorBar[0.1, 0.01]}, {{21.6, 0.05}, ErrorBar[0.1, 0.01]}, {{21.4, 0.02}, 
 ErrorBar[0.1, 0.01]}, {{21.2, 0.03}, ErrorBar[0.1, 0.01]}, {{21, 0.04}, 
 ErrorBar[0.1, 0.01]}, {{20.8, 0.08}, ErrorBar[0.1, 0.01]}, {{20.6, 0.15}, 
 ErrorBar[0.1, 0.01]}, {{20.4, 0.13}, ErrorBar[0.1, 0.01]}, {{20.2, 0.07}, 
 ErrorBar[0.1, 0.01]}, {{20, 0.03}, ErrorBar[0.1, 0.01]}, {{19.8, 0.02}, 
 ErrorBar[0.1, 0.01]}, {{19.6, 0.04}, ErrorBar[0.1, 0.01]}, {{19.4, 0.09}, 
 ErrorBar[0.1, 0.01]}, {{19.2, 0.15}, ErrorBar[0.1, 0.01]}, {{19, 0.16}, 
 ErrorBar[0.1, 0.01]}, {{18.8, 0.09}, ErrorBar[0.1, 0.01]}, {{18.6, 0.03}, 
 ErrorBar[0.1, 0.01]}, {{18.4, 0.02}, ErrorBar[0.1, 0.01]}, {{18.2, 0.03}, 
 ErrorBar[0.1, 0.01]}, {{18, 0.07}, ErrorBar[0.1, 0.01]}, {{17.8, 0.12}, 
 ErrorBar[0.1, 0.01]}, {{17.6, 0.15}, ErrorBar[0.1, 0.01]}}, 
 FrameLabel -> {"Position x (cm)", "Intensity I (W/m^2)"}, 
 PlotStyle -> Directive[Red]];

data = {{22, 0.13}, {21.8, 0.11}, {21.6, 0.05}, {21.4, 0.02}, 
       {21.2, 0.03}, {21, 0.04}, {20.8, 0.08}, {20.6, 0.15}, 
       {20.4, 0.13}, {20.2, 0.07}, {20, 0.03}, {19.8, 0.02},
       {19.6, 0.04}, {19.4, 0.09}, {19.2, 0.15}, {19, 0.16}, 
       {18.8, 0.09}, {18.6, 0.03}, {18.4, 0.02}, {18.2, 0.03}, 
       {18, 0.07}, {17.8, 0.12}, {17.6, 0.15}};

m = NonlinearModelFit[data, a*Cos[(b*x) + c] + d, {a, b, c, d}, x, 
   MaxIterations -> 2000];

m["ParameterTable"]

plot2 = Plot[m[x], {x, 17, 23}, PlotStyle -> Directive[Black], 
   GridLines -> Automatic];

Show[plot1, plot2, Frame -> True, PlotRange -> All]

Any ideas to improve the output?

$\endgroup$
1
  • $\begingroup$ You don't necessarily need a new function. You just need better starting values: m = NonlinearModelFit[data, a*Cos[(b*x) + c] + d, {{a, 0.06}, {b, 5}, c, {d, 0.09}}, x]. $\endgroup$
    – JimB
    Commented Mar 26, 2018 at 16:58

2 Answers 2

0
$\begingroup$

I just tried the model given in the answer here: Fitting data points using trigonometric functions

which for your code would be

model = e + a*Sin[b*x + f] + c*Sin[d*x + g];
fit = NonlinearModelFit[data, model, {a, {b, 2}, c, {d, 4}, e, f, g}, x];

For me it looks quite ok that way. Maybe with some additional effort it can be further improved.

$\endgroup$
1
  • $\begingroup$ It works! Thank you very much! $\endgroup$ Commented Mar 26, 2018 at 16:54
0
$\begingroup$
         model2 = y0 + A*Cos[s*t + c]; fit2 = 
          NonlinearModelFit[data, model2, {y0, A, s, c}, t, 
           Method -> {NMinimize, 
            Method -> {"DifferentialEvolution", "ScalingFactor" -> 0.27, 
        "CrossProbability" -> 0.10, 
         "PostProcess" -> {FindMinimum, Method -> "QuasiNewton"}}}];
        plot2 = Plot[fit2[t], {t, 17, 22}, PlotRange -> All, Frame -> True, 
         ImageSize -> 560, PlotStyle -> Red];
         Show[plot1, plot2]
        fit2["ParameterTable"];
        prt2 = fit2   ["BestFitParameters"]

fitted plot

$\endgroup$

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