0

I want to fit a gaussian function to a data below:

data = Block[{r = RandomReal[100, 100]},
              Transpose[{r, Exp[-(r - 50)^2/10] + RandomReal[.1, 100]}]];
lp = ListPlot[data, PlotRange -> All]

enter image description here

I fitted as follow:

FindFit[data, a E^(-(x - \[Mu])^2/(2 \[Sigma]^2)), {a, \[Mu], \[Sigma]}, x]

And the result was:

{a -> 1.02684, \[Mu] -> 50.0249, \[Sigma] -> 2.39315}

The result looks good.
I wanted to plot both the data points and the obtained gaussian function together, so I used Show with PlotRange -> All (without the option, the y-axis of the graph I got was up to 0.20):

Show[{ListPlot[data, PlotStyle -> Red],
      Plot[a E^(-((x - \[Mu])^2/(2 \[Sigma]^2))) /.
      {a -> 1.02684, \[Mu] -> 50.0249, \[Sigma] -> 2.39315}, {x, 1, 100}]},
      PlotRange -> All]

The output was, however, not what I wanted:

enter image description here

You can hardly ever see the gaussian function very near the x-axis only a little.

When I put additional PlotRange -> All as an option for the Plot for the gaussian, I got the desired result:

Show[{ListPlot[data, PlotStyle -> Red],
Plot[a E^(-((x - \[Mu])^2/(2 \[Sigma]^2))) /. {a -> 
     1.02684, \[Mu] -> 50.0249, \[Sigma] -> 2.39315}, {x, 1, 100}, PlotRange -> All]},
     PlotRange -> All]

enter image description here

I don't know the reason why I couldn't get the full plot of the gaussian with only the PlotRange -> All as an option for the Show?

Taiki Bessho
  • 886
  • 5
  • 15
  • 1
    If the discussion in the linked question does not solve your problem, please edit your question to explain why it is not applicable to your situation. – J. M.'s missing motivation Jun 30 '16 at 06:09
  • @J.M. Yeah, the question and answer you pointed out was what I wanted! My question was solved completely, so should I remove this question or do something other? – Taiki Bessho Jun 30 '16 at 06:22

0 Answers0