# How to add error bars to a fit function in Mathematica 10?

My professor gave us the following code to fit a Lorentzian to our data. I want to add vertical error bars (with individual error found in the experiment) to the plot but I don't know how. Any suggestions and related material would be appreciated!

    Clear[a, b, c, f]

Rdata = {{1, 8.404}, {9, 11.312}, {16, 53.500}, {17, 57.308}, {18,
57.491}, {19, 7.315}, {36, 2.450}, {54, 0.182}, {90, 2.787}, {180,
2.634}};
Rdataerror = {{{1, 8.404}, 0.003}, {{9, 11.312},
0.011}, {{16, 53.500}, 0.556}, {{17, 57.308},
0.770}, {{18, 57.491}, 1.220}, {{19, 7.315}, 4.926}, {{36, 2.450},
0.056}, {{54, 0.149}, 1.6}, {{90, 2.787}, 0.0718}, {{180, 2.634},
0.512}};

phidata = {{1, 0.007}, {9, 0.03}, {16, -0.125}, {17, -0.29}, {18,
0.82}, {19, -4.432}, {36, 3.08}, {54, -1.94}, {90, -1.45}, {180, -2.39}};

phidataerror = {{{1, 0.007}, 0.005}, {{9, 0.03},
0.002}, {{16, -0.125}, 0.003}, {{17, -0.29}, 0.031}, {{18, 0.82},
0.1}, {{19, -4.432}, 1}, {{36, 3.08}, 0.04}, {{54, -1.94},
1.6}, {{90, -1.45}, 0.12}, {{180, -2.39}, 0.39}};

FindFit[Rdata, a/Sqrt[(f^2 - b^2)^2 + f^2 c^2], {a, {b, 2}, {c, 56}}, f]

disp[f_] = a/Sqrt[(f^2 - b^2)^2 + f^2 c^2] /. %;

phi[f_] = Piecewise[{{Pi - ArcTan[c f/(f^2 - b^2)], -c f/(f^2 - b^2) <
0}}, -ArcTan[c f/(f^2 - b^2)]] /. %%;

Show[Plot[disp[f], {f, 0, 60}, PlotRange -> {0, 80}], ListPlot[Rdata],AxesLabel -> {"f (Hz)", "amplitude ratio R"}]

Show[Plot[disp[f], {f, 100, 600}, PlotRange -> {0, 3}], ListPlot[Rdata], AxesLabel -> {"f (Hz)", "amplitude ratio R"}]

Show[Plot[phi[f], {f, 0, 200}, PlotRange -> {0, 3.2}], ListPlot[phidata], AxesLabel -> {"f (Hz)", "- (delta phi)"}]

Show[Plot[phi[f], {f, 15, 20}, PlotRange -> {0, 3.2}], ListPlot[phidata], AxesLabel -> {"f (Hz)", "- (delta phi)"}]

• As I put together my answer, and looking at the graph of Rdata: Is there also error in the x-direction? This might be the case, or the fitting function might be suboptimal. Commented Feb 23, 2015 at 1:43
• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. Commented Feb 23, 2015 at 1:48

As requested, I try to give some different options on displaying error (besides the obvious Error Bar Package):

1. Generate the minimum and maximum datapoints:
RdataLow=MapThread[{#1[$1$], #1[$2$] - .5 #2[$2$]} &, {Rdata, Rdataerror}]; RdataHigh=MapThread[{#1[$1$], #1[$2$] + .5 #2[$2$]} &, {Rdata, Rdataerror}];
2. Then, find fits for all datapoints (min, ideal, max) at once:
fits = FindFit[#, a/Sqrt[(f^2 - b^2)^2 + f^2 c^2], {a, {b, 2}, {c, 56}}, f] & /@ {Rdata, RdataLow, RdataHigh}

3. And finally: Plot all of them at once:
Plot[Evaluate[a/Sqrt[(f^2 - b^2)^2 + f^2 c^2] /. fits], (* ... *), Filling -> {2 -> {3}}]

Of course, you could also just Show multiple plots at once, e.g. a ListPlot of the errors together with the original plots. Or you could just display the error values by using the Epilog:>{} option within one of your plot commands like so:

Plot[(* ... *), Epilog :> {Text[#[[2]], #[[1]]] & /@ Rdataerror}]


Same goes for the vertical error bars: You would in that case just use Line, not Text, using the low and high datasets created before, which, all combined, results in:

Plot[Evaluate[a/Sqrt[(f^2 - b^2)^2 + f^2 c^2] /. fits], {f, 0, 80},
PlotRange -> All, Filling -> {2 -> {3}},
Epilog :> {{PointSize[Large], Blue,
Point /@ Rdata}, {Thickness[Large], Red,