我应该如何格式化我的.dat文件,这样一个三维向量图可以制成?我的、向量、这样一个、文件

2023-09-08 00:37:16 作者:何处繁华笙歌落

我正在这种编程任务的大学,我们必须写一个 C ++ 程序,计算磁场矢量在三维空间中的某些线圈。

I'm working this programming task for college where we have to write a c++ program that calculates the magnetic field vector for certain coils in 3D space.

我已经成功地写这个计划,我想我已经得到了它的工作pretty的好。

I've managed to write this program and I think I've got it working pretty well.

我想在一个特殊的THINH虽然增加(这是我的试卷,所以它必须是额外的好!):我wan't绘制矢量出来

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.

我已经习惯了叫 gnuplot的 C ++ (通过管道),这是我平时做:

I'm used to calling gnuplot from c++ (via piping) and this is what I usually do:

创建的数据写入到输出流的 .DAT 文件 打开一个gnuplot的管道 请gnuplot的阴谋中的所有内容的 .DAT create an output stream that writes the data to a .dat file open a gnuplot pipe make gnuplot plot all the contents of the .dat

由于我的数据一直是2D, X 的情节,我很丢在这里。我的问题是:

Since my data has always been 2D, xand y plots, I'm quite lost here. My question is:

如何格式化 .DAT 文件(例如我用括号组矢量分量?) 什么是实际gnuplot的命令来绘制一个三维矢量场? How to format the .dat file (e.g. do I use braces to group vector components?) what is the actual gnuplot command to plot a 3D vector field?

这将会是容易的,如果我能格式化 .DAT 文件是这样的:

It'd be easy if I could format the .dat file like this:

# Px    Py    Pz    Bx    By    Bz
  1     0     2     0.7   0.5   0.25 #<= example data line
  ... more data ...

当在该点的磁场矢量 P =(1,0,2)等于一个vector B =(0.7,0.5,0.25 )。这将是易于编程,真正的问题是:这该怎么办?以及如何我绘制它gnuplot的。 (哇,我问过同样的问题3次,我猜)。

when the magnetic field vector in the point P=(1,0,2)equals a vector B=(0.7,0.5,0.25). 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).

好了,因为有人问我描述我是如何的管道的(不知道这是否是正确的术语思想)的东西 gnuplot的。在这里,它是:

Ok, since someone asked me to describe how I pipe (don't know if it's the right term thought) stuff to gnuplot. Here it is:

首先打开一个管道,并呼吁它管道

FILE *pipe = popen("gnuplot -persist 2>/dev/null", "w");

告诉 gnuplot的办通过管什么:

Tell gnuplot what to do through the pipe:

fprintf(pipe, "set term x11 enhanced \n");
fprintf(pipe, "plot x^2 ti 'x^2' with lines\n");

注意这是绝对必要的。这就是执行命令。

notice the \nwhich is absolutely necessary. It is what executes the command.

关闭管道:

pclose(pipe);

必要的库称为&LT; fstream的&GT; 我相信。

The necessary library is called <fstream> I believe.

推荐答案

我做了这个简单的例子向您展示如何绘制矢量场。输出会是这样的PIC:

I made this simple example to show you how to draw a vector field. The output would be something like this pic:

我用来绘制,这是该数据例如:

The data example I used to plot this was:

# Px    Py    Pz    Bx    By    Bz
  0     0     0     0.8   0.8   0.45
  0     0     1     0.5   0.7   0.35
  0     0     2     0.7   0.5   0.25
  0     1     0     0.65   0.65   0.50
  0     1     1     0.6   0.6   0.3
  0     1     2     0.45   0.45   0.20
  1     0     0     0.5   0.7   0.35
  1     0     1     0.75   0.75   0.4
  1     0     2     0.85   0.85   0.25
  1     1     0     0.90   0.85   0.23
  1     1     1     0.95   0.86   0.20
  1     1     2     0.98   0.88   0.13
  2     0     0     0.73   0.83   0.43
  2     0     1     0.53   0.73   0.33
  2     0     2     0.73   0.53   0.23
  2     1     0     0.68   0.68   0.52
  2     1     1     0.63   0.57   0.23
  2     1     2     0.48   0.42   0.22

要绘制它的命令是:

gnuplot> splot "./data3d.dat" with vectors

现在你应该阅读第44页的官方手册(这里的的