【matlab】绘制曲面图

一位老哥让笔者画一个下面这种图,笔者当然要拿出喜欢的Matlab啦。

【matlab】绘制曲面图_第1张图片

1. meshgrid

在进行 3D 绘图操作时,涉及到x、y、z三组数据,而x、y这两组数据可以看做是在xoy平面内对坐标进行采样得到的坐标对(x,y)。例如,要在 3<=x<=5,6<=y<=9,z不限制区间 这个区域内绘制一个 3D 图形,如果只需要整数坐标为采样点的话。我们可能需要下面这样一个坐标构成的矩阵:

(3,9),(4,9),(5,9);
(3,8),(4,8),(5,8);
(3,7),(4,7),(5,7);
(3,6),(4,6),(5,6);

  在matlab中我们可以这样描述这个坐标矩阵,即,先把x坐标独立出来:

3,4,5;
3,4,5;
3,4,5;
3,4,5;

再把各个点的y坐标也独立出来:

9,9,9;
8,8,8;
7,7,7;
6,6,6;

这样对应的x、y结合,便表示了上面的坐标矩阵。meshgrid就是产生这样两个矩阵,来简化我们的操作。然后根据(x,y)计算获得z,并绘制出三维图形。

2. interp2

在Matlab中有一个二维的插值函数,举个例子,假设我们有一个3×3的矩阵,但是我们有了新的需求,想把该矩阵扩展成5×5的新的更大的矩阵。此时就出现了一个问题,5×5的矩阵一共有25个像素值,而3×3的矩阵只有9个像素值。所以如何用这9个像素值给新的25个像素值赋值,这就是一个插值的问题。

Vq = interp2(X,Y,V,Xq,Yq) interpolates to find Vq, the values of the
    underlying 2-D function V at the query points in matrices Xq and Yq.
    Matrices X and Y specify the points at which the data V is given.
 
    Xq can be a row vector, in which case it specifies a matrix with
    constant columns. Similarly, Yq can be a column vector and it
    specifies a matrix with constant rows.
 
Vq = interp2(V,Xq,Yq) assumes X=1:N and Y=1:M where [M,N]=SIZE(V).
 
Vq = interp2(V,K) returns the interpolated values on a refined grid
    formed by repeatedly halving the intervals K times in each dimension.
    This results in 2^K-1 interpolated points between sample values.
 
Vq = interp2(V) is the same as interp2(V,1).
 
Vq = interp2(...,METHOD) specifies alternate methods.  The default
    is linear interpolation.  Available methods are:
 
      'nearest' - nearest neighbor interpolation
      'linear'  - bilinear interpolation
      'spline'  - spline interpolation
      'cubic'   - bicubic interpolation as long as the data is
                  uniformly spaced, otherwise the same as 'spline'
 
    For faster interpolation when X and Y are equally spaced and monotonic,
    use the syntax Vq = interp2(...,*METHOD).
 
Vq = interp2(...,METHOD,EXTRAPVAL) specificies a method and a scalar
    value for Vq outside of the domain created by X and Y.  Thus, Vq will
    equal EXTRAPVAL for any value of Yq or Xq which is not spanned by Y
    or X respectively. A method must be specified for EXTRAPVAL to be used,
    the default method is 'linear'.
 
    All the interpolation methods require that X and Y be monotonic and
    plaid (as if they were created using MESHGRID).  If you provide two
    monotonic vectors, interp2 changes them to a plaid internally.
    X and Y can be non-uniformly spaced.
 
For example, to generate a coarse approximation of PEAKS and
    interpolate over a finer mesh:
        [X,Y,V] = peaks(10); [Xq,Yq] = meshgrid(-3:.1:3,-3:.1:3);
        Vq = interp2(X,Y,V,Xq,Yq); mesh(Xq,Yq,Vq)

3. 实现代码

x1 = 1e-8;
x2 = 1e-3;
y1 = 1e-8;
y2 = 1e-3;
gridwidth = 100;

z = [69.55 70.47 70.33 69.78 70.19 71.47; ...
    70.06 69.83 69.04 70.65 69.11 69.59; ...
    69.66 69.02 69.40 70.95 71.11 71.61; ...
    70.63 69.65 69.79 69.55 69.67 70.11; ...
    69.91 69.15 70.42 69.40 69.44 69.79 ; ...
    69.73 69.63 68.94 70.55 70.40 71.83];
x = linspace(x1, x2, size(z,1));
y = linspace(y1, y2, size(z,2));
%生成网格
[X,Y] = meshgrid(linspace(x1, x2, gridwidth),linspace(y1, y2, gridwidth));

% interp2 进行二维插值,插值要适中,过密将会变成一团黑6X6->500X500
V = interp2(x,y,v,X,Y,'cubic');
surf(X,Y,V);

%设置X轴和Y轴显示的范围
xlim=([x1 x2]);
ylim=([y1 y2]);
ax = gca;
 %改变x轴和y轴坐标间隔显示
set(gca,'XTick',linspace(x1,x2,6));
set(gca,'YTick',linspace(y1,y2,6));
xticklabels({'1e-8','1e-7','1e-6','1e-5','1e-4','1e-3'});
yticklabels({'1e-8','1e-7','1e-6','1e-5','1e-4','1e-3'});
colorbar
axis tight

你可能感兴趣的:(Matlab编程与绘图)