# Automatically fiting experimental data using NonLinearModelFit

Please, I have been working for some time to perform an automatic fit of my experimental data using an analytical function developed from a model based on the theory of kinematical diffraction.

The idea is to find the parameters (b, t, m, L) corresponding to the best fit. So far, I have only been able to perform a manual fit. But when I try to do an automatic fit using the function 'FindFit' or 'NonlinearModelFit', neither of the two provide me with a solution. After having a look on some couple of solutions proposed in the forum, I conclude that the analytical function used to fit the data might be the problem.

I am a new Mathematica user, not a professional programmer, but I am willing to learn. Please, any solution helping me to fit my data would be very welcome. The code is below.

I do not know how to upload the expe. data file, therefore I copied and paste it.

(******* Experimental Data *******)

data = {{-0.0240482,-1.81326},{-0.0239912,-1.73542},{-0.0239342,-1.45053},{-0.0238772,-1.22645},{-0.0238202,-1.0405},{-0.0237632,-0.77869},{-0.0237063,-0.497861},{-0.0236493,-0.259655},{-0.0235923,-0.0823257},{-0.0235353,-0.0318},{-0.0234783,0.0531861},{-0.0234213,0.0445949},{-0.0233643,-0.30208},{-0.0233074,-1.26823},{-0.0232504,-1.52536},{-0.0231934,-1.68997},{-0.0231364,-1.74257},{-0.0230794,-1.83779},{-0.0230224,-1.95102},{-0.0229654,-2.0238},{-0.0229085,-2.0797},{-0.0228515,-2.12749},{-0.0227945,-2.13166},{-0.0227375,-2.17023},{-0.0226805,-2.16627},{-0.0226235,-2.16424},{-0.0225665,-2.16236},{-0.0225096,-2.1559},{-0.0224526,-2.15125},{-0.0223956,-2.22758},{-0.0223386,-2.39086},{-0.0222816,-2.41948},{-0.0222246,-2.46173},{-0.0221676,-2.45458},{-0.0221106,-2.50036},{-0.0220537,-2.47597},{-0.0219967,-2.45513},{-0.0219397,-2.51579},{-0.0218827,-2.47304},{-0.0218257,-2.48605},{-0.0217687,-2.45129},{-0.0217117,-2.3948},{-0.0216548,-2.29378},{-0.0215978,-2.2838},{-0.0215408,-2.25057},{-0.0214838,-2.20268},{-0.0214268,-2.26881},{-0.0213698,-2.36599},{-0.0213128,-2.40492},{-0.0212559,-2.40551},{-0.0211989,-2.38133},{-0.0211419,-2.37128},{-0.0210849,-2.36717},{-0.0210279,-2.37263},{-0.0209709,-2.35605},{-0.0209139,-2.3188},{-0.020857,-2.27561},{-0.0208,-2.22494},{-0.020743,-2.15167},{-0.020686,-2.03491},{-0.020629,-1.99562},{-0.020572,-1.93279},{-0.020515,-1.9041},{-0.020458,-1.92548},{-0.0204011,-2.03569},{-0.0203441,-2.05815},{-0.0202871,-1.99919},{-0.0202301,-1.96485},{-0.0201731,-1.90343},{-0.0201161,-1.83458},{-0.0200591,-1.71522},{-0.0200022,-1.34334},{-0.0199452,-1.00972},{-0.0198882,-0.759807},{-0.0198312,-0.531968},{-0.0197742,-0.276069},{-0.0197172,-0.0262041},{-0.0196602,0.19532},{-0.0196033,0.23909},{-0.0195463,0.320668},{-0.0194893,0.340291},{-0.0194323,0.06475},{-0.0193753,-1.01243},{-0.0193183,-1.31493},{-0.0192613,-1.45398},{-0.0192044,-1.52927},{-0.0191474,-1.61448},{-0.0190904,-1.72186},{-0.0190334,-1.7674},{-0.0189764,-1.78645},{-0.0189194,-1.81483},{-0.0188624,-1.8122},{-0.0188054,-1.83276},{-0.0187485,-1.77184},{-0.0186915,-1.70848},{-0.0186345,-1.69685},{-0.0185775,-1.66327},{-0.0185205,-1.63792},{-0.0184635,-1.75626},{-0.0184065,-1.92733},{-0.0183496,-1.95626},{-0.0182926,-1.95523},{-0.0182356,-1.99048},{-0.0181786,-2.00506},{-0.0181216,-1.97263},{-0.0180646,-2.00856},{-0.0180076,-1.98286},{-0.0179507,-1.99435},{-0.0178937,-1.96325},{-0.0178367,-1.88783},{-0.0177797,-1.78381},{-0.0177227,-1.68787},{-0.0176657,-1.63688},{-0.0176087,-1.62247},{-0.0175518,-1.62042},{-0.0174948,-1.76724},{-0.0174378,-2.02084},{-0.0173808,-2.15678},{-0.0173238,-2.19226},{-0.0172668,-2.25504},{-0.0172098,-2.31423},{-0.0171528,-2.36157},{-0.0170959,-2.34712},{-0.0170389,-2.34767},{-0.0169819,-2.38444},{-0.0169249,-2.3965},{-0.0168679,-2.41865},{-0.0168109,-2.42785},{-0.0167539,-2.42284},{-0.016697,-2.43986},{-0.01664,-2.41052},{-0.016583,-2.43452},{-0.016526,-2.46266},{-0.016469,-2.48967},{-0.016412,-2.50556},{-0.016355,-2.48099},{-0.0162981,-2.44751},{-0.0162411,-2.41455},{-0.0161841,-2.31878},{-0.0161271,-2.26112},{-0.0160701,-1.78756},{-0.0160131,-1.41864},{-0.0159561,-1.21275},{-0.0158992,-0.975437},{-0.0158422,-0.688694},{-0.0157852,-0.409156},{-0.0157282,-0.260388},{-0.0156712,-0.220808},{-0.0156142,-0.138843},{-0.0155572,-0.160181},{-0.0155002,-0.584171},{-0.0154433,-1.60568},{-0.0153863,-1.8307},{-0.0153293,-1.92421},{-0.0152723,-1.92228},{-0.0152153,-1.99669},{-0.0151583,-2.04497},{-0.0151013,-2.0652},{-0.0150444,-2.02528},{-0.0149874,-2.05857},{-0.0149304,-2.04167},{-0.0148734,-1.98113},{-0.0148164,-1.91877},{-0.0147594,-1.85719},{-0.0147024,-1.81132},{-0.0146455,-1.75004},{-0.0145885,-1.76104},{-0.0145315,-1.87269},{-0.0144745,-1.97751},{-0.0144175,-1.97303},{-0.0143605,-1.95133},{-0.0143035,-1.93812},{-0.0142466,-1.91414},{-0.0141896,-1.91244},{-0.0141326,-1.8895},{-0.0140756,-1.88},{-0.0140186,-1.7915},{-0.0139616,-1.72628},{-0.0139046,-1.60982},{-0.0138476,-1.50794},{-0.0137907,-1.37058},{-0.0137337,-1.3168},{-0.0136767,-1.28625},{-0.0136197,-1.27518},{-0.0135627,-1.41303},{-0.0135057,-1.69524},{-0.0134487,-1.83509},{-0.0133918,-1.87082},{-0.0133348,-1.88469},{-0.0132778,-1.92229},{-0.0132208,-1.93458},{-0.0131638,-1.92521},{-0.0131068,-1.88975},{-0.0130498,-1.93906},{-0.0129929,-1.92025},{-0.0129359,-1.88657},{-0.0128789,-1.84528},{-0.0128219,-1.78502},{-0.0127649,-1.73014},{-0.0127079,-1.71764},{-0.0126509,-1.67656},{-0.012594,-1.76685},{-0.012537,-1.94884},{-0.01248,-1.9754},{-0.012423,-1.96123},{-0.012366,-1.96204},{-0.012309,-1.93664},{-0.012252,-1.91499},{-0.012195,-1.81067},{-0.0121381,-1.34888},{-0.0120811,-1.05175},{-0.0120241,-0.851894},{-0.0119671,-0.574415},{-0.0119101,-0.267068},{-0.0118531,0.0216929},{-0.0117961,0.133511},{-0.0117392,0.179129},{-0.0116822,0.284151},{-0.0116252,0.162886},{-0.0115682,-0.514862},{-0.0115112,-1.42774},{-0.0114542,-1.73293},{-0.0113972,-1.8503},{-0.0113403,-1.9404},{-0.0112833,-2.06367},{-0.0112263,-2.1096},{-0.0111693,-2.1643},{-0.0111123,-2.16087},{-0.0110553,-2.21029},{-0.0109983,-2.24697},{-0.0109414,-2.24677},{-0.0108844,-2.25423},{-0.0108274,-2.23154},{-0.0107704,-2.23813},{-0.0107134,-2.23926},{-0.0106564,-2.20911},{-0.0105994,-2.26066},{-0.0105424,-2.22659},{-0.0104855,-2.19798},{-0.0104285,-2.20054},{-0.0103715,-2.17932},{-0.0103145,-2.13512},{-0.0102575,-2.13751},{-0.0102005,-2.09929},{-0.0101435,-2.08432},{-0.0100866,-2.03684},{-0.0100296,-1.97185},{-0.00997259,-1.8733},{-0.0099156,-1.78133},{-0.00985861,-1.68903},{-0.00980163,-1.62204},{-0.00974464,-1.59357},{-0.00968765,-1.59105},{-0.00963067,-1.69349},{-0.00957368,-1.83792},{-0.0095167,-1.8851},{-0.00945971,-1.89841},{-0.00940272,-1.86893},{-0.00934574,-1.87453},{-0.00928875,-1.85286},{-0.00923177,-1.83693},{-0.00917478,-1.82872},{-0.00911779,-1.81087},{-0.00906081,-1.7821},{-0.00900382,-1.75166},{-0.00894683,-1.67885},{-0.00888985,-1.60178},{-0.00883286,-1.58332},{-0.00877588,-1.54338},{-0.00871889,-1.53052},{-0.0086619,-1.59064},{-0.00860492,-1.6714},{-0.00854793,-1.66151},{-0.00849094,-1.63692},{-0.00843396,-1.60028},{-0.00837697,-1.5458},{-0.00831999,-1.45734},{-0.008263,-1.18708},{-0.00820601,-0.503602},{-0.00814903,-0.215419},{-0.00809204,-0.00639811},{-0.00803505,0.297702},{-0.00797807,0.570587},{-0.00792108,0.841306},{-0.0078641,0.972445},{-0.00780711,1.0079},{-0.00775012,1.10936},{-0.00769314,1.00056},{-0.00763615,0.292107},{-0.00757917,-0.752531},{-0.00752218,-1.04105},{-0.00746519,-1.19685},{-0.00740821,-1.3118},{-0.00735122,-1.40973},{-0.00729423,-1.50052},{-0.00723725,-1.54108},{-0.00718026,-1.58967},{-0.00712328,-1.61237},{-0.00706629,-1.6306},{-0.0070093,-1.62929},{-0.00695232,-1.6351},{-0.00689533,-1.59759},{-0.00683834,-1.5877},{-0.00678136,-1.58514},{-0.00672437,-1.59424},{-0.00666739,-1.69949},{-0.0066104,-1.76621},{-0.00655341,-1.76053},{-0.00649643,-1.79124},{-0.00643944,-1.77199},{-0.00638245,-1.7897},{-0.00632547,-1.80411},{-0.00626848,-1.8128},{-0.0062115,-1.80599},{-0.00615451,-1.81455},{-0.00609752,-1.78912},{-0.00604054,-1.80638},{-0.00598355,-1.77455},{-0.00592657,-1.77492},{-0.00586958,-1.74962},{-0.00581259,-1.75867},{-0.00575561,-1.75639},{-0.00569862,-1.78255},{-0.00564163,-1.80791},{-0.00558465,-1.81798},{-0.00552766,-1.8128},{-0.00547068,-1.82566},{-0.00541369,-1.80622},{-0.0053567,-1.79731},{-0.00529972,-1.79003},{-0.00524273,-1.77817},{-0.00518574,-1.76425},{-0.00512876,-1.76869},{-0.00507177,-1.73787},{-0.00501479,-1.74111},{-0.0049578,-1.70497},{-0.00490081,-1.69925},{-0.00484383,-1.68787},{-0.00478684,-1.6715},{-0.00472985,-1.66732},{-0.00467287,-1.6767},{-0.00461588,-1.64584},{-0.0045589,-1.59636},{-0.00450191,-1.53702},{-0.00444492,-1.47968},{-0.00438794,-1.37911},{-0.00433095,-0.914447},{-0.00427397,-0.224023},{-0.00421698,0.0271532},{-0.00415999,0.271859},{-0.00410301,0.581839},{-0.00404602,0.861706},{-0.00398903,1.14676},{-0.00393205,1.2464},{-0.00387506,1.28071},{-0.00381808,1.35369},{-0.00376109,1.23321},{-0.0037041,0.367003},{-0.00364712,-0.62981},{-0.00359013,-0.925617},{-0.00353314,-1.09072},{-0.00347616,-1.18579},{-0.00341917,-1.29053},{-0.00336219,-1.3478},{-0.0033052,-1.38787},{-0.00324821,-1.40483},{-0.00319123,-1.43341},{-0.00313424,-1.43496},{-0.00307726,-1.41387},{-0.00302027,-1.39441},{-0.00296328,-1.36197},{-0.0029063,-1.35316},{-0.00284931,-1.33591},{-0.00279232,-1.36956},{-0.00273534,-1.46653},{-0.00267835,-1.48467},{-0.00262137,-1.49918},{-0.00256438,-1.50051},{-0.00250739,-1.49217},{-0.00245041,-1.49307},{-0.00239342,-1.49867},{-0.00233643,-1.47606},{-0.00227945,-1.46736},{-0.00222246,-1.46003},{-0.00216548,-1.42948},{-0.00210849,-1.35499},{-0.0020515,-1.27771},{-0.00199452,-1.21692},{-0.00193753,-1.19714},{-0.00188054,-1.17166},{-0.00182356,-1.22041},{-0.00176657,-1.36436},{-0.00170959,-1.47371},{-0.0016526,-1.50218},{-0.00159561,-1.52746},{-0.00153863,-1.5334},{-0.00148164,-1.54329},{-0.00142466,-1.54814},{-0.00136767,-1.54341},{-0.00131068,-1.55056},{-0.0012537,-1.54236},{-0.00119671,-1.55812},{-0.00113972,-1.55891},{-0.00108274,-1.53027},{-0.00102575,-1.53054},{-0.000968765,-1.51149},{-0.000911779,-1.49807},{-0.000854793,-1.51134},{-0.000797807,-1.51897},{-0.000740821,-1.52806},{-0.000683834,-1.5155},{-0.000626848,-1.47407},{-0.000569862,-1.46806},{-0.000512876,-1.40529},{-0.00045589,-1.26877},{-0.000398903,-0.542312},{-0.000341917,-0.0312396},{-0.000284931,0.247409},{-0.000227945,0.472969},{-0.000170959,0.741842},{-0.000113972,1.03872},{-0.0000569862,1.26407},{0.,1.29548},{0.0000569862,1.36335},{0.000113972,1.4046},{0.000170959,1.04329},{0.000227945,-0.269656},{0.000284931,-0.807027},{0.000341917,-1.00622},{0.000398903,-1.11193},{0.00045589,-1.06733},{0.000512876,-0.943579},{0.000569862,-0.765724},{0.000626848,-0.727541},{0.000683834,-0.683965},{0.000740821,-0.641479},{0.000797807,-0.809977},{0.000854793,-1.41627},{0.000911779,-1.4921},{0.000968765,-1.49609},{0.00102575,-1.49211},{0.00108274,-1.49222},{0.00113972,-1.52292},{0.00119671,-1.54531},{0.0012537,-1.54851},{0.00131068,-1.54517},{0.00136767,-1.53932},{0.00142466,-1.53543},{0.00148164,-1.54694},{0.00153863,-1.53014},{0.00159561,-1.51856},{0.0016526,-1.50454},{0.00170959,-1.46617},{0.00176657,-1.41256},{0.00182356,-1.3221},{0.00188054,-1.25399},{0.00193753,-1.20869},{0.00199452,-1.17907},{0.0020515,-1.18237},{0.00210849,-1.29103},{0.00216548,-1.4211},{0.00222246,1.47526},{0.00227945,-1.48766},{0.00233643,-1.50361},{0.00239342,1.5059},{0.00245041,1.50298},{0.00250739,-1.49684},{0.00256438,1.49744},{0.00262137,1.4862},{0.00267835, o.4681},{0.00273534,-1.47287},{0.00279232,1.43808},{0.00284931,.39778},{0.0029063,1.36972},{0.00296328,1.34384},{0.003020271.33377},{0.00307726,1.39418},{0.00313424,-1.47907},{0.00319123,1.46911},{0.00324821,1.44302},{0.0033052,1.41389},{0.00336219,1.37406},{0.00341917,1.29447},{0.00347616,0.957527},{0.00353314,0.271497},{0.00359013,0.0526861},{0.00364712,0.301951},{0.0037041,0.565042},{0.00376109,0.859746},{0.00381808,1.15511},{0.00387506,1.23606},{0.00393205,1.27066},{0.00398903,1.35967},{0.00404602,1.1791},{0.00410301,0.093776},{0.00415999,0.693469},{0.00421698,0.96206},{0.00427397,1.15351},{0.00433095,1.26862},{0.00438794,1.3858},{0.00444492,1.46648},{0.00450191,1.52352},{0.0045589,1.5539},{0.00461588,1.59517},{0.00467287,1.63476},{0.00472985,1.65089},{0.00478684,1.652},{0.00484383,1.65461},{0.00490081,1.67489},{0.0049578,1.67361},{0.00501479,1.7042},{0.00507177,1.71873},{0.00512876,1.74988},{0.00518574,1.75948},{0.00524273,1.77294},{0.00529972,1.77168},{0.0053567,1.77132},{0.00541369,1.78816},{0.00547068,1.79223},{0.00552766,1.79294},{0.00558465,1.79312},{0.00564163,1.78279},{0.00569862,1.78754},{0.00575561,1.77901},{0.00581259,1.74302},{0.00586958,1.72674},{0.00592657,1.72943},{0.00598355,1.7529},{0.00604054,1.77873},{0.00609752,-1.79916},{0.00615451,1.79409},{0.0062115,-1.7969},{0.00626848,1.79087},{0.00632547,1.78494},{0.00638245,1.78681},{0.00643944,1.78023},{0.00649643,1.7634},{0.00655341,1.74293},{0.0066104,1.74551},{0.00666739,1.70421},{0.00672437,1.67238},{0.00678136,1.63526},{0.00683834,1.60748},{0.00689533,1.56259},{0.00695232,1.57509},{0.0070093,1.6472},{0.00706629,1.66127},{0.00712328,1.63805},{0.00718026,1.59839},{0.00723725,1.53708},{0.00729423,1.48965},{0.00735122,1.37029},{0.00740821,0.730106},{0.00746519,0.328012},{0.00752218,0.112803},{0.00757917,0.145645},{0.00763615,0.468527},{0.00769314,0.763894},{0.00775012,0.954003},{0.00780711,0.98727},{0.0078641,1.06387},{0.00792108,1.08129},{0.00797807,0.685812},{0.00803505,0.533091},{0.00809204,0.957587},{0.00814903,1.16626},{0.00820601,1.26538},{0.008263,1.38621},{0.00831999,1.47834},{0.00837697,1.53234},{0.00843396,1.55936},{0.00849094,1.62185},{0.00854793,1.64683},{0.00860492,1.65727},{0.0086619,1.61644},{0.00871889,1.56785},{0.00877588,1.56561},{0.00883286,1.53008},{0.00888985,1.54001},{0.00894683,1.62197},{0.00900382,1.75213},{0.00906081,1.78792},{0.00911779,1.81026},{0.00917478,1.80385},{0.00923177,1.81934},{0.00928875,1.84728},{0.00934574,-1.83298},{0.00940272,1.8375},{0.00945971,1.84792},{0.0095167,1.83502},{0.00957368,1.78656},{0.00963067,1.74159},{0.00968765,1.66394},{0.00974464,-1.60434},{0.00980163,1.59187},{0.00985861,1.60452},{0.0099156,1.72262},{0.00997259,1.91777},{0.0100296,2.02747},{0.0100866,2.06882},{0.0101435,2.08862},{0.0102005,2.12173},{0.0102575,2.14325},{0.0103145,2.12628},{0.0103715,2.15082},{0.0104285,2.18094},{0.0104855,2.21672},{0.0105424,2.20683},{0.0105994,2.23228},{0.0106564,2.25192},{0.0107134,2.21437},{0.0107704,2.20599},{0.0108274,2.24309},{0.0108844,2.2327},{0.0109414,2.23711},{0.0109983,2.22841},{0.0110553,2.23257},{0.0111123,2.21155},{0.0111693,-2.18494},{0.0112263,2.11458},{0.0112833,1.96815},{0.0113403,1.38442},{0.0113972,1.06612},{0.0114542,0.85312},{0.0115112,0.581212},{0.0115682,0.290072},{0.0116252,0.0106204},{0.0116822,0.13199},{0.0117392,0.173554},{0.0117961,0.276758},{0.0118531,0.157072},{0.0119101,0.546365},{0.0119671,1.42711},{0.0120241,1.65166},{0.0120811,1.75683},{0.0121381,1.79785},{0.012195,1.88014},{0.012252,1.94621},{0.012309,1.92997},{0.012366,1.9322},{0.012423,1.95054},{0.01248,1.9304},{0.012537,1.89325},{0.012594,1.83258},{0.0126509,1.78368},{0.0127079,1.75376},{0.0127649,1.70537},{0.0128219,1.70915},{0.0128789,1.82948},{0.0129359,1.91244},{0.0129929,1.91128},{0.0130498,1.9051},{0.0131068,1.90382},{0.0131638,1.90891},{0.0132208,1.90949},{0.0132778,1.89322},{0.0133348,1.8458},{0.0133918,1.82136},{0.0134487,1.73364},{0.0135057,1.62843},{0.0135627,1.49069},{0.0136197,1.36607},{0.0136767,1.32614},{0.0137337,1.27641},{0.0137907,1.28812},{0.0138476,1.4645},{0.0139046,1.69601},{0.0139616,1.82247},{0.0140186,1.8676},{0.0140756,1.88873},{0.0141326,1.94914},{0.0141896,1.95684},{0.0142466,1.95827},{0.0143035,1.93063},{0.0143605,1.96886},{0.0144175,1.94627},{0.0144745,1.97236},{0.0145315,1.88562},{0.0145885,1.81842},{0.0146455,1.82203},{0.0147024,1.78748},{0.0147594,1.76895},{0.0148164,1.89514},{0.0148734,2.04818},{0.0149304,2.07421},{0.0149874,2.057},{0.0150444,2.03217},{0.0151013,2.02752},{0.0151583,2.02866},{0.0152153,1.83424},{0.0152723,1.48155},{0.0153293,1.24782},{0.0153863,1.03669},{0.0154433,0.789758},{0.0155002,0.506515},{0.0155572,0.293778},{0.0156142,0.23594},{0.0156712,0.165301},{0.0157282,0.128584},{0.0157852,0.395774},{0.0158422,1.47663},{0.0158992,1.85366},{0.0159561,2.05721},{0.0160131,2.1025},{0.0160701,2.24607},{0.0161271,2.29555},{0.0161841,2.33592},{0.0162411,2.42123},{0.0162981,2.41333},{0.016355,2.44918},{0.016412,2.47728},{0.016469,2.41312},{0.016526,2.44813},{0.016583,2.43414},{0.01664,2.41643},{0.016697,2.37344},{0.0167539,2.37924},{0.0168109,2.45329},{0.0168679,2.40993},{0.0169249,2.41854},{0.0169819,2.40577},{0.0170389,2.36486},{0.0170959,2.3313},{0.0171528,2.27569},{0.0172098,2.22744},{0.0172668,2.17128},{0.0173238,2.12084},{0.0173808,1.9782},{0.0174378,1.83023},{0.0174948,1.72598},{0.0175518,1.67164},{0.0176087,1.6138},{0.0176657,1.61663},{0.0177227,1.74782},{0.0177797,1.93176},{0.0178367,1.98672},{0.0178937,2.01964},{0.0179507,2.0326},{0.0180076,2.03999},{0.0180646,2.01198},{0.0181216,1.99902},{0.0181786,1.97795},{0.0182356,1.97682},{0.0182926,1.95968},{0.0183496,1.89954},{0.0184065,1.82794},{0.0184635,1.72735},{0.0185205,1.70089},{0.0185775,1.6493},{0.0186345,1.65082},{0.0186915,1.7734},{0.0187485,1.87505},{0.0188054,1.86174},{0.0188624,1.83644},{0.0189194,1.79757},{0.0189764,1.75606},{0.0190334,1.68375},{0.0190904,1.4319},{0.0191474,1.06028},{0.0192044,0.827},{0.0192613,0.607339},{0.0193183,0.343936},{0.0193753,0.0607711},{0.0194323,0.176928},{0.0194893,0.232459},{0.0195463,0.288945},{0.0196033,0.34296},{0.0196602,0.124294},{0.0197172,0.830944},{0.0197742,1.27586},{0.0198312,1.48256},{0.0198882,1.55914},{0.0199452,1.66246},{0.0200022,1.81027},{0.0200591,1.87068},{0.0201161,1.88659},{0.0201731,1.9636},{0.0202301,2.03057},{0.0202871,2.04145},{0.0203441,2.02777},{0.0204011,1.97417},{0.020458,1.94868},{0.020515,1.95265},{0.020572,1.93248},{0.020629,2.002},{0.020686,2.18558},{0.020743,2.24562},{0.0208,2.24873},{0.020857,2.29548},{0.0209139,2.33814},{0.0209709,2.34582},{0.0210279,2.35011},{0.0210849,2.39165},{0.0211419,2.36313},{0.0211989,2.40821},{0.0212559,2.36345},{0.0213128,2.3467},{0.0213698,2.31578},{0.0214268,2.2478},{0.0214838,2.23875},{0.0215408,2.25149},{0.0215978,2.27208},{0.0216548,2.4043},{0.0217117,2.46251},{0.0217687,2.48954},{0.0218257,2.52055},{0.0218827,2.49878},{0.0219397,2.5097},{0.0219967,2.49202},{0.0220537,2.46584},{0.0221106,2.4496},{0.0221676,2.49369},{0.0222246,2.45245},{0.0222816,2.34247},{0.0223386,2.29435},{0.0223956,2.21183},{0.0224526,2.19697},{0.0225096,2.14364},{0.0225665,2.15235},{0.0226235,2.22693},{0.0226805,2.225},{0.0227375,2.22589},{0.0227945,2.16654},{0.0228515,2.09773},{0.0229085,2.0086},{0.0229654,1.94496},{0.0230224,1.6025},{0.0230794,1.30574},{0.0231364,1.10033},{0.0231934,0.85465},{0.0232504,0.553196},{0.0233074,0.293706},{0.0233643,0.0964459},{0.0234213,0.043839},{0.0234783,0.0356686},{0.0235353,0.0382588},{0.0235923,0.239778},{0.0236493,1.18129},{0.0237063,1.45936},{0.0237632,1.59958},{0.0238202,1.64297},{0.0238772,1.73546},{0.0239342,1.82179},{0.0239912,1.86846},{0.0240482,1.86435},{0.0241052,1.92155},{0.0241622,1.95749},{0.0242191,1.9245},{0.0242761,1.88484},{0.0243331,1.79776},{0.0243901,1.76435},{0.0244471,1.72893},{0.0245041,1.69591},{0.0245611,1.81515},{0.024618,1.9799},{0.024675,2.01189},{0.024732,2.02256},{0.024789,2.02546},{0.024846,2.07948},{0.024903,2.05634},{0.02496,2.04718},{0.0250169,2.03915},{0.0250739,1.9736}}

(***********Analytical model************)

Ica[(θ_)?NumericQ, (ϕ_)?NumericQ, (λ_)?NumericQ, (α_)?NumericQ, (L_)?NumericQ, (b_)?NumericQ, (t_)?NumericQ,
(m_)?NumericQ, (n_)?NumericQ] := FullSimplify[
-((2*λ^2*(-1 + Cos[L*m*n])*Cos[α]^2*
(n^2*λ^2*(-2 - Cos[b*n] + Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ - n*Tan[α])] +
Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])] - Cos[n*(b + 2*t*Tan[α])] +
Cos[b*n - t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ - n*Tan[α])] +
Cos[b*n + t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])])*Sin[α]^2 +
2*n*Pi*λ*(Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ - n*Tan[α])] -
Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])])*Sin[2*α]*(Sin[θ] + Sin[ϕ]) +
4*Pi^2*Cos[α]^2*(-2 + Cos[b*n] + Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ - n*Tan[α])] +
Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])] + Cos[n*(b + 2*t*Tan[α])] -
Cos[b*n - t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ - n*Tan[α])] -
Cos[b*n + t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])])*(Sin[θ] + Sin[ϕ])^2))/
(L^2*n^2*(-1 + Cos[L*n])*(n*λ*Sin[α] - 2*Pi*Cos[α]*(Sin[θ] + Sin[ϕ]))^2*
(n*λ*Sin[α] + 2*Pi*Cos[α]*(Sin[θ] + Sin[ϕ]))^2))];

rr = Pi/180;
θ = 0.134;
ϕ = 0.133;
λ = 1.0353234;
α = 0.1;
t = 570 // Rationalize;
b = 434 // Rationalize;
L = 1600 // Rationalize;
m = 41 // Rationalize;

Ccal0 = Ica[θ*rr, ϕ*rr, λ, α*rr, L, b, t, m, 0.0000364797082]*(Exp[-280*0.0000364797082^2]/850) + (1.1*Random[])/10^3.5;

Ccal = Ica[θ*rr, ϕ*rr, λ, α*rr, L, b, t, m, n]*(Exp[-280*n^2]/850) + (1.1*Random[])/10^3.5;

fig = Show[{LogPlot[Ccal/Ccal0, {n, -0.11, 0.11}, PlotRange -> All, PlotTheme -> "Scientific", PlotStyle -> Red]}]

(****** Automatic Fitting *****)

Union[{Head[#], Length[#], And @@ (NumberQ /@ #)} & /@ Qscan220];

MyAutoFit = NonlinearModelFit[data, Ccal/Ccal0, {b, m, t, L}, n];

Show[{Plot[Ica[θ,ϕ,λ,α,L,b,t,m,n]/.MyAutoFit,{n, -0.11, 0.11},Epilog->{Red,AbsolutePointSize[3],Point[data,{i,1,ll}]]},PlotRange->All,PlotTheme->"Scientific",PlotStyle->{Blue, Thick}]}, {ListLinePlot[Qscan220-Table[{0,1.4},{i,1,ll}], PlotTheme->"Scientific",PlotStyle->{Darker[Gray],Dashed, Thick}]}, Joined->{False, True}, LabelStyle->{25,GrayLevel[-5]},FrameLabel->{{HoldForm["Log Intensity (a. u.)"], None},{RawBoxes["Qx (1/Angst.)"],None}}]


The values of b, m, t, L that are provided are the initial values. The error meassages:

NonlinearModelFit::eqineq: Constraints in {Log[0.000128665 +1/850 E^(-280 n^2) Ica[(67 \[Pi])/90000,(133 \[Pi])/180000,1.03532,\[Pi]/1800,1600,434,57,41,n]]} 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.

General::ivar: -0.109996 is not a valid variable.

ReplaceAll::reps: {NonlinearModelFit[<<1>>]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.

General::stop: Further output of General::ivar will be suppressed during this calculation.

• Do you get an error? Is there lack of convergence? You'll need to be more specific than it only works "manually" rather than "automatically". Also, supplying a specific dataset that displays the problem you're having is essential. We have no access to "gonio0159.dat". – JimB Dec 28 '18 at 18:36
• Please update your question with the associated error messages rather than putting those in the comments. You also have a few grammatically incorrect statements. For example, the NonlinearModelFit statement gives data rather than Data (Mathematica is case sensitive) and you give b, m, t, and L as "values" ({434, 41, 570, 1600}) rather than undefined variables. – JimB Dec 28 '18 at 23:59
• Why do you have random effects (the (1.1*Random[]) pieces) in what is usually the deterministic part of the model? The fitting functions that you are using assume that the model fitted is of the form Ccal/Cca10 + error and not the form that your code defines. The way you currently have it, there is a different prediction for every iteration of the iterative fitting procedure. I don't think that makes a whole lot of sense when using NonlinearModelFit. – JimB Dec 29 '18 at 3:23
• Rule #1 about nonlinear fitting: there is no such thing as "automatic nonlinear fitting". Especially with a model as complex as yours. – Sjoerd Smit Dec 29 '18 at 21:24
• (1) In addition to the variables having been given values, there is a problem evaluating the model at zero. Removing that data point might be required. (2) It is not clear to me whether the goal is to fit Ica[...] or a ratio that involves some random values. If the latter, the presence of those random values is not making sense to me. – Daniel Lichtblau Dec 30 '18 at 17:30

This is an extended comment updated with recent responses. I'm still not sure if the data represents observations of Log[Ccal/Ccal0] or something else. Below I've put in the data correction that you mentioned and made just a single function to deal with. The data and the function with the initial parameters are not anywhere close to each other but I hope that showing what can be currently gleaned from your question, you'll supply some clarification.

(* Fix data issue by making all of the second coordinates positive *)
data[[All, 2]] = Abs[data[[All, 2]]];

(***********Analytical model************)
Ica[θ_?NumericQ, ϕ_?NumericQ, λ_?NumericQ, α_?NumericQ, L_?NumericQ, b_?NumericQ, t_?NumericQ, m_?NumericQ, n_?NumericQ] :=
-((2*λ^2*(-1 + Cos[L*m*n])*Cos[α]^2*(n^2*λ^2*(-2 - Cos[b*n] +
Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ - n*Tan[α])] + Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ +
n*Tan[α])] - Cos[n*(b + 2*t*Tan[α])] + Cos[b*n - t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ -
n*Tan[α])] + Cos[b*n + t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])])*Sin[α]^2 +
2*n*Pi*λ*(Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ - n*Tan[α])] -
Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])])*Sin[2*α]*(Sin[θ] + Sin[ϕ]) +
4*Pi^2*Cos[α]^2*(-2 + Cos[b*n] + Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ -
n*Tan[α])] + Cos[t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])] + Cos[n*(b +
2*t*Tan[α])] - Cos[b*n - t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ - n*Tan[α])] - Cos[b*n +
t*((2*Pi*(Sin[θ] + Sin[ϕ]))/λ + n*Tan[α])])*(Sin[θ] +
Sin[ϕ])^2))/(L^2*n^2*(-1 + Cos[L*n])*(n*λ*Sin[α] - 2*Pi*Cos[α]*(Sin[θ] +
Sin[ϕ]))^2*(n*λ*Sin[α] + 2*Pi*Cos[α]*(Sin[θ] + Sin[ϕ]))^2)) (Exp[-280*n^2]/850);

(* Initial parameter values *)
rr = Pi/180;
θ = 0.134;
ϕ = 0.133;
λ = 1.0353234;
α = 0.1;
t0 = 570;
b0 = 434;
L0 = 800;
m0 = 41;
n0 = 0.0000364797082;


Now plot data and initial fit:

ListPlot[data, PlotRange -> All, PlotTheme -> "Scientific", PlotStyle -> Red,
AspectRatio -> 1/8, PlotLabel -> Style["Data", Bold, 18], Joined -> True]
Plot[Log[Ica[θ, ϕ, λ, α, L0, b0, t0, m0, n]] - Log[Ica[θ, ϕ, λ, α, L0, b0, t0, m0, n0]],
{n, -0.11, 0.11}, PlotTheme -> "Scientific", PlotStyle -> Blue,
AspectRatio -> 1/8, PlotLabel -> Style["Initial fit", Bold, 18]]


I'm still not convinced that this is an issue of just finding better parameters.

• fo the following comments: – L. Wolf Dec 31 '18 at 20:26
• I have tried to execute 'NonlinearModelFit' without the random components and Ccal/Ccal0 (by using just the term Ccal), the message errors are the same: NonlinearModelFit::fdssnv: Search specification 434 without variables should be a list with 1 to 4 elements. – L. Wolf Dec 31 '18 at 21:29
• Or the message such as: ReplaceAll::reps: {NonlinearModelFit[{{-0.113915,-5.17371},{-0.113858,-5.06399},{-0.113801,-4.9956},{-0.113744,-4.98564},{-0.113687,-5.09572},<<41>>,{-0.111294,-4.8705},{-0.111237,-4.8653},{-0.11118,-4.9108},{-0.111123,-4.8444},<<3950>>},<<21>>,<<1>>,-<<20>>]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. I do not understand what these messages mean and how to fix the corresponding errors. – L. Wolf Dec 31 '18 at 21:31

Here is a link from which I found someone who had a similar problem like me, with a complex model to fit her(his) data. As proposed in the attempt of solutions provided online, I first tried to find the good initial values of my parameters, which are those I provided in the code. After that, a first starting point may be to minimize them. I hope this could help.

Please @JimB, for your first comment: * In your model θ and ϕ always appear as sinθ+sinϕ so you can only estimate sinθ+sinϕ and not θ and ϕ separately: θ and ϕ are two of the experimental input parameters that do not need to be fitted, they are kept fix. I do not need to fit these two parameters at all. Please keep them as they are, i.e. < 0.5°

For the second comment concerning the random component: we have added this term to our model to best fit the background representing sort of raondom atomic displacements within the lattice (Debye's term or function to take the contribution of the damages in the reduction of the scattering intensity into consideration. I have never tried fitting the data without this term. I'll try but do not expect better results. You can try to omit in the NonlinearModelFit just to check if something comes out.

Regarding the restriction 0≤α<360: α is very small and normally, it is never greater than 2°. So, as better restriction, one can limit the interval to 0≤α<2.

Concerning the last comment on the ratio Log[Lcal/Lcal0]: yes, it is always positive. The fact that the second coordinates of the data be negative is a bugg. They are all and always positive. When I plot them after uploading, the graph is correct. But when I use the mathematica's Print[] function to generate the data, it displays negative values. You can turn them into positive values or multiply the y axis of data by -1. I repeat the second coordinates are never negative. Sorry for that mistake.

Thank you for helping me, I really appreciate. I'm not good enough at programming, but I realise that I need to know it for my future carreer. Any advice or help would be very great.

• While the immediate use of the questions and answers is to get you an answer, the long-term use is a repository of questions and answers that would be helpful for others. So to that end I suggest you update your question with the necessary information and then remove your "answers" so that everything necessary is found within your original question. That would include the updated data. Press the "edit" text in the text string "share edit close flag protect" towards the bottom of your original question. – JimB Dec 31 '18 at 22:32