I generated 2 .dat files with 1000 data points separately, imported as Table and flatten them, then combined and formed a Table for ListPlot. But it gives me a blank plot and I don't know why.

Here's the full code:

a = OpenWrite["a0_sim.dat"];
p = OpenWrite["p_sim.dat"];

bmax[\[Theta]w_] := (1.14*10^9)/Sin[\[Theta]w/2]^(9/7);
db[\[Theta]w_] := bmax[\[Theta]w]/10^5;
Pb[\[Theta]w_, b_] := (2 b db[\[Theta]w])/bmax[\[Theta]w]^2;        
a0[\[Theta]w_, b_] := (9*10^10)/((2.5*10^63)/(Sin[\[Theta]w/2]^7 b^7)-Sin[\[Theta]w/2]^2); 

n = 10^3;
m = 0;
While[m < n, {\[Theta]w = \[Pi] Random[];
               b = bmax[\[Theta]w] Random[];
               Write[a, a0[\[Theta]w, b]];
               Write[p, Pb[\[Theta]w, b]];
ain = Flatten@Import["a0_sim.dat"];
pin = Flatten@Import["p_sim.dat"];
ap = Table[{ain[[i]], pin[[i]]}, {i, 1, Length@ain}];

And what my table prints like: enter image description here

This is really troubling me and thanks so much for your help!

  • 2
    $\begingroup$ Please show the ListPlot command you used. Display all code and data in Mathematica format, not as images. $\endgroup$ – bbgodfrey Jul 26 '17 at 3:26

Here's the Mathematica way of achieving that:

bmax[θw_] := (1.14*10^9)/Sin[θw/2]^(9/7);   
db[θw_] := bmax[θw]/10^5;
Pb[θw_, b_] := (2 b db[θw])/bmax[θw]^2;
a0[θw_, b_] := (9*10^10)/((2.5*10^63)/(Sin[θw/2]^7 b^7)-Sin[θw/2]^2);
ap = Table[
   θw = π Random[];
   b = bmax[θw] Random[];
   {a0[θw, b], Pb[θw, b]}
   , {m, 0, 10^3}];

enter image description here

If in addition you want to export the files, use Export.

  • $\begingroup$ Thanks so much! That does the job so tidy. So does my problem occurs because my coding complicates things? But where is the problem exactly, if I may ask? $\endgroup$ – Yuan Gao Alex Jul 26 '17 at 4:05
  • $\begingroup$ I didn't bother to look for the mistake. The advantage of doing things in the "natural" way is that things are "automatically" fine and you avoid having to solve problems that simply don't occur in the first place. I'd recommend going over this post to get some basic Mathematica practices. $\endgroup$ – yohbs Jul 26 '17 at 13:11

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.