0
$\begingroup$

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}}];



  ResMetVecpRun // TraditionalForm
 ];

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]][[1]][[10]][[2]], {i, 1, maxSampleRuns}]];

Any recommendations? Advices are very wellcome. thanks

$\endgroup$
  • $\begingroup$ Welcome! Cold you elaborate on your problem? At the moment the question is unclear. Adding sample and input / output is usually helpful, too. $\endgroup$ – Yves Klett Nov 21 '14 at 13:04
  • $\begingroup$ @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 $\endgroup$ – Walter Schrabmair Nov 21 '14 at 13:38
  • $\begingroup$ 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? $\endgroup$ – Walter Schrabmair Nov 21 '14 at 13:47
  • $\begingroup$ Please update the question with that info, which will help getting better answers. $\endgroup$ – Yves Klett Nov 21 '14 at 15:25
2
$\begingroup$

Use the Check[] function to handle exceptions that throw messages. In detail, however, I would say that the problem is not well defined.

$\endgroup$
  • $\begingroup$ Yes as it is still in progress. I would be happy when I have some code to ask here once more, if there is still something unclear. Thanks But thanks for that info. CHeck could be useful. $\endgroup$ – Walter Schrabmair Nov 21 '14 at 14:11
  • $\begingroup$ 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[]. $\endgroup$ – mikuszefski Nov 21 '14 at 15:15
  • $\begingroup$ well as the data for the model is random, I think you cant avoid the CHECK, as it is done for Monte Carlo. There can be bad conditions so there is no good fit at all. $\endgroup$ – Walter Schrabmair Nov 21 '14 at 16:08
  • $\begingroup$ @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[]? $\endgroup$ – mikuszefski Nov 22 '14 at 20:07
  • $\begingroup$ Thanks for that info, Yes I had to spend 2 hrs to find that it is better to use TraditionalForm later. I was a little bit confused with the different Forms. Well the fitting is ok, as even for large iterations there will be no fit at all as the randomnumbers for the model input data will not be a valid sample set. Tho handle this I have used Check[] and write a ERRORFLAG in the matrix. After the total MC runs are done, I will delete the MC run with the Errorflag true. Thanks anyway for your info. $\endgroup$ – Walter Schrabmair Nov 23 '14 at 0:05

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.