1

I'm trying to do the following in gnuplot: color with gradient an ellipse. Each point of the ellipse has its z-value given by the function

charge_density(t,beta) = -sin(t)*beta*(sqrt(1-(beta**2)))/(1-((sin(t)*beta)**2))

The radius function of the ellipse is given by a similar function:

radius(t,beta) = sqrt(1-(beta**2))/sqrt(1-((sin(t)*beta)**2))

Where beta is just a parameter satisfying 0<beta<1, and t is the angle.

Well, I tried to use the "+" special file with the lc rgb variable option, but it doesn't work with polar coordinates.

I also tried the set mapping cylindrical, but nothing happened.

Is this possible only with cartesian coordinates? In this case, I'll have to do 2 graphics and modify the above functions...

Or will I have to create a data file with angle, radius, z data?

I'd like to do this with pm3d and the following palette:

set palette model RGB defined (-1 "blue", 0 "black", 1 "red")
Larara
  • 225
  • 1
  • 9

1 Answers1

3

here is the code:

beta =0.5
charge_density(t,beta) = -sin(t)*beta*(sqrt(1-(sin(t)*beta)**2))/(1-((sin(t)*beta)**2))
radius(t,beta) = sin(t*beta) # your function equals 1!

# convert polar to carthesian
r_x(t)=radius(t,beta)*cos(t)
r_y(t)=radius(t,beta)*sin(t)


set palette model RGB defined (-1 "blue", 0 "black", 1 "red")

set size ratio -1 # same unit length in x and y

# number of sample points.
# increase if curve has edges
set samples 100 

#decouple range of "+" and xrange
set parametric

plot  [0:3*pi] "+" u (r_x($1)):(r_y($1)):(charge_density($1,beta))\
    with lines linewidth 3 linecolor palette

and there the result:

enter image description here

NOTE: Your radius equals to 1, so I took another function. Also, your charge_density has one extra closing parenthesis.

Some comments:

  • if you plot with lc rgb variable, a 24bit RGB color value is expected:

    (red*256^2 + green*256 + blue) with red, green, blue = 0...255

    If you want gnuplot to use the color according to the palette, write lc palette

  • gnuplot 4.6 does not support "+" for polar. Also, the mapping sets the behavior for 3D plots. However, as your formula calculates a radius for an angle, you can easily transform this to carthesian coordinates and plot these. There is still one drawback: The range given in the plot sets the xrange, too. This also means that the "length" of your curve changes when you change the xrange. You can solve this by set parametric which causes gnuplot to use a dedicated variable u instead of x when plotting functions. It is nice (and helps you) that this affects the special file "+", too. I do not know if this is a (positive) bug or a feature.

sweber
  • 2,916
  • 2
  • 15
  • 22
  • Sweber, I corrected the functions. Thanks for advising! – Larara Nov 23 '14 at 06:20
  • That was exactly what I was needing! Just one thing: although I used `set parametric`, it was the `set xrange` and `set yrange` that worked to change the ranges, not the `set urange` and `set vrange`. – Larara Nov 23 '14 at 06:42
  • The point is, the script could contain a `set xrange[-1:1]`. Then, `"+"` goes from -1 to 1, too. So, your parameter depends on the xrange, what is not what you want here. If you use `plot [0:3*pi] "+"...`, the xrange will automatically be set to [0:9.4], overriding your `set xrange` command. But with `parametric`, it doesn't. And yes, the `set urange` has no effect here. As said, it's a bit strange. – sweber Nov 23 '14 at 17:30