Adam West
Adam West

Reputation: 105

Gnuplot: Graphing and Defining Variables in Polar Coordinates

I am brand new to Gnuplot and am having a problem trying to figure out how to graph in Polar Coordinates for a school assignment. What bothers me is we didn't go over other coordinate systems like Polar or Parametric at all for Gnuplot, and the internet tutorials I find seem to assume some basic knowledge and just tell me to do "set polar".

Here is the problem I am trying to solve:

The electron density around a particular molecule centered at the origin is described by

n(r,theta) = [cos(r)]^2 * {1+[cos(theta)]^2} * exp(-r^2/25)

where r and theta are usual polar coordinates [e.g., (x,y) = (r*cos(theta),r*sin(theta))].

Write a gnuplot script elec.gpl that generates a surface plot of this function on a domain of x=-5..5 and y=-5..5. Set your script so that

gnuplot> elec.gpl

generates the plot as a postscript file called "elec.ps"

Since I am completely unfamiliar with graphing Gnuplot in polar coordinates, I am stumped. I have a tried a few different things including the following:

    set terminal png enhanced
    set output 'elec.ps'
    set polar
    set angle degrees
    set title 'Electron Density Around Molecule'
    set xrange[-5:5]
    set yrange[-5:5]
    set grid
    set grid polar
    plot (cos(x))^2 *(1+(cos(y))^2)*exp(-x^2/25)
    quit

I have tried changing x to r, y to t, y to theta, etc. I simply can't figure out how Gnuplot wants me to define polar coordinate inputs. Is there a way to redefine x as r*cos(theta) and y as r*sin(theta) and then let me set inputs and ranges for r and theta?

Thank you for your help! :)

Upvotes: 1

Views: 3257

Answers (1)

Christoph
Christoph

Reputation: 48390

The polar mode allows you to plot a function depending on a single parameter t, which is used as angle, see e.g. the gnuplot polar demo. So you could plot one 'trajectory' for a fixed radius.

As you want to visualize a density distribution, I think you are better off with a heat map. I suggest to use the parametric mode, you one dummy variable is used as radius, the other one as angle theta. For better readability, I name the dummy variables accordingly (with set dummy ...), but for the ranges you must stick with the original dummy names u and v. So here is an example:

reset
set terminal pngcairo size 900,800
set output 'elec.png'

set title 'Electron Density Around Molecule'

set parametric
set dummy r, theta # instead of u,v
set urange[0:1.5*pi] # r
set vrange[0:2*pi]   # theta
set isosamples 200

set size ratio 1
set autoscale fix
set pm3d map
set palette defined (0 'white', 1 'red')

splot r*cos(theta), r*sin(theta), \
      cos(r)**2 + (1 + cos(theta)**2)*exp(-r**2/25) with pm3d t ''

In parametric mode you must specify three functions for splot depending on the dummy variable (here r and theta) which are spearated by commas.

The result is:

enter image description here

Now you can go on and enhance the plot according to your needs.

Upvotes: 1

Related Questions