# issue in data analysis of 127 galaxies

Posted 8 years ago
9192 Views
|
5 Replies
|
0 Total Likes
|
 Good evening,I encounter a difficult problem with Mathematica (last release). I have 127 lines of datas, with for each line, 9 columns. I imported from a txt file. The fourth column is called ropt, and using it I define rd as ropt/3.2.I want to plot for radius between 1/3.2 and 1, a certain function dvtot[radius*rd] and this for each line of the data table.I tried with this but it didn't work (blank plot) : ListPlot[Table[{radius, dvtot[radius*rd]}, {radius, 1/3.2, 1, 0.1}], Filling -> Axis, PlotLegends -> "Expressions", Frame -> True, PlotLabel -> "dLog(Vtot)/dLog(r)", PlotRange -> All, FrameLabel -> {r/Rd, dLog[V]/dLog[r]}, PlotStyle -> Thickness[0.003]] I tried also this: Plot[{dvtot[radius*rd[[2]]]}, {radius, 1/3.2, 1}, PlotLegends -> "Expressions", Frame -> True, PlotLabel -> "dLog(Vtot)/dLog(r)", PlotRange -> All, FrameLabel -> {r/Rd, dLog[V]/dLog[r]}, PlotStyle -> Thickness[0.003]] Of course this works it give me one curve corresponding to the second line of the data table, but I want to automatically generate plots for each line of datas and export them, so at the end I will end up with 127 plots.Also I would like to have one plot with 127 curves, but I think it is less relevant than the thing before.I don't know it loops (like for loops) can be easier to handle with. I can give you more in detail the function that I want to plot, but I think it is not relevant for my problem, because the problem that I have is not in defining the function; the function works.Thanks for you helpJean-Philippe
5 Replies
Sort By:
Posted 8 years ago
 Hello Jean-Phillipe,Module is a Mathematica construct which can declare some variables to be local by naming them in the leading curly braces. It encapsulates the code it contains in a context where those variables are in scope only within the module. It is similar to a C function with local variables.My use of record was only as a user variable. I think of one line of your data file as a record. I was writing a function which would accept a single record as an argument and make and export a plot. This so I could Map (look up Map -- the short form is /@ ) it onto the data file and produce a plot for each record. This is a major observation in thinking in Mathematica. In C, we would use For or Do or While. But it is rare that an iteration of the loop depends on prior iteration results.In Mathematica, it is more effective to use Map, Thread, Apply, and other functionality to cause the same calculation on the elements of a list. Note that once your data was imported, it is a list of lists, with each of the lists being a list of the data items. Mapping the exportPlot function onto the data caused exportPlot to be executed on each record, which is one level down. (Map in the long form accepts an argument defining the level at which it operates.)I attach "Galaxies 3.nb" A few changes I made:1) The definition for v does not need to be a function -- we're just going to do symbolic math with it.2) I modified the definition of dvtot to include all of the several arguments. (We could have used a Rule and Replace, but this is clear.)3) I put the name back in the plot -- you could always remove it again.4) makePlot and exportPlot both call dvtot with a full argument set.One comment on blank plots: The most common cause is that the expression being plotted does not evaluate to a numerical value when the independent variable is replaced by a number. (Which is what Plot is doing.) A good way to check is to try to evaluate the expression yourself, for example 3x^2/.x->2 would evaluate to 12, but a x^2/.x->2 would evaluate to 4a, which is not a number. In your usage, Plot would still find v0 and v1 in symbol form -- I corrected that by including them in the argument list.Please do let me know how this works -- I'm really pleased to help. And I was really thrilled yesterday to learn that our little comet-explorer has phoned home.Best regards,David Attachments:
Posted 8 years ago
 hello david,Thanks a lot for your reply and your clear code. I don't understand, however, how Module and record work? Also, effectively, I didn't give you dvtot, here it is: v[v0_, v1_, r_, ropt_] := v0 + v1*r/ropt (*velocity for each individual galaxy*) dvtot[radius_] := (r/v[v0, v1, r, ropt])*D[v[v0, v1, r, ropt], r] /. r -> radiusI tried to use the function MakePlot that you created, and to modify it for the purpose of this dvtot, but it doesn't work (the example doesn't display anything), and I don't know why. Could you, please help me?Here is the code, that I use: makePlot[record_] := Module[{name, v0, v1, ropt, rde}, name = record[[1]]; v0 = record[[7]]; v1 = record[[8]]; ropt = record[[4]]; rd = ropt/3.2; Plot[dvtot[radius], {radius, 1/3.2, 1}, PlotLabel -> name <> "\n" <> "dLog(Vtot)/dLog(r)", Frame -> True, FrameLabel -> {"r/Rd", "dLog[V]/dLog[r]"}, PlotStyle -> Thickness[0.003]]]At the beginning v was only function of r_, but even with this it doesn't work.Thanks in advance, regardsJean-Philippe
Posted 8 years ago
 hi okkes dulgerci,First thanks for your answer. I attach the txt file to this post. I finally was able to generate the plots that I want, but I am stucked with another issue; which is to export as pdfs each of the plot. The way out that I found to generate the plots was to use a for loop; for instance: For[i = 1, i <= 10, i++, Print[Plot[{dvtot[radius*rd[[i]], i], 1.1 dvd[radius*rd[[i]], i]}, {radius, 1/3.2, 3}, PlotLegends -> "Expressions", Frame -> True, PlotLabel -> "dLog(Vtot)/dLog(r)", PlotRange -> All, FrameLabel -> {r/Rd, dLog[V]/dLog[r]}, PlotStyle -> Thickness[0.003]]] ] Do you know, please, how to proceed?Thanks in advance, regardsJean-Philippe Attachments:
Posted 8 years ago
 Hello Jean-Philippe,Attached please find a notebook giving an example. You did not specify your function dvtot, so I made one up as an example. What I have done is I think a good way to work with Mathematica:I start with small functions, and build them up to larger functions to do the work. That way things can be tested as we go.dvtot is a function of radius and rd. The fact that it depends only on the product is in the definition.Next makePlot uses that to generate a plot from a single record -- any record -- of the data file. We make a plot to test it.Next, exportPlot uses that to create a plot from a single record, and export it to a file named for the galaxy designator.And finally, this function is "mapped" onto the data file, which generates and exports a single plot for each record.Last, a plot of all the records is produces separately. A "pure function" is mapped onto the data to provide the list of expressions Plot accepts as an argument. Evaluate is used to cause plot to see a list, so it styles the lines.Kind regards,David Attachments:
Posted 8 years ago