3个变量的函数的轮廓图 [英] Contour plot of a function of 3 variables
问题描述
我们可以如下制作二维中f=(x.^2) + (y.^2);
的等高线图:
As we can make contour plot of f=(x.^2) + (y.^2);
in 2-D as follows:
[x,y]= meshgrid(-10:10, -10:10);
contour(x,y, (x.^2)+(y.^2));
,我们可以使用contour3
我们可以在3-D中绘制f=(x.^2) + (y.^2) + (z.^2);
的等高线图吗?
Can we make contour plot of f=(x.^2) + (y.^2) + (z.^2);
in 3-D
推荐答案
matlab函数isosurface
可以满足您的要求.但是,您也可以使用其他替代方法(例如使用surf
)来获得所需的结果.我将介绍这两种方法.
The matlab function isosurface
can do what you are asking. However you can also achieve the results you want using other alternatives, like using surf
. I will cover both methods.
我们需要为x
,y
和z
创建域,然后使用这些值生成3D网格,以便我们可以评估函数f(x,y,z) = x^2 + y^2 + z^2
.然后,我们将给常量k
一个值,并将所有这些信息提供给isosurface
,以便我们可以获得满足以下条件的(x,y,z)
值族:f(x,y,z) = k
.请注意,该轮廓实际上是球体!最后,我们可以使用patch
生成具有这些值的曲面.
We need to create the domain for x
, y
and z
and then generate a 3D mesh with those values so that we can evaluate the function f(x,y,z) = x^2 + y^2 + z^2
. Then, we shall give a value to the constant k
and feed all this information to isosurface
, so that we can obtain the family of (x,y,z)
values that satisfy the condition: f(x,y,z) = k
. Note that this contours are in fact spheres! Finally we can use patch
to generate a surface with those values.
有趣的是,反复为k
指定不同的值,并查看与这些值相关的轮廓.
It is very interesting to give different values for k
iterativelly and see the contours associated with those values.
% Value for x, y and z domain.
a = 10;
% Domain for x ,y and z.
x = linspace(-a,a);
y = linspace(-a,a);
z = linspace(-a,a);
% Generate a 3D mesh with x, y and z.
[x,y,z] = meshgrid(x,y,z);
% Evaluate function (3D volume of data).
f = x.^2 + y.^2 + z.^2;
% Do contours from k = 0 to k = 100 in steps of 1 unit.
for k = 0:100
% Draw the contour that matches k.
p = patch(isosurface(x,y,z,f,k));
isonormals(x,y,z,f,p)
p.FaceColor = 'red';
p.EdgeColor = 'none';
% Adjust figure properties.
title(sprintf('Contours of f(x,y,z) = x^2 + y^2 + z^2\nwith f(x,y,z) = k = %d',k));
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
axis equal;
grid on;
box on;
axis([-10 10 -10 10 -10 10]);
camlight left;
lighting phong;
% Update figure.
drawnow;
% Clear axes.
cla;
end
这是输出:
与前面的方法一样,要使函数f(x,y,z) = x^2 + y^2 + z^2
轮廓化,我们需要将函数匹配为一个常数值,即f(x,y,z) = k
,其中k
是您选择的任何常数值.
As in the previous method, to contour the function f(x,y,z) = x^2 + y^2 + z^2
, we need to match the function to a constant value, this is f(x,y,z) = k
, where k
is any constant value you choose.
如果按照k
,x
和y
的方式隔离z
,我们将得到:z = ± sqrt(k-x.^2-y.^2)
,因此我们具有x
,y
和z
的显式值.现在,让我们迭代地为k
提供不同的值,并查看通过surf
函数获得的结果!
If we isolate z
in terms of k
, x
and y
we have: z = ± sqrt(k-x.^2-y.^2)
, so we have the explicit values for x
, y
and z
. Now, let's give different values for k
iterativelly and see the results that we get with the surf
function!
% Do contours from k = 0 to k = 100 in steps of 1 unit.
for k = 0:100
% Find the value where: k - x^2 - y^2 = 0
a = sqrt(k);
% Domain for x and y.
x = linspace(-a,a);
y = linspace(-a,a);
[x,y] = meshgrid(x, y);
% Isolate z in terms of k, x and y.
z = sqrt(k-x.^2-y.^2);
% Find complex entries in z.
i = find(real(z)~=z);
% Replace complex entries with NaN.
z(i) = NaN;
% Draw upper hemisphere of surface.
surf(x,y,z,'FaceColor','red','EdgeColor','none');
hold on;
% Draw lower hemisphere of surface.
surf(x,y,-z,'FaceColor','red','EdgeColor','none');
% Adjust figure properties.
title(sprintf('Contours of f(x,y,z) = x^2 + y^2 + z^2\nwith f(x,y,z) = k = %d',k));
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
axis equal;
grid on;
box on;
axis([-10 10 -10 10 -10 10]);
camlight left;
lighting phong;
% Update figure.
drawnow;
hold off;
end
这是输出:
参考
我从 David Arnold的文章 "Matlab中的复数和绘图" ,非常值得一读,它将帮助您了解如何绘制生成复数的函数.
I took some of the ideas from David Arnold's article "Complex Numbers and Plotting in Matlab", which is well worth a read and will help you understand how to plot functions that generate complex numbers.
这篇关于3个变量的函数的轮廓图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!