Need help plotting with mathematica

In summary, when I hit shift enter, the slider returns as output, but the graph disappears once I stop pressing on the slider.
  • #1
jtassilo
14
0
Hi,
I want to use mathemiatica to predict the spectrum of compounds with different conformers. The input consists of 4 values: frequency (of each vibration and every conformer), Intensity, Polarizability and total energy (of the conformer).
I now want to plot the frequency vs the Intensity.
1. How do I get mathematica to plot only a vs b if my input is of the format (a, b, c, d)?
2. Next I want to weight each frequency peak using the Boltzmann distribution. So i basically want to get a graph plotting
frequency vs the Intensity*exp(-d/kT)/sum of(exp(-d/kT)).
3. After weighing each peak height with the Boltzmann distribution i want to broaden the frequencies using a gaussian profile, because in real spectra, the peaks have a certain width and are bell shaped and do not consist of single points. The gaussian function is a function of c (the Polarizability).

So in the end I would like to plot a*Function of c vs b*Function of d.
I would greatly
appreciate any help/suggestions.
 
Physics news on Phys.org
  • #2
I do not understand what you are asking. If a and b are each separate inputs to your function then they are independent of each other.
 
  • #3
let me rephrase my first question: Is it possible to plot in mathematica only x vs y if your input file consisits of (x, y, z, a), and you don't want to reformat your input file. If the input file is of the format (x,y) you can use the command ListPlot[input]; what is the command if your input file has a different format?
 
  • #5
Hm, I am getting the following message after typing ListPlot[data[[All, {1, 2}]]]:
{x1, y1, z1, b1}, {x2, y2, z2, b2} is not a list of numbers or pairs of numbers. (of course x1 is a specific number, which I abbreviate here with x1)
 
  • #6
You are really not describing your problem very well. What do you get when you do the following:

Dimensions[data]
MatrixQ[data]
MatrixQ[data,NumericQ]

If your data is not a n-row by 4-column matrix of numbers then what is it?
 
  • #7
First of all thanks for you help and patience!
My input is an excel file consisiting of 4 columns and 29 rows) {{a1, b1, c1, d1}, {a2, b2, c2, d2},...
When I typed the commands, I got
{1, 29, 4}
False
False
I would now like to
1. plot the first column versus the second (plot a vs b)
2. modify the values of the first 2 columns by functions that depend on values in the the other two columns.
 
  • #8
jtassilo said:
First of all thanks for you help and patience!
My input is an excel file consisiting of 4 columns and 29 rows) {{a1, b1, c1, d1}, {a2, b2, c2, d2},...
When I typed the commands, I got
{1, 29, 4}
False
False
I would now like to
1. plot the first column versus the second (plot a vs b)
2. modify the values of the first 2 columns by functions that depend on values in the the other two columns.
OK, you have an extra "layer", perhaps representing the worksheets in Excel. To trim that off use:
data = data[[1]];

Then you should get
Dimensions[data] == {29,4}
and the other two commands should give you True

Then ListPlot[data[[All,{1,2}]]] should work.

To get the other information in there try something like

data2 = Map[{f[#[[3]],#[[4]]],g[#[[3]],#[[4]]],#[[3]],#[[4]]}&,data]

Where f and g are the functions that determine your new values for a and b respectively.
 
  • #9
It worked, thanks!
I have one last question:
I am trying to plot several Gauss function.
My input is:
g= {a1, a2, a3,...}
Plot[Exp[-0.5*((x - g)/2)^2], {x, 0, 1200}]

This plots multiple gauss functions. The gauss functions overlap though and i want to add all the gauss functions together (basically they positively interfere ).
I tried the following command (it didn't work):
Plot[Sum[Exp[-0.5*((x - g)/2)^2], {x, 0, 1200}], {x, 0, 1000}]
Do you know what command might work for adding the functions?
 
  • #10
Try:

Plot[Apply[Plus,Thread[Exp[-0.5*((x - g)/2)^2]]],{x,0,1200}]

I am not sure it will work.
 
  • #11
Yes it works great,
it does however not work when I try to use Map:
Plot[Apply[Plus, Thread[Map[{ #[[2]]...} &, data]]], {x, 0, 1200}]
Any suggestions why Plot
[Apply[Plus, Thread...]
does not work with Map?
The normal Plot[Map[{ #[[2]]...} &, data]]], {x, 0, 1200}] command works.
 
  • #12
The expression:
Thread[Map[{ #[[2]]...} &, data]]

Threads the function Map over the list that appears in the first argument. This means that it evaluates to
{Map[#[[2]] &, data], Map[ ... &, data], ...}

Which is probably not what you wanted to do.
 
  • #13
Ok, this here is hopefully my last question:
I'm doing a dynamic plot using a rather complex equation (The problem does not occur with simple equations). When I hit shift enter, only the slider is returned as output and below it says "&Aborted". The strange thing is that when I use the slider it shows the graph. I can see how the graph changes when I move the mouse on the slider, but as soon as I stop pressing on the slider, the graph disappears and is replaced by "&Aborted". I read the help files concerning Dynamics and Aborted, but I haven't found a command that changes this problem. Since the calculation takes mathematica quite some time I am currently thinking that my computer might be too slow.
Any Suggestions are appreciated
 
  • #14
If it is easy to post the code then I will try it out. I know that there is an abbreviated rendering while you are dynamically adjusting the setting, but I have not run into that specific error before.
 
  • #15
Here is the input: My other input is named data={{a1, b1, c1, d1,e1, f1}, {a2,..}}
Panel[Column[{Row[{Slider[Dynamic[T], {1, 350, 1}], Dynamic[T]}],
Dynamic[Plot[
Apply[Plus,
Thread[1.097455759580536*10^-5/
data[[All,
1]]*(1879421 - data[[All, 1]])^3/(1 -
Exp[-data[[All,
1]]*100*299792458*6.626*10^-34/(1.38*10^-23*T)])*
data[[All, 2]]* data[[All, 6]]*
Exp[- data[[All, 5]]*4.3597*10^-18/(1.38*10^-23 T)]/
Total[data[[All, 6]]*
Exp[-data[[All, 5]]*4.3597*10^-18/(1.38*10^-23*T)]]/(
data[[All, 1]]*(2 Pi)^0.5)*
Exp[-0.5*((x - data[[All, 1]])/2)^2]]], {x, 0, 1000},
ImageSize -> {{400, 1000}},
AxesLabel -> {v [cm^-1], I [10^-10 m^2/sr]}, PlotRange -> All,
PlotLabel -> Pentane T [K]]]}]]
 
Last edited:
  • #16
Also note please that if the beginning of the input (Thread[1.097455759580536*10^-5 is replaced by a smaller number such as Thread[1.097455759580536*10^-105, the whole script does not work anymore, and the Gauss-function is approximated by a triangle. I think that it has something to do with the significant figures mathematica uses, but the helpfile isn't really helpful explaining how to fix this problem.
 
  • #17
I am certain now that the $Aborted message has to do with the calculating capacity. When I change the number of input rows from 29 to for example 1400, the error message is displayed even for simple equations
 
  • #18
Well, if it has to do with numerical precision then I won't be able to help with dummy data. However, there were a few non-numerical things that I noticed:
1) Use Manipulate instead of Panel[...{Slider[Dynamic,... It is a much cleaner interface and I think that T may be getting stuck.
2) Get rid of the call to the Thread function. All of the functions inside of it are "Listable" meaning that they are automatically threaded over their arguments.
3) Wrap your function to be plotted in Evaluate:
e.g. Plot[Evaluate[Apply[Plus,...]]],...]
This will reduce the number of computations that have to be done at run time.
4) With my dummy data I was not getting any $Aborted messages, but I had to get rid of the ImageSize -> {{400, 1000}} option which was really messing up the display for some reason.
 
  • #19
Hey, here's another question:
Is there a way to export the graph I made not as a bitmap or pdf, but as a set of points (x, y) with a predefined grid/accuracy? (Of course not a dynamic graph, but a normal graph)
 
  • #20
Sure, just use Rasterize[graph]

That will return a Graphics object where the first element is a Raster object. The first element of the Raster object, in turn, is a matrix containing the RGB color values at each point in the predefined grid.
 
  • #21
hm I couldn't get it to work...
Lets say I want a list or table with x and y values for the function y=sinx.
Is there any way to do that?
 
  • #22
Sure, just use Table for that.

Table[{x,Sin[x]},{x,xmin,xmax,dx}]
 
  • #23
Great, thanks for the tip!
And I have another question concerning loop commands: I want to import several files (c1, c2...) Is there a command that allows to import c1, c2.. at once like:
Do[Import["ci.txt"], {i, imin, imax, di}]? I've been trying several loop commands but couldn't get it to work.
Also is it possible to somehow use loop commands to simplify the following expression (substituting c1 and c2 by ci again):
Evaluate[Apply[Plus, Exp[-(x - c1[[All, 1]])^2] + Exp[-((x - c2[[All, 1]]))^2]+...]]?
 
  • #24
I usually put the filenames in a list of strings called filenames. Then you can use the following command:

dat = Map[Import[#]&, filenames];

You can even specify options in the Import command if they will all use the same options.
 

1. How do I plot a function in Mathematica?

To plot a function in Mathematica, use the Plot function and specify the function you want to plot as the first argument. For example, to plot the function f(x) = x^2, you would use the code "Plot[x^2, {x, 0, 5}]" where the second argument specifies the range of values for x.

2. Can I customize the appearance of my plot in Mathematica?

Yes, Mathematica offers many options for customizing the appearance of your plots. You can change the color, style, and thickness of lines, add labels and titles, and adjust the axes and ticks. Refer to the Mathematica documentation for a full list of customization options.

3. How do I plot multiple functions on the same graph in Mathematica?

To plot multiple functions on the same graph, simply separate each function with a comma inside the Plot function. For example, "Plot[{x^2, x^3}, {x, 0, 5}]" will plot both f(x) = x^2 and g(x) = x^3 on the same graph.

4. Is it possible to save my plot in Mathematica?

Yes, you can save your plot in Mathematica by using the Export function. This allows you to save your plot as an image file, such as PNG or JPEG, or as a vector graphics file, such as PDF or SVG. You can also use the Save As option in the plot window to save your plot as a Mathematica notebook.

5. What is the difference between a 2D and 3D plot in Mathematica?

A 2D plot in Mathematica displays a function in two dimensions, with the x and y axes representing the independent and dependent variables, respectively. A 3D plot, on the other hand, adds a third dimension, typically represented by the z axis. This allows you to visualize functions that depend on two or more variables.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
240
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top