【matlab】绘制曲面图

news/2024/10/18 3:36:26/

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

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 theunderlying 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 withconstant columns. Similarly, Yq can be a column vector and itspecifies 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 gridformed 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 defaultis linear interpolation.  Available methods are:'nearest' - nearest neighbor interpolation'linear'  - bilinear interpolation'spline'  - spline interpolation'cubic'   - bicubic interpolation as long as the data isuniformly 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 scalarvalue for Vq outside of the domain created by X and Y.  Thus, Vq willequal EXTRAPVAL for any value of Yq or Xq which is not spanned by Yor 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 andplaid (as if they were created using MESHGRID).  If you provide twomonotonic 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 andinterpolate 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

http://www.ppmy.cn/news/431922.html

相关文章

企业数字化升级要花多少钱?

摘要 随着科技的不断发展和企业竞争的日益激烈&#xff0c;数字化升级已成为企业保持竞争力和实现可持续发展的关键要素。然而&#xff0c;企业数字化升级所需的成本因各种因素而异&#xff0c;本文将介绍一些影响成本的主要因素&#xff0c;并提供一些常见的数字化升级投资范例…

第四十八章 千角兽

“呵呵。噢&#xff0c;对了&#xff0c;昨天我去找候坡老师谈判时&#xff0c;却被你的数据吓到了。”弗洛格老师点了一下巴哥奔的临鸾&#xff0c;“果然&#xff0c;-3236&#xff0c;告诉我&#xff0c;你是如何做到顺利完成魔鬼任务并实现超越的&#xff1f;说实话&#x…

三星s6经常信号无服务器,三星手机出现信号不好的详细解决方法

很多使用三星手机的朋友会发现出现手机信号不好的问题&#xff0c;下面就以三星9300为例&#xff0c;教你手机信号不好的详细解决方法&#xff01; 方法一&#xff1a;通过GSM网络首选网络设置 所谓GSM&#xff0c;中文名为全球移动通讯系统&#xff0c;俗称“全球通”&#xf…

google play电子市场和gmail如何安装在国产手机、三星手机、摩托手机里

买了一些手机&#xff0c;像三星和摩托以及国产手机&#xff0c;基本上把google的一些服务gmail,日历&#xff0c;google play电子市场都给砍掉了&#xff0c;感觉很不爽&#xff0c;毕竟谷歌的服务很好用&#xff0c;特别是gmail很多联系人可以 同步到手机上&#xff0c;所以就…

iPhone 8无线充电会很好,但这方面三星快充手机更好,你承认吗?

关于无线充电&#xff0c;苹果推出的iPhone本周再次引人注目&#xff0c;Slashleaks发布了针对“iPhone 8”设计的无线充电模块的照片。 无线充电将是苹果iPhone的一个新功能&#xff0c;这当然是未来的。它正在充电...没有有线连接。除此之外&#xff0c;还有就是为无线充电基…

三星手机投屏到Windows10电脑上教程(利用自带连接软件)

三星手机投屏到Windows10电脑上教程 投屏需要条件投屏操作步骤打开window10设置点开-系统-选到-投影到此电脑如上图设置&#xff08;最好一模一样&#xff0c;不然会出现连不上的情况&#xff09;&#xff0c;然后退出电脑上打开你的连接应用&#xff0c;确认电脑是否准备好手机…

盘点开源ChatGPT建立的私有知识库

ChatGPT 可以落地的一个行业就是建立私有知识库&#xff0c;将ChatGPT落地TO B行业&#xff0c;可基于ChatGPT和私有数据构建智能知识库和个性化AI。 这个应该是ChatGPT 最热的一个创业方向。 可能出现的产品&#xff0c;有智能AI客服、企业内部/外部知识库、个人知识库&…

电信号标定中,为什么多采用串行标定,而不是并行标定?

在电信号标定中&#xff0c;串行标定和并行标定是两种不同的标定方法。串行标定是指逐个测量和校准电气设备的方法&#xff0c;而并行标定则是指同时测量和校准多个电气设备的方法。在实际应用中&#xff0c;串行标定通常比并行标定更常用&#xff0c;这主要有以下几点原因&…