I have written the following code:
fit6peakslorentz[data_, minpos_] := Module[
{pltdta, fit, fitfu, pltfit, fit1, mini, maxi, amp0, begin, end},
pltdta = ListPlot[data, PlotRange -> All];
(* start vals *)
mini = Min[Transpose[data][[2]]];
maxi = Max[Transpose[data][[2]]];
(* x00 = Select[data, #[[2]] \[Equal] mini &][[1]][[1]];*)
amp0 = mini - maxi;
begin = Last[Transpose[data][[1]]];
end = First[Transpose[data][[1]]];
(* now fit *)
fit = NonlinearModelFit[
data,
c + q1 (y1^2/((x - x01)^2 + y1^2))
+ q2 (y2^2/((x - x02)^2 + y2^2))
+ q3 (y3^2/((x - x03)^2 + y3^2))
+ q4 (y4^2/((x - x04)^2 + y4^2))
+ q5 (y5^2/((x - x05)^2 + y5^2))
+ q6 (y6^2/((x - x06)^2 + y6^2)),
{
{c, maxi},
{q1, amp0}, {x01, minpos[[1]]}, {y1, 1},
{q2, amp0}, {x02, minpos[[2]]}, {y2, 1},
{q3, amp0}, {x03, minpos[[3]]}, {y3, 1},
{q4, amp0}, {x04, minpos[[4]]}, {y4, 1},
{q5, amp0}, {x05, minpos[[5]]}, {y5, 1},
{q6, amp0}, {x06, minpos[[6]]}, {y6, 1}
},
x
];
fitfu[x_] = c + q1 (y1^2/((x - x01)^2 + y1^2))
+ q2 (y2^2/((x - x02)^2 + y2^2))
+ q3 (y3^2/((x - x03)^2 + y3^2))
+ q4 (y4^2/((x - x04)^2 + y4^2))
+ q5 (y5^2/((x - x05)^2 + y5^2))
+ q6 (y6^2/((x - x06)^2 + y6^2)) /. fit["BestFitParameters"];
pltfit =
Plot[fitfu[x], {x, begin, end}, PlotRange -> All, PlotStyle -> Red];
Print[Show[pltdta, pltfit, PlotRange -> {{begin, end}, All}]];
fit["ParameterTable"]
]
but it fails to fit the way I want it to. Entering the peaks manually the code finds them fine but the offset is all wrong: