# Handling msg for fitting

I have a trivial question: I do Monte Carlo with some curve fitting. Now always when a random run creates a complex fitting solution it should write 1 in a Matrix. How can I handle such msgs in a module? Thanks Walter

ADD: Well, this is still in progress. But when I make a fit and the fitting is not real then I get a red Warning messsage. How Can I handle this msgs so that, when I get such a non proper fit (as of the random input data for the model) I would write to a list a 1. Finally after 1000 RUns I can count this 1s and get the info how many non-proper fits according due the random input data for the model I have got. Hope I could express what I am meaning. Thanks –

Add2: It would be a idea if for the findfit fn, I could ask for the number of interations. If interations = MXINTERATIONS (f.e. 1000) then I see that it could not convergate. So I could abort the findfit and write 1 in the list. Could be this a solution? –

Well I have done this in that way:

calcMethod[randFN_, randfacdef_, samplerun_] :=
Module[{limit, vmaxmod, kmmod, yy0, xx, yy, data, datalb, dataeh,
datahw, minxlb, maxxlb, minylb, maxylb, minx, miny, maxx, maxy,
minxeh, minyeh, maxxeh, maxyeh, minxhw, maxxhw, minyhw, maxyhw,
vmaxnl, kmnl, vmaxlb, kmlb, vmaxeh, kmeh, vmaxhw, kmhw, mmenten,
eadieh, hanesw, mmentenmax, fit, rand = randFN,
randfac = randfacdef, errflagnl},

vmaxmod = 20.0; kmmod = 1.0;
(* Print["Ideal model:   vmax = ",vmaxmod,",     km = ",kmmod]; *)

limit = 3.*kmmod;
xx = {limit, limit/2, limit/4, limit/8, limit/16, limit/32};
mmentenmax = 32/limit;
(*  rand=RandomReal[{-1.,2.},6]; *)
(* rand=randFN; *)
yy0 = vmaxmod*xx/(kmmod + xx);
yy = vmaxmod*xx/(kmmod + xx) + rand*randfac;

data = {xx, yy}\[Transpose]; minx = Min[xx]; maxx = Max[xx];
miny = Min[yy]; maxy = Max[yy];
datalb = {1/xx, 1/yy}\[Transpose]; minxlb = Min[1/xx];
maxxlb = Max[1/xx]; minylb = Min[1/yy]; maxylb = Max[1/yy];
dataeh = {yy/xx, yy}\[Transpose]; minxeh = Min[yy/xx];
maxxeh = Max[yy/xx]; minyeh = Min[yy]; maxyeh = Max[yy];
datahw = {xx, xx/yy}\[Transpose]; minxhw = Min[xx];
maxxhw = Max[xx]; minyhw = Min[xx/yy]; maxyhw = Max[xx/yy];
(* Print["yy data: ",yy]; *)
fit = Check[
FindFit[data, {u*x/(v + x) (*,{0<v<2*kmmod} *) }, {u, v}, x], 1,
FindFit::cvmit];
(* fit=FindFit[data,{u*x/(v+x),{0<v<2*kmmod}},{u,v},x]; *)
If[ (ToString[fit] != "1") , errflagnl = 0 , errflagnl = 1];
vmaxnl = u /. fit;
kmnl = v /. fit;

(*Print["Nonlinear fit: vmax = ",vmaxnl,", km = ",kmnl];*)

ResMetVecpRun =
Transpose[{{"Methodname", "Vmax Model", "km Model",
"vmax Methode", "km Methode", "minimum x", "maximum x",
"minimum y", "maximum y",
"ERROR-FLAG"}, {"Method 1 (Run:" <> ToString[samplerun] <> ")",
vmaxmod, kmmod, vmaxnl, kmnl, minx, maxx, miny, maxy,
errflagnl}}];

];


TO call:

ResMetVec =
Table[calcMethod[RandomReal[{-1., 1.}, 6], 5., i ], {i, 1,
maxSampleRuns}];


and the SUMs:

ResMetVec // MatrixForm
Print["Number of non-convergent FITs (Methode 1):",
Sum[ResMetVec[[i]][][][], {i, 1, maxSampleRuns}]];


Any recommendations? Advices are very wellcome. thanks

• Welcome! Cold you elaborate on your problem? At the moment the question is unclear. Adding sample and input / output is usually helpful, too. Nov 21 '14 at 13:04
• @YvesKlett Well, this is still in progress. But when I make a fit and the fitting is not real then I get a red Warning messsage. How Can I handle this msgs so that, when I get such a non proper fit (as of the random input data for the model) I would write to a list a 1. Finally after 1000 RUns I can count this 1s and get the info how many non-proper fits according due the random input data for the model I have got. Hope I could express what I am meaning. Thanks Nov 21 '14 at 13:38
• It would be a idea if for the findfit fn, I could ask for the number of interations. If interations = MXINTERATIONS (f.e. 1000) then I see that it could not convergate. So I could abort the findfit and write 1 in the list. Could be this a solution? Nov 21 '14 at 13:47
• Please update the question with that info, which will help getting better answers. Nov 21 '14 at 15:25

Use the Check[] function to handle exceptions that throw messages. In detail, however, I would say that the problem is not well defined.
• Surely you can update your question. Actually you should, as the question is not very clear. As Yves Klett pointed out, you should present some code or concepts in a simplified version. The idea would be to avoid the messages in the first place, hence, not needing Check[]. Nov 21 '14 at 15:15
• @walter-schrabmair First of all it seems to me that the code you show, contains plenty of stuff beyond a minimum working example. Anyway, here some comments. You should use Return[] to return your result. Use TraditionalForm in displaying later but not on the return value; that makes it easier to work with the output. Finally, it seems that you have an error message due to max iterations. What about increasing the MaxIteration of FindFit[]? Nov 22 '14 at 20:07