简体   繁体   English

Octave:向/从文本文件写入/读取 f(x,y) 出错

[英]Octave: Write/Read f(x,y) to/from text file goes wrong

I have problem with reading and drawing function from text file.我在从文本文件中读取和绘制 function 时遇到问题。 Code to make and write function:制作和编写 function 的代码:

clc, clear;
plik=fopen('dane.txt', 'w')
x= -5:.1:5;
y= flip(rot90((-5:0.1:5)));
z =sin(x).*y;
fprintf(plik,'x          y         z\n');
for i=1:101
  for j=1:101
    fprintf(plik,'%6.3f   \t%6.3f   \t%6.3f \n', [x(i); y(j); z(i,j)]);
  endfor
 endfor
mesh (x,y,z)
fclose(plik);

And code to read and draw function f(x,y).以及读取和绘制 function f(x,y) 的代码。

clear,clc;
plik = fopen('dane.txt','r');
a = dlmread(plik, '\t', 1, 0);

x=a(:,1);
y=a(:,2);
z=a(:,3);

plot3(x, y, z);
grid on
fclose(plik);

The problem is with drawing plot.问题在于绘图 plot。 It draws desired function BUT it also draws something that looks like f(x,y)=x*y plot.它绘制了所需的 function 但它也绘制了看起来像 f(x,y)=x*y plot 的东西。 What can be an issue here?这里有什么问题?

The plot3 command takes X, Y, and Z column-vectors, and then treats each row as an (x,y,z) coordinate to plot. plot3命令采用 X、Y 和 Z 列向量,然后将每一行视为 plot 的 (x,y,z) 坐标。 As it moves from one row to the next, it draws a line connecting one point to the next .当它从一行移动到下一行时,它会绘制一条连接一个点到下一个点的线

The way your data is arranged in the file, most points are rather close to each other, so drawing a line from one point to the next is mostly harmless, except that you have a big jump from when you go from point (x_i, y_N) to (x_{i+1}, y_0).您的数据在文件中的排列方式,大多数点彼此相当接近,因此从一个点到下一个点画一条线几乎是无害的,除了您从点 (x_i, y_N ) 到 (x_{i+1}, y_0)。 The line that is drawn in this case is quite visible, since it connects the right end of the graph to the left one.在这种情况下绘制的线非常明显,因为它将图形的右端连接到左端。

If you'd like to get rid of that line, either plot markers only (eg plot3(x,y,z,'o') , or reshape your data into a square matrix; in the presence of a matrix instead of a single vector, plot3 treats each column of the matrix as a separate line to draw, therefore this would enable the 'big' line to go away. Note that if you do this however, you'll have to specify a colour, otherwise each line will be drawn in an arbitrarily new colour.如果您想摆脱那条线,仅 plot 标记(例如plot3(x,y,z,'o') ,或将您的数据重塑为方阵;在存在矩阵而不是单个矩阵的情况下向量,plot3 将矩阵的每一列视为要绘制的单独线,因此这将使“大”线远离 go。请注意,如果您这样做,则必须指定颜色,否则每条线将用任意的新颜色绘制。

Also, note that the output will still not be identical to that of 'mesh';另外,请注意 output 仍将与“网格”不同; instead your surface will be created as a series of "parallel lines".相反,您的表面将被创建为一系列“平行线”。 If you want to recreate the mesh, just pass the reshaped matrices into mesh as before.如果要重新创建网格,只需像以前一样将重新整形的矩阵传递到mesh中。

In other words compare换句话说比较

r = @(x) reshape( x, [101,101] )
plot( r(x), r(y), r(z) );

to:至:

mesh( r(x), r(y), r(z) );

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM