下面的示例类似于astropy文档中给出的示例。
复合模型=两个1D高斯函数的和。
约束:mean_1 = 2 * mean_0
import numpy as np import matplotlib.pyplot as plt from astropy.modeling import models, fitting def tie_center(model): mean = 2* model.mean_0 return mean tied_parameters = {'mean_1': tie_center} np.random.seed(42) g1 = models.Gaussian1D(2, 0.4, 0.3) g2 = models.Gaussian1D(2.5, 0.2, 0.2) TwoGaussians = (models.Gaussian1D + models.Gaussian1D).rename('TwoGaussians') x = np.linspace(-1, 1, 200) y = g1(x) + g2(x) + np.random.normal(0., 0.2, x.shape) gg_init = TwoGaussians(amplitude_0=1.4, mean_0=1.2, stddev_0=0.1,\ amplitude_1=1.0,stddev_1=0.2, tied=tied_parameters) fitter = fitting.SLSQPLSQFitter() gg_fit = fitter(gg_init, x, y) plt.figure(figsize=(8,5)) plt.plot(x, y, 'ko') plt.plot(x, gg_fit(x)) plt.xlabel('Position') plt.ylabel('Flux') plt.show() print(gg_fit.mean_0,gg_fit.mean_1)
当Compund_model =三个1D高斯函数之和时,在星座中绑定参数 约束:所有三种方法的总和应始终等于1。
def tie_center(model): mean = 1-(model.mean_0+ model.mean_1) return mean tied_parameters = {'mean_2': tie_center} np.random.seed(42) g1 = models.Gaussian1D(2, 0.4, 0.3) g2 = models.Gaussian1D(2.5, 0.2, 0.2) g3 = models.Gaussian1D(1.5, 0.4, 0.1) ThreeGaussians = (models.Gaussian1D + models.Gaussian1D + models.Gaussian1D).rename('ThreeGaussians') x = np.linspace(-1, 1, 200) y = g1(x) + g2(x) + g3(x) + np.random.normal(0., 0.2, x.shape) gg_init = ThreeGaussians(amplitude_0=1.4, mean_0=0.3, stddev_0=0.1, amplitude_1=1.0, mean_1=0.3,stddev_1=0.2, \ amplitude_2=1.5,stddev_2=0.1,tied=tied_parameters) fitter = fitting.SLSQPLSQFitter() gg_fit = fitter(gg_init, x, y) plt.figure(figsize=(8,5)) plt.plot(x, y, 'ko') plt.plot(x, gg_fit(x)) plt.xlabel('Position') plt.ylabel('Flux') plt.show() print(gg_fit.mean_0,gg_fit.mean_1, gg_fit.mean_2)