1
$\begingroup$

I have a fitted model given in the code below and I am trying to shift the function to get its maxima in the center ($t=0$). The variable center[i] controls the center position of the basis function and therefore I want to shift all the centers of these basis functions (here I have used 2 basis functions).

NBasisFun = 2;    
model = Sum[a[i] Exp[-(b[i])^2 (t - center[i])^2], {i, 1, NBasisFun, 1}];
fitPrams = {a[1] -> 6.13531, a[2] -> 0.501507, b[1] -> 4.97662, b[2] -> 1.82646, 
center[1] -> -0.127246, center[2] -> -0.393956};

Plot[model /. fitPrams, {t, -1, 1}]

To shift all the basis functions, I find the maxima of the fitted function and the corresponding $t$ value (PeakLocation) using NMaximize then I create a new model i.e. PeakShiftedModel where I add this value to center[i].

PeakLocation = t /. Last[NMaximize[model /. fitPrams, t]]
PeakShiftedModel = Sum[a[i] Exp[-(b[i])^2 (t - center[i] + PeakLocation)^2], 
{i, 1, NBasisFun, 1}] /. fitPrams

Plot[PeakShiftedModel, {t, -1, 1}]

While this approach works, I want to change directly the fitted parameters by adding the PeakLocation to the center positions (center[i]) of the fitted parameters fitPrams. Something that looks like this:

fitPrams = {a[1] -> 6.13531, a[2] -> 0.501507, b[1] -> 4.97662, b[2] -> 1.82646, 
center[1] -> -0.127246 - PeakLocation, center[2] -> -0.393956 - PeakLocation};

How do I do it?

$\endgroup$
  • 2
    $\begingroup$ Try fitPrams /. (center[k_] -> val_) :> (center[k] -> val - PeakLocation). $\endgroup$ – J. M. is away Mar 14 '18 at 11:53
  • $\begingroup$ yes, that seems to work good. $\endgroup$ – dykes Mar 14 '18 at 11:58
1
$\begingroup$
fitPrams[[-2 ;;, -1]] = fitPrams[[-2 ;;, -1]] - PeakLocation; 
fitPrams

{a[1] -> 6.13531, a[2] -> 0.501507, b[1] -> 4.97662, b[2] -> 1.82646, center[1] -> 0.00230586, center[2] -> -0.264404}

$\endgroup$

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.