1
$\begingroup$

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)"}]
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Jinxed Feb 23 '15 at 1:43
  • $\begingroup$ 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. $\endgroup$ – bbgodfrey Feb 23 '15 at 1:48
0
$\begingroup$

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, 
    MapThread[Line[{#1, #2}] &, {RdataLow, RdataHigh}]}}]

the whole plot

$\endgroup$
  • $\begingroup$ I got a function value error? and invalid variables? $\endgroup$ – Daniel Schulze Feb 23 '15 at 1:58
  • $\begingroup$ @DanielSchulze: What do you mean by that? Of course, the quality of the fit depends on the function you want to fit for. $\endgroup$ – Jinxed Feb 23 '15 at 9:21
  • $\begingroup$ And the fitting method itself. $\endgroup$ – Jinxed Feb 23 '15 at 9:32
  • $\begingroup$ I am still having trouble understanding the code and the error message that the program is giving me. $\endgroup$ – Daniel Schulze Mar 3 '15 at 5:25
  • $\begingroup$ @DanielSchulze: What error message? $\endgroup$ – Jinxed Mar 3 '15 at 10:27

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.