2

I have written a code where i am trying to simulate n numbers of bubbles but my problem is that i have to hard code my function as you can see that i am using fx, fy, fz then fx1, fy1, fz1 and fx2, fy2, fz2.

Instead of that i want to create my functions using a loop and also want to plot them using a loop.Is there any way to do that.

Here is the code :

w=1.0
set isosample 50
set parametric
set urange [0:2*pi]
set vrange [-pi/2:pi/2]
do for [w=1:50]  {
  fx(v,u,w)  = w*cos(v)*cos(u)
  fy(v,u,w)  = w*cos(v)*sin(u)
  fz(v,w)    = w*sin(v) 
  fx1(v,u,w) = 20+w*cos(v)*cos(u)
  fy1(v,u,w) = 30+w*cos(v)*sin(u)
  fz1(v,w)   = 30+w*sin(v) 
  fx2(v,u,w) = 40+w*cos(v)*cos(u)
  fy2(v,u,w) = 60+w*cos(v)*sin(u)
  fz2(v,w)   = 60+w*sin(v) 
  splot fx(v,u,w), fy(v,u,w), fz(v,w), fx1(v,u,w), fy1(v,u,w), fz1(v,w), fx2(v,u,w), fy2(v,u,w), fz2(v,w)  with pm3d
}
Thor
  • 45,082
  • 11
  • 119
  • 130
  • Can you be a bit clearer about what you are trying to achieve? It seems like you want to generate the functions on the fly, in that case `eval` might be helpful. [This question/answer](https://stackoverflow.com/q/21233470/1331399) shows an example of this – Thor Feb 23 '18 at 08:50
  • I am trying to give loop while plotting my functions. – Rajneesh Sharma Feb 26 '18 at 05:50

1 Answers1

3

You can use the splot for syntax of gnuplot:

set isosample 50
set parametric
set urange [0:2*pi]
set vrange [-pi/2:pi/2]

fx(v,u)  = 2.0*d + w*cos(v)*cos(u)
fy(v,u)  = 3.0*d + w*cos(v)*sin(u)
fz(v)    = 3.0*d + w*sin(v)   

do for [w=1:50]  {
  splot for [d = 0:20:10] fx(v,u), fy(v,u), fz(v) with pm3d
}

I think you want to read these gnuplot help pages: help for and help for loops. Depending on your needs, you might also want to read help word and for example this question or help array if you are on gnuplot 5.2.

maij
  • 4,094
  • 2
  • 12
  • 28