所以我有一个非常冗长的功能,它可以绘制出更高级函数的高度,并具有许多峰和谷。
我应该将这个表面绘制在x和y轴上,其中x跨越1000到1000个均匀间隔的点,从0到10。
但是我并不真正理解meshgrid和mesh的复杂性。
如前所述,这是我应该绘制的错综复杂的功能。
function z = topography(x,y)
z= 0.001*(x-3).^2 +0.001*(y-4).^2 ...
+0.5*exp(-0.25.*(x-11).^2-0.01*y.^2)...
+0.5*exp(-0.01.*x.^2-0.25*(y-11).^2)...
+0.5*exp(-0.1.*(x+1).^2-0.01*(y-5).^2)...
+0.3*exp(-0.1.*(x-3.5).^2-0.5*(y+1).^2)...
+0.5*exp(-0.1.*(x-8).^2-0.1*(y-0).^2)...
+1.*exp(-0.1.*(x-9).^2-0.1*(y-8.5).^2)...
+0.5*exp(-0.5.*(x-6).^2-0.1*(y-6).^2)...
+0.25*exp(-0.5.*(x-3).^2-0.5*(y-8).^2)...
+0.5*exp(-(x-5).^2-0.5*(y-5).^2)...
+0.25*exp(-0.75.*(x-2).^2-(y-8).^2)...
+0.5*exp(-(x-6).^2-0.5*(y-3).^2)...
+0.5*exp(-(x-5).^2-0.5*(y-9).^2)...
+0.5*exp(-(x-9).^2-0.5*(y-5).^2);
end
这是不使用meshgrid的方法:
xx=linspace(0,10,1000); % A vector with x-values from 0 to 10
yy=linspace(0,10,1000); % A vector with y-values from 0 to 10
zz=zeros(length(xx)); % A matrix of z-values
for i = 1 : length(xx)
for j = 1 : length(yy)
zz(i,j)= topography(xx(j),yy(i));
end
end
figure(1);
mesh(xx, yy, zz) % The graph z=f(x,y) for topography
这是我尝试使用meshgrid将其转换为脚本
xx = linspace(0,10,1000);
yy=linspace(0,10,1000);
zz=topography(x,y);
[x,y,z]=meshgrid(xx,yy,zz);
mesh(z);
我期待一个漂亮的3D图形描绘冗长的功能“地形图”。相反,我收到错误消息:
Requested 1000x1000x10201 (76.0GB) array exceeds maximum array size
preference. Creation of
arrays greater than this limit may take a long time and cause MATLAB to become
unresponsive.
See array size limit or preference panel for more information.
Error in meshgrid (line 77)
xx = repmat(xx, ny, 1, nz);
Error in figure11 (line 6)
[x,y,z]=meshgrid(xx,yy,zz);
它说数组超出了数组限制。但是为什么这不是使用循环完成的工作脚本的情况呢?
meshgrid
通常用于仅生成自变量。一旦创建了自变量网格,就会使用它们计算因变量。
因此,在您的脚本中,使用meshgrid
创建x-y平面,并通过使用这些网格评估函数来创建地形(即高度):
x = linspace(0,10,1000);
y = linspace(0,10,1000);
[xx, yy] = meshgrid(x,y);
zz = topography(xx, yy);
mesh(xx, yy, zz);
这假设函数是完全矢量化的,因此它可以接收到矩阵并生成一个大小相等的矩阵。发布的地形功能似乎就是这样。