Skip to main content
Formatted code
Source Link
MarcoB
  • 67.7k
  • 18
  • 96
  • 198

I am trying to create a visual aid for peak fitting. There should be a background profile and on top, I want to place peaks. So far, I am stuck with the following issue. For simplicity, I am using a gaussian and two peaks. I have:

\[Mu]dμd = {5422.6501499999995`, 6450.7998`, 6711.8498500000005`, 
   6824.2998`, 8795.4004`, 9502.0996`};
{t0, t1} = {4690.5`, 11110.5`};

The following code works as intended (I manipulate the sigmas as I wish):

ClearAll[model0]
model0 = 
  Exp[-((t - \[Mu]d[[1]]μd[[1]])^2/(2 \[Sigma]1^2σ1^2))] + 
   Exp[-((t - \[Mu]d[[2]]μd[[2]])^2/(2 \[Sigma]2^2σ2^2))]; 

With[
 {localmodel = model0 /.
    {\[Sigma]1σ1 -> \[Sigma]v1σv1, \[Sigma]2σ2 -> \[Sigma]v2σv2}},
 Manipulate[
  Quiet@Plot[localmodel, {t, t0, t1}, PlotRange -> Full],
  {{\[Sigma]v1σv1, 100}, 0.01, t1 - t0}, 
  {{\[Sigma]v2σv2, 100}, 0.01, 
   t1 - t0}
  ]
 ]

Since, I am planning to have more peaks in the model, the above will become soon cumbersome and iI was hoping to simplify things by defining the model compactly but the approach below is not working (I have never understood how Mathematica interprets subscripts):

model1 = 
 Sum[Exp[-((t - \[Mu]d[[i]]μd[[i]])^2/(2 Subscript[\[Sigma]Subscript[σ, i]^2))], {i, 1, 
   2}] 

With[
 {localmodel = model1 /.
    {\[Sigma]σ -> \[Sigma]vσv}
  },
 Manipulate[
  Show[
   Plot[model1[t, \[Sigma]]σ], {t, t0, t1}, PlotRange -> Full],
   ],
  {{Subscript[\[Sigma]vSubscript[σv, 1], 1000}, 0.01, 
   t1 - t0}, 
  {{Subscript[\[Sigma]vSubscript[σv, 2], 1000}, 0.01, t1 - t0}
  ]
 ]

How should I go about this so that I can define things compactly, ideally also in the Manipulate variables?

I am trying to create a visual aid for peak fitting. There should be a background profile and on top, I want to place peaks. So far, I am stuck with the following issue. For simplicity, I am using a gaussian and two peaks. I have:

\[Mu]d = {5422.6501499999995`, 6450.7998`, 6711.8498500000005`, 
   6824.2998`, 8795.4004`, 9502.0996`};
{t0, t1} = {4690.5`, 11110.5`};

The following code works as intended (I manipulate the sigmas as I wish):

ClearAll[model0]
model0 = 
  Exp[-((t - \[Mu]d[[1]])^2/(2 \[Sigma]1^2))] + 
   Exp[-((t - \[Mu]d[[2]])^2/(2 \[Sigma]2^2))];
With[
 {localmodel = model0 /.
    {\[Sigma]1 -> \[Sigma]v1, \[Sigma]2 -> \[Sigma]v2}},
 Manipulate[
  Quiet@Plot[localmodel, {t, t0, t1}, PlotRange -> Full],
  {{\[Sigma]v1, 100}, 0.01, t1 - t0}, {{\[Sigma]v2, 100}, 0.01, 
   t1 - t0}
  ]
 ]

Since, I am planning to have more peaks in the model, the above will become soon cumbersome and i was hoping to simplify things by defining the model compactly but the approach below is not working (I have never understood how Mathematica interprets subscripts):

model1 = 
 Sum[Exp[-((t - \[Mu]d[[i]])^2/(2 Subscript[\[Sigma], i]^2))], {i, 1, 
   2}]
With[
 {localmodel = model1 /.
    {\[Sigma] -> \[Sigma]v}
  },
 Manipulate[
  Show[
   Plot[model1[t, \[Sigma]], {t, t0, t1}, PlotRange -> Full],
   ],
  {{Subscript[\[Sigma]v, 1], 1000}, 0.01, 
   t1 - t0}, {{Subscript[\[Sigma]v, 2], 1000}, 0.01, t1 - t0}
  ]
 ]

How should I go about this so that I can define things compactly, ideally also in the Manipulate variables?

I am trying to create a visual aid for peak fitting. There should be a background profile and on top, I want to place peaks. So far, I am stuck with the following issue. For simplicity, I am using a gaussian and two peaks. I have:

μd = {5422.6501499999995`, 6450.7998`, 6711.8498500000005`, 
   6824.2998`, 8795.4004`, 9502.0996`};
{t0, t1} = {4690.5`, 11110.5`};

The following code works as intended (I manipulate the sigmas as I wish):

ClearAll[model0]
model0 = Exp[-((t - μd[[1]])^2/(2 σ1^2))] + Exp[-((t - μd[[2]])^2/(2 σ2^2))]; 

With[
 {localmodel = model0 /. {σ1 -> σv1, σ2 -> σv2}},
 Manipulate[
  Quiet@Plot[localmodel, {t, t0, t1}, PlotRange -> Full],
  {{σv1, 100}, 0.01, t1 - t0}, 
  {{σv2, 100}, 0.01, t1 - t0}
 ]
]

Since I am planning to have more peaks in the model, the above will become soon cumbersome and I was hoping to simplify things by defining the model compactly but the approach below is not working (I have never understood how Mathematica interprets subscripts):

model1 = Sum[Exp[-((t - μd[[i]])^2/(2 Subscript[σ, i]^2))], {i, 1, 2}] 

With[
 {localmodel = model1 /. {σ -> σv}},
 Manipulate[
  Show[
   Plot[model1[t, σ], {t, t0, t1}, PlotRange -> Full],
  ],
  {{Subscript[σv, 1], 1000}, 0.01, t1 - t0}, 
  {{Subscript[σv, 2], 1000}, 0.01, t1 - t0}
 ]
]

How should I go about this so that I can define things compactly, ideally also in the Manipulate variables?

Source Link
atapaka
  • 4k
  • 1
  • 14
  • 33

Fitting data to peaks visually with manipulate - how to compactly implement for many peaks

I am trying to create a visual aid for peak fitting. There should be a background profile and on top, I want to place peaks. So far, I am stuck with the following issue. For simplicity, I am using a gaussian and two peaks. I have:

\[Mu]d = {5422.6501499999995`, 6450.7998`, 6711.8498500000005`, 
   6824.2998`, 8795.4004`, 9502.0996`};
{t0, t1} = {4690.5`, 11110.5`};

The following code works as intended (I manipulate the sigmas as I wish):

ClearAll[model0]
model0 = 
  Exp[-((t - \[Mu]d[[1]])^2/(2 \[Sigma]1^2))] + 
   Exp[-((t - \[Mu]d[[2]])^2/(2 \[Sigma]2^2))];
With[
 {localmodel = model0 /.
    {\[Sigma]1 -> \[Sigma]v1, \[Sigma]2 -> \[Sigma]v2}},
 Manipulate[
  Quiet@Plot[localmodel, {t, t0, t1}, PlotRange -> Full],
  {{\[Sigma]v1, 100}, 0.01, t1 - t0}, {{\[Sigma]v2, 100}, 0.01, 
   t1 - t0}
  ]
 ]

Since, I am planning to have more peaks in the model, the above will become soon cumbersome and i was hoping to simplify things by defining the model compactly but the approach below is not working (I have never understood how Mathematica interprets subscripts):

model1 = 
 Sum[Exp[-((t - \[Mu]d[[i]])^2/(2 Subscript[\[Sigma], i]^2))], {i, 1, 
   2}]
With[
 {localmodel = model1 /.
    {\[Sigma] -> \[Sigma]v}
  },
 Manipulate[
  Show[
   Plot[model1[t, \[Sigma]], {t, t0, t1}, PlotRange -> Full],
   ],
  {{Subscript[\[Sigma]v, 1], 1000}, 0.01, 
   t1 - t0}, {{Subscript[\[Sigma]v, 2], 1000}, 0.01, t1 - t0}
  ]
 ]

How should I go about this so that I can define things compactly, ideally also in the Manipulate variables?