astropy/astropy — pull request #1494 by astrofrog (2013-11-09 11:37:55+00:00)
Title: Simplify documentation for creating new models

Now that @adonath has made it easier to create models (and I know there may still be changes to that in other pull requests), I think we can make the following page a lot simpler:

http://docs.astropy.org/en/latest/modeling/index.html

For example, the following is an example of creating a model that is the sum of two Gaussians:

```
import numpy as np
np.random.seed(12345)
import matplotlib.pyplot as plt

from astropy.modeling.functional_models import Custom1DModel
from astropy.modeling.fitting import NonLinearLSQFitter

def sum_of_gaussians(x, amplitude1=1., mean1=0., sigma1=1.,
amplitude2=1., mean2=1., sigma2=1.):
return (1. / (sigma1 * np.sqrt(2. * np.pi)) * np.exp(-0.5 * ((x - mean1) / sigma1)**2) +
1. / (sigma2 * np.sqrt(2. * np.pi)) * np.exp(-0.5 * ((x - mean2) / sigma2)**2))

# Generate fake data

x = np.linspace(-5., 5., 200)
y = sum_of_gaussians(x, amplitude1=1., mean1=-0.1, sigma1=0.2,
amplitude2=0.5, mean2=1., sigma2=0.3)
y += np.random.normal(0., 0.1, x.shape)

# Try fitting it

m = Custom1DModel(sum_of_gaussians)
fit = NonLinearLSQFitter(m)
fit(x, y)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x, y, 'o', color='k')
ax.plot(x, m(x), color='r')
fig.savefig('data.png')
```

pretty easy, and worked first go!

![data](https://f.cloud.github.com/assets/314716/1232862/ff1879e6-28ed-11e3-957e-741733deba65.png)