If I make a batch fitting routine, something like:

FitResultsData =     
             SpectrumData = Import[SpectrumList[[i]]]; 

             SpectrumFit = NonlinearModelFit[SpectrumData, Model, {a, b, c}, x];
                 aFitOut = a /. SpectrumFit["BestFitParameters"];
                 bFitOut = b /. SpectrumFit["BestFitParameters"];   
                 cFitOut = c /. SpectrumFit["BestFitParameters"];

             {i, aFitOut , bFitOut, cFitOut},
             {i, 1, Length[SpectrumList]}       

and a fit fails completely, e.g. I get a Power::infy: Infinite expression 1/0.^2 encountered. error or something, I find that when it comes to going on to use FitResultsData after all Table[..] has finished fitting and executing no matter which row I select for example FitResultsData[[1]] the error Power::infy: Infinite expression 1/0.^2 encountered. will be returned. This happens even say the original source of the error was in spectrum i = 99.

Is there a method of escaping such errors, such that even though one spectrum fit might be bad, it doesn't stop be accessing the 99% successful


1 Answer 1


I tried to reproduce your situation in a simpler contrived example:

results =
   {i, a /. NonlinearModelFit[
              {1}, If[i == 2, 1/0, 1], {a}, x
   {i, 3}

As you can see, the code above triggers the Power::infy error on purpose for one value of the parameter ($i=2$), but works fine for the other two.

It is true that retrieving one of the successful results still triggers errors and warnings from the leftover failed one, but nevertheless the results from the successful runs are there and can be accessed:

showing errors as well as correct results being retrieved

If the errors bother you, you can turn them off using Quiet when accessing results:

showing that Quiet prevents the errors from showing up

  • $\begingroup$ Thanks for replying. Yes, I too had a problem creating an example, which replicates my problem, in a way that is easy for anyone to produce. But quiet worked perfectly!!! $\endgroup$
    – user27119
    Jan 9, 2020 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.