2
$\begingroup$

It is a simple fit I would like mathematica to do: Fitting a set of experimentally obtained data points with an exponentially decreasing cosine function.

 datafit = Table[data3[[i, 2]], {i, 1, Length[data3]}];
 tab = Table[Sin[i], {i, 0.01, 100, 0.01}];
 model = Cos[a*x] Exp[b*x];
 fit = FindFit[datafit, model, {{a, 100}, {b, -10}}, x]

 Show[ListLinePlot[data3, PlotStyle -> Red], 
 Plot[{Evaluate[model /. fit]}, {x, 0, 0.6}]

This should give me the fit, but instead completely wrong values are found for the parameters, even when I adjusted the starting values.

The file can be found here (Dropbox).

OutPut:

Failed-Fit. Blue: "Fit", Red:data

With the blue "Fit" and the data in red.

I hope somebody finds my mistake or can explain why Mathematica is doing this!

EDIT: For a=145 and b=-2.8 I get the following result:

enter image description here

Which shows, that at least the initial values I have chosen are somewhat resonable.

$\endgroup$
7
  • 2
    $\begingroup$ Your dropbox file doesn't load for me. However, odds are you just have to specify initial values. $\endgroup$
    – Feyre
    Commented Nov 24, 2016 at 10:01
  • $\begingroup$ First plot the model with your starting values against the data and make sure that you've got at least the frequency approximately correct. $\endgroup$ Commented Nov 24, 2016 at 10:14
  • $\begingroup$ @Feyre thanks for your comment, I did specify the initial values (added a picture), but somehow Mathematica does not find the best values obviously. The file is a .dat-file, which is the reason that is not available for a priview in your browser, but can be imported to Matematica. Use this Link: (dropbox.com/s/xufd736fburdq8a/norm.dat?dl=0) $\endgroup$
    – Arne H.
    Commented Nov 24, 2016 at 10:32
  • 1
    $\begingroup$ You're plotting data3, but you're fitting against datafit, which doesn't have the x values. If you fit the same dataset that you're plotting, and you make sure the frequency starting value is chosen correctly, it will work. $\endgroup$ Commented Nov 24, 2016 at 10:59
  • 3
    $\begingroup$ The frequency starting value needs to be quite close, by the nature of fitting against an oscillating function. Just today there was another question about that: mathematica.stackexchange.com/questions/131933/… $\endgroup$ Commented Nov 24, 2016 at 11:05

1 Answer 1

4
$\begingroup$

FindFit does an excellent job without any starting values, at least in Mathematica 11

file = FileNames["norm.dat", NotebookDirectory[]];
datain = Import[file[[1]]];
model = Cos[a*x]*Exp[b*x];
fit = FindFit[datain, model, {a, b}, x];
plot = Plot[model /. fit, {x, 0, datain[[-1, 1]]}, 
  Epilog -> {Red, Line[datain]}]

enter image description here

$\endgroup$

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