1
$\begingroup$

Update:

The data has been made available as a text file.

It is desired to use the Weights option with NonlinearModelFit and the direct coding gives the error message "Wolfram Kernel for Windows has stopped working." Below the commands are given the import the data and run a few models. The first two work fine; the last one gives the error message given above.

data = Import["sample_data.txt", "Table"];

(* Create data for NonlinearModelFit *)
dataWeights = data[[All, 3]];
dataSpectrum = data[[All, {1, 2}]];
dataWeightsN = dataWeights/Max[dataWeights];

(* This works *)
nlm1 = NonlinearModelFit[dataSpectrum, a (x/150.0)^(-2) + b (x/150.0)^(-1),
  {{a, 449}, {b, -177}}, x];
nlm1["BestFitParameters"]
(* {a -> 440.0413493307273`,b -> -174.44382871465248`} *)

(* This works:  dataSpectrum is multiplied by 1.0 and the Weights option is added *)
nlm2 = NonlinearModelFit[1.0*dataSpectrum, a (x/150.0)^(-2) + b (x/150.0)^(-1),
  {{a, 449}, {b, -177}}, x, Weights -> dataWeightsN];

nlm2["BestFitParameters"] 
(* {a -> 430.15765125168565`,b -> -156.68960890633196`} *)

(* This doesn't work.  Original data and model with Weights are used *)
(* Error message is "Wolfram Kernel for Windows has stopped working" *)
nlm3 = NonlinearModelFit[dataSpectrum, a (x/150.0)^(-2) + b (x/150.0)^(-1),
 {{a, 449}, {b, -177}}, x, Weights -> dataWeightsN]

End of update

Has anyone experience corruption while importing numbers from a textfile?

The issue I have is that nonlinear model fitting completes only without a weighting function, but the issue goes away if I modify my lists and arrays by multiplying them by 1.0 before feeding them to the NLM function. (or manually cutting and pasting the array values into the code works too, but that is way too manual as I may have to analyze 100 data sets)

Example: I have 25 ordered pairs of numbers I wish to fit using NLM

dataList={{90.5125000, 1001.3813546}, {91.5,977.38053}, {92.56, 966.23423}, and so on}.

I have a weights list of 25 values in a list like

dataWeights={0.85836, 0.577296, 0.2, 0.68656, 1, 0.23434, and so on}

This call doesn't work (meaning all variables turn blue)

   z = NonlinearModelFit[datalist, a (x/150.0)^(-2) + b(x/150.0)^(-1), {a,b}, x, Weights -> dataWeights];

but this one works

z = NonlinearModelFit[datalist, a (x/150.0)^(-2) + b(x/150.0)^(-1), {a,b}, x];

However, if I issue the command:

datalist=1.0*datalist;

both calls work or if I cut and paste, both calls work.

datalist was created by the following Code

data = Import["filename.txt", "Table"];
dataWeights = data[[1 ;;, 3]];
datalist = data[[1 ;;, 1 ;; 2]];

Sorry that I don't provide the data, as the problem goes away if I enter all of the data into this question with a cut and paste.

So the question boils down to Mathematica 11.1 - has anyone had experience with white space issues in text files containing numbers, and if yes, did you figure out what to do with your text file to make sure you didn't get problems in the future.

$\endgroup$
5
  • 2
    $\begingroup$ What do you mean by "this one doesn't work"? Does it give an error, do the values not make sense? Since you haven't included all the data, we cannot possibly reproduce the problem, so you'll need to be explicit about what the problem is. $\endgroup$ – bill s Apr 7 '17 at 22:39
  • 1
    $\begingroup$ end running the issue a bit, but this problem is not a nonlinear fit. (nor was the first one). Have you tried LinearModelFit? In any case 25 vals is not too much to post, just paste the whole thing into the question. $\endgroup$ – george2079 Apr 7 '17 at 23:33
  • $\begingroup$ You should provide guesses for the parameters in the parameter list, in your case, just one, like {{a, guessforthisparameter}}, otherwise NonlinearModelFit starts evaluating expressions around a=1, where your function may be ill conditioned. $\endgroup$ – Vito Vanin Apr 8 '17 at 0:56
  • $\begingroup$ George, there are higher order polynomials that don't work either, but I did not show them. $\endgroup$ – Tom Mozdzen Apr 9 '17 at 23:01
  • $\begingroup$ Short answer is that it was a bug in Mathematica. Went away in 11.3 $\endgroup$ – Tom Mozdzen May 30 '18 at 2:44
3
$\begingroup$

I've taken the weights from your previous post (which had 26 elements) and removed the last data point from above to get 26 elements.

datalist = {{140.1885986328125, 318.54713569102125}, {140.5792236328125, 316.3740245454749},
 {140.9698486328125, 314.1513044515799}, {141.3604736328125, 312.0074230752567},
 {141.7510986328125, 309.84565604848154}, {142.1417236328125, 307.7355334299345},
 {142.5323486328125 , 305.633670557817}, {142.9229736328125, 303.52814971608547},
 {143.3135986328125, 301.4927654142471}, {143.7042236328125, 299.4417704661457},
 {144.0948486328125, 297.4154292210221}, {144.4854736328125, 295.42313663398266},
 {144.8760986328125, 293.40462468351114}, {145.2667236328125, 291.4606242860452},
 {145.6573486328125, 289.4850824087863}, {146.0479736328125, 287.57533642316},
 {146.4385986328125, 285.65551629016227}, {146.8292236328125, 283.75135460154684},
 {147.2198486328125, 281.86934847149666}, {147.6104736328125, 280.03473502558626},
 {148.0010986328125, 278.1649121557317}, {148.3917236328125, 276.35675418061123},
 {148.7823486328125, 274.51855140060024}, {149.1729736328125, 272.7408232115596},
 {149.5635986328125, 270.96588244643533}, {149.9542236328125, 269.1897259023379}};

dataWeights = {0.9987527, 0.99976478704084643, 0.999722848903, 0.9993126, 
       0.9997489, 0.99974733307567956, 0.98364314121504532, 
       0.99878819613270000, 0.99972987911051269, 0.99919462417872860, 
       0.92152614146854338, 0.99960687021314616, 0.99885219400497854, 
       0.99958609168318560, 0.807542606375684133, 0.358750462322291623, 
       0.99949217272776386, 0.99962266189591619, 0.99880149439187476, 
       0.99953788549367709, 0.99907244642256050, 0.99911067891768793, 
       0.87373801598284525, 0.87409790012176219, 0.98225430427248133, 
       0.515316685575128931};

NonlinearModelFit[datalist, a (x/150.0)^(-2), {a}, x] // Normal
(* 6.16036582770958`*^6/x^2 *)

NonlinearModelFit[datalist, a (x/150.0)^(-2), {a}, x, Weights -> dataWeights] // Normal
(* 6.163821795620144`*^6/x^2 *)

Everything seems to work fine. You just need to paste in the data and commands that you're having trouble with. If you've read the data from a file, just typing datalist and pasting in the output should get us the exact data you're using.

$\endgroup$
4
  • $\begingroup$ Pasting would be a pain. But the simpler work around is easier. This works: datalist=1.0*datalist as well as datalist=Round[datalist, 0.000001] $\endgroup$ – Tom Mozdzen Apr 10 '17 at 17:32
  • 1
    $\begingroup$ A pain? I just copied and pasted the data from the Mathematica notebook and pasted in the above answer. No pain at all. $\endgroup$ – JimB Apr 10 '17 at 18:27
  • $\begingroup$ I may have thousands of data sets to analyze - method must not have any manual steps, if I understand what you are suggesting. $\endgroup$ – Tom Mozdzen Apr 14 '17 at 15:27
  • 1
    $\begingroup$ I agree cutting-and-pasting thousands of times is not practical. However, when I mentioned cutting-and-pasting it was about providing a single minimal working example for us to work on. $\endgroup$ – JimB Apr 14 '17 at 15:37
0
$\begingroup$

I discovered that the problem was with my data somehow. I'm guessing there is something in the text file that is not importing correctly

When I inserted

datalist = Round[datalist, 0.000000000001]; 

or

datalist=1.0*datalist;

the problem went away.

Very strange, but it is a simple workaround that is working for me. some bad whitespace somewhere?

The original data was imported from a text file with the commands

data = Import["datafile.txt", "Table"];
datalist = data[[1 ;;, 1 ;; 2]];
datalist = Select[datalist, # != {#, 0} &];

Comments welcome

$\endgroup$
2
  • $\begingroup$ You still haven't given enough information for anyone to make educated comments. We'd really like to solve the issue but we're just not getting clarification from you. $\endgroup$ – JimB Apr 9 '17 at 21:50
  • $\begingroup$ There you go, but I think the cutting and pasting has removed the issue, although I would have thought the import would have cleaned it up all by itself. I'm not sure how to upload a text file directly. $\endgroup$ – Tom Mozdzen Apr 9 '17 at 22:59

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