0
$\begingroup$

How do I find a fit for the dataS (see below) with the constraint that the parameter np is an integer, and that the parameters la+lc = 100. A weak attempt at this is provided below for a specific model.

Clear[dataS, np, th, lc, la, sc, sa, q]

dataS = {{0.0200243, 8.51113}, {0.0223801, 7.38727}, {0.0247359, 6.49431}, {0.0270917, 5.78094}, {0.0294475, 5.24383}, {0.0318033, 4.85481}, {0.0341591, 4.58093}, {0.0365148, 4.38386}, {0.0388706, 4.26912}, {0.0412264, 4.19256}, {0.0435821, 4.1748}, {0.0459379, 4.14528}, {0.0482937, 4.10548}, {0.0506494, 4.01107}, {0.0530051, 3.82845}, {0.0553609, 3.54445}, {0.0577166, 3.1787},  {0.0600723, 2.74405}, {0.062428, 2.29171}, {0.0647837, 1.8642}, {0.0671394, 1.48718}, {0.0694951, 1.16843}, {0.0718508, 0.927279}, {0.0742065, 0.735171}, {0.0765621, 0.588362}, {0.0789178, 0.476123}, {0.0812734, 0.388092}, {0.083629, 0.319444}, {0.0859847, 0.264057}, {0.0883403, 0.220424}, {0.0906959, 0.184743}, {0.0930515, 0.155816}, {0.0954071, 0.132327}, {0.0977626, 0.113781}, {0.100118, 0.0978715}, {0.102474, 0.0844484}, {0.104829, 0.0736506}, {0.107185, 0.0651143}, {0.10954, 0.057349}}; 

Fc[q_, lc_, sc_] := Exp[I*lc*q]*Exp[-0.5*sc^2*q^2]; 
Fa[q_, la_, sa_] := Exp[I*la*q]*Exp[-0.5*sa^2*q^2]; 
ModelP = (2/q^4)*Re[((1 - Fc[q, lc, sc])*(1 - Fa[q, la, sa]))/(1 - Fc[q, lc, sc]*Fa[q, la, sa]) + (1/np)*((Fa[q, la, sa]*(1 - Fc[q, lc, sc])^2)/(1 - Fc[q, lc, sc]*Fa[q, la, sa])^2)*
       (1 - Fc[q, lc, sc]*Fa[q, la, sa])^np]; 
f1 = FindFit[dataS, {ModelP, {4 < np < 50, 10 < la < 30, 60 < lc < 100, 1 < sa < 40, 1 < sc < 40}}, {np, la, lc, sa, sc}, q]

Output: {np -> 9.66343, la -> 14.9484, lc -> 63.5122, sa -> 23.7186, sc -> 2.7161}

$\endgroup$
  • 1
    $\begingroup$ It is good to note that your use of N as a variable is a no-no. N is a built-in function. There are a few more single letter reserved symbols (C, D, E, I, O), so it's best to avoid all capitalized variable names (you can use capitalization using lower camel case, though). That way you avoid any conflict with built-in functions that are always capitalized. $\endgroup$ – Sjoerd C. de Vries Nov 16 '14 at 16:08
  • $\begingroup$ @ Sjoerd Agree, have changed N to Np $\endgroup$ – thils Nov 16 '14 at 20:01
  • 1
    $\begingroup$ Well, actually I was suggesting not to capitalize the first letter of a symbol name. So, although Np is slightly better I'd advise against it. $\endgroup$ – Sjoerd C. de Vries Nov 16 '14 at 20:14
  • $\begingroup$ @ Sjoerd changed Np to np $\endgroup$ – thils Nov 16 '14 at 22:52
4
$\begingroup$

The requirement that la + lc == 100 is simple to implement, just pass 100 - la to Fc, or use

ModelP/.{lc->100-la}

in the call to FindFit.

For the other trouble, remember that N is a special function in Mathematica, so avoid ever using this as a variable. Using n instead, I tried

ModelP = (2/q^4)*Re[((1 - Fc[q, lc, sc])*(1 - Fa[q, la, sa]))/
(1 - Fc[q, lc, sc]*Fa[q, la, sa]) +
(1/n)*((Fa[q, la, sa]*(1 - Fc[q, lc, sc])^2)/
(1 - Fc[q, lc, sc]*Fa[q, la, sa])^2)*(1 - Fc[q, lc, sc]*Fa[q, la, sa])^n];

f1 = FindFit[
dataS, {ModelP /. {lc -> 100 - la}, {4 < n < 50, 10 < la < 30, 
1 < sa < 40, 1 < sc < 40, n \[Element] Integers}}, {n, la, lc, sa,
sc}, q]

but I get the error message:

"Constraints in {n[Element]Integers,1 < sa,1 < sc,4 < n,10 < la,la < 30,n < 50,sa < 40,sc < 40} are not all equality or inequality constraints. With the exception of integer domain constraints for linear programming, domain constraints or constraints with Unequal (!=) are not supported."

A workaround could be to make the model a function of n, and then run FindFit for several integer values of n and compare the results.

$\endgroup$
  • $\begingroup$ Still unable to use the above input to get anywhere close to the answer, useful nevertheless. If I remove (4 < n < 50) the error message disappears. $\endgroup$ – thils Nov 17 '14 at 3:31
  • $\begingroup$ Yes, this is what I meant. If you want to test that range (4 < n < 50) then do f1 = Table[FindFit[ dataS, {ModelP /. {lc -> 100 - la,n -> i}, {10 < la < 30, 1 < sa < 40, 1 < sc < 40}}, {la, lc, sa, sc}, q], {i, 5, 49} ]. Then you can compare the results for different n. $\endgroup$ – Marius Ladegård Meyer Nov 17 '14 at 7:09

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.