Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I format my .dat file so that a 3D vector plot can be made?
    primarykey
    data
    text
    <p>I'm working this programming task for college where we have to write a <code>c++</code> program that calculates the magnetic field vector for certain coils in 3D space. </p> <p>I've managed to write this program and I think I've got it working pretty well. </p> <p>I want to add in a special thinh though (it's my exam paper, so it has to be extra good!): I wan't to plot the vectors out.</p> <p>I'm used to calling <code>gnuplot</code> from <code>c++</code> (via piping) and this is what I usually do:</p> <ol> <li>create an output stream that writes the data to a <code>.dat</code> file</li> <li>open a gnuplot pipe</li> <li>make gnuplot plot all the contents of the <code>.dat</code></li> </ol> <p>Since my data has always been 2D, <code>x</code>and <code>y</code> plots, I'm quite lost here. My question is:</p> <ol> <li>How to format the <code>.dat</code> file (e.g. do I use braces to group vector components?)</li> <li>what is the actual gnuplot command to plot a 3D vector field?</li> </ol> <p>It'd be easy if I could format the <code>.dat</code> file like this:</p> <pre><code># Px Py Pz Bx By Bz 1 0 2 0.7 0.5 0.25 #&lt;= example data line ... more data ... </code></pre> <p>when the magnetic field vector in the point <code>P=(1,0,2)</code>equals a vector <code>B=(0.7,0.5,0.25)</code>. This would be easy to program, the real question is: will this do ? and how to I plot it in gnuplot. (wow, I've asked the same question 3 times I guess).</p> <hr> <h2><em>Piping</em> to gnuplot</h2> <p>Ok, since someone asked me to describe how I <em>pipe</em> (don't know if it's the right term thought) stuff to <code>gnuplot</code>. Here it is:</p> <ol> <li><p>First open up a pipe and call it <code>pipe</code>:</p> <pre><code>FILE *pipe = popen("gnuplot -persist 2&gt;/dev/null", "w"); </code></pre></li> <li><p>Tell <code>gnuplot</code> what to do through the pipe:</p> <pre><code>fprintf(pipe, "set term x11 enhanced \n"); fprintf(pipe, "plot x^2 ti 'x^2' with lines\n"); </code></pre> <p>notice the <code>\n</code>which is absolutely necessary. It is what executes the command.</p></li> <li><p>close the pipe:</p> <pre><code>pclose(pipe); </code></pre></li> </ol> <p>The necessary library is called <code>&lt;fstream&gt;</code> I believe. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload