I had asked a question regarding simultaneous plotting here.
My Program will fit real part and imaginary part of a data simultaneously and gives out the value something like this
I column has frequency, II column has Amplitud, III column has Phase.
Here is my modified program from the responses I got from previous question.
Qfit[data1_, fmin_, fmax_] := Module[{da1 = {}, da2 = {}, L, A, B, kint, kport, f, f0, index, mini,
res, i, r, myF, nlm, data = {}, allData}, Clear[A, B, kint, kport, f, f0, index, mini, res, L]; data = Select[data1, fmin <= #[[1]] <= fmax &];da1 = Transpose[{data[[All, 1]],
data[[All, 2]]*Cos[data[[All, 3]]]}];da2 = Transpose[{data[[All,
1]], (data[[All, 2]]*Sin[data[[All, 3]]])}]; mini = Flatten[Position[da1[[All, 2]], Min[da1[[All, 2]]]]][[1]];res = da1[[mini, 1]];allData =Join[{1, Sequence @@ #} & /@ da1, {2, Sequence @@ #} & /@ da2]; i[f_] := A*B*(4 (f - f0)^2 + (kint - kport) (kint + kport))/(4 (f - f0)^2 + (kint + kport)^2);r[f_] := A*(1 - B (4 kport (f - f0))/(4 (f - f0)^2 + (kint + kport)^2));myF[index_, f_] := KroneckerDelta[index - 1] i[f] + KroneckerDelta[index - 2] r[f]; nlm = NonlinearModelFit[allData, myF[index,
f], {{kint, 0.001}, {kport, 0.001}, {f0, res}, {A, 0.1}, {B,
0.05}}, {index, f}, MaxIterations -> Infinity];fitparams = nlm["BestFitParameters"]; Print[fitparams];Set @@@ fitparams;Print["Internal Quality factor (Loss inside cavity) = ", f0/kint, " External Quality factor (Input port 1 loss) = ", f0/kport, " Total loss or Quality factor = ", f0/(kint + kport)]; Print["The graphs are ", Show[ListPlot[{da1, da2}],
Plot[{nlm[1, f], nlm[2, f]}, {f, fmin, fmax}],
ImageSize -> Large],Show[ListPlot[Transpose@{Last /@ da1, Last /@ da2}, Joined -> True,
AxesLabel -> {"Re", "Im"}],
ParametricPlot[{nlm[1, f], nlm[2, f]}, {f, Min@(First /@ da1),
Max@(First /@ da1)}, PlotStyle -> Red], PlotRange -> All,
ImageSize -> Large]]]
This usually work for all most all the values, but for some data like this,here is a data from experiment.
For this we have to compute as follows Qfit[data,10.45,10.475].
It is not converging at all. I do not why it is happening I appreciate any quick responses, and sorry for my ignorance.
r[f]
piece the resulting standard errors are near zero for all parameters and for thei[f]
piece the resulting standard errors are also all near zero with a correlation matrix of all 1's or -1's. $\endgroup$