griddata 调用方法:ZI = griddata(x,y,z,XI,YI)[XI,YI,ZI] = griddata(x,y,z,XI,YI)[...] = griddata(...,method)[...] = griddata(...,method,options)method 的值 为'linear' -- 则,以三角形为基础的线性内插'cubic' -- 则,以三角形为基础的三次方程内插'nearest' -- 则,用最邻近的点 内插'v4'-- -- 则,MATLAB 4 格点样条函数内插默认'linear' 线性内插三角形为基础,就是按Delaunay方法先找出内插点四周的3个点,构成三角形,内插点在三角形内。然后线性内插,或三次方程内插。'cubic' 和 'v4' 插值结果构成的曲面较光滑,'linear'和 'nearest' 插值结果构成的曲面不光滑不连续。
知道一系列点的坐标如下(1.486,3.059,0.1);(2.121,4.041,0.1);(2.570,3.959,0.1);(3.439,4.396,0.1);(4.505,3.012,0.1);(3.402,1.604,0.1);(2.570,2.065,0.1);(2.150,1.970,0.1);(1.794,3.059,0.2);(2.121,3.615,0.2);(2.570,3.473,0.2);(3.421,4.160,0.2);(4.271,3.036,0.2);(3.411,1.876,0.2);(2.561,2.562,0.2);(2.179,2.420,0.2);(2.757,3.024,0.3);(3.439,3.970,0.3);(4.084,3.036,0.3);(3.402,2.077,0.3);(2.879,3.036,0.4);(3.421,3.793,0.4);(3.953,3.036,0.4);(3.402,2.219,0.4);(3.000,3.047,0.5);(3.430,3.639,0.5);(3.822,3.012,0.5);(3.411,2.385,0.5);(3.103,3.012,0.6);(3.430,3.462,0.6);(3.710,3.036,0.6);(3.402,2.562,0.6);(3.224,3.047,0.7);(3.411,3.260,0.7);(3.542,3.024,0.7);(3.393,2.763,0.7)怎样用MATLAB绘制成三维曲面呢?使用griddata插值A=[1.486,3.059,0.1;2.121,4.041,0.1;2.570,3.959,0.1;3.439,4.396,0.1;4.505,3.012,0.1;3.402,1.604,0.1;2.570,2.065,0.1;2.150,1.970,0.1;1.794,3.059,0.2;2.121,3.615,0.2;2.570,3.473,0.2;3.421,4.160,0.2;4.271,3.036,0.2;3.411,1.876,0.2;2.561,2.562,0.2;2.179,2.420,0.2;2.757,3.024,0.3;3.439,3.970,0.3;4.084,3.036,0.3;3.402,2.077,0.3;2.879,3.036,0.4;3.421,3.793,0.4;3.953,3.036,0.4;3.402,2.219,0.4;3.000,3.047,0.5;3.430,3.639,0.5;3.822,3.012,0.5;3.411,2.385,0.5;3.103,3.012,0.6;3.430,3.462,0.6;3.710,3.036,0.6;3.402,2.562,0.6;3.224,3.047,0.7;3.411,3.260,0.7;3.542,3.024,0.7;3.393,2.763,0.7];x=A(:,1);y=A(:,2);z=A(:,3);scatter(x,y,5,z)%散点图figure[X,Y,Z]=griddata(x,y,z,linspace(1.486,4.271)',linspace(1.604,4.276),'v4');%插值pcolor(X,Y,Z);shading interp%伪彩色图figure,contourf(X,Y,Z) %等高线图figure,surf(X,Y,Z)%三维曲面
x = rand(1,12);y = rand(1,12);z = rand(1,12); % now use some random z axis dataxi = linspace(min(x),max(x),30); % x interpolation pointsyi = linspace(min(y),max(y),30); % x interpolation points[Xi,Yi] = meshgrid(xi,yi); % create grid of x and yZi = griddata(x,y,z,Xi,Yi); % grid the data at Xi,Yi points% Zi = griddata(x,y,z,Xi,Yi, 'linear') % same as above(default)% Zi = griddata(x,y,z,Xi,Yi, 'cubic') % triangle based cubic interpolation% Zi = griddata(x,y,z,Xi,Yi, 'nearest') % triangle based nearest neighbor% Zi = griddata(x,y,z,Xi,Yi, 'invdist') % inverse distance methodmesh(Xi,Yi,Zi)hold onplot3(x,y,z, 'ko') % show original data as wellhold offtitle('Figure 18.10: Griddata Example')
【问题描述】
有三列数字保存于excel表格中,现在想把这三列数字画成三维图。
19.91058 | 209.3531 | 220 |
19.88966 | 209.6685 | 220.9091 |
19.86895 | 209.987 | 221.8182 |
19.84847 | 210.3088 | 222.7273 |
19.82822 | 210.6338 | 223.6364 |
19.80821 | 210.9625 | 224.5455 |
19.78843 | 211.2948 | 225.4545 |
19.7689 | 211.631 | 226.3636 |
19.74961 | 211.9712 | 227.2727 |
19.73058 | 212.3158 | 228.1818 |
19.7118 | 212.665 | 229.0909 |
19.69328 | 213.0191 | 230 |
19.67503 | 213.3783 | 230.9091 |
19.65705 | 213.7431 | 231.8182 |
19.63935 | 214.1137 | 232.7273 |
19.62194 | 214.4907 | 233.6364 |
19.60482 | 214.8746 | 234.5455 |
19.588 | 215.266 | 235.4545 |
19.57148 | 215.6655 | 236.3636 |
19.55529 | 216.074 | 237.2727 |
19.53942 | 216.4924 | 238.1818 |
19.52389 | 216.9218 | 239.0909 |
19.5087 | 217.3637 | 240 |
19.49389 | 217.8194 | 240.9091 |
19.47955 | 218.2883 | 241.8182 |
19.46577 | 218.7697 | 242.7273 |
19.45265 | 219.2627 | 243.6364 |
19.44026 | 219.7668 | 244.5455 |
19.4287 | 220.2813 | 245.4545 |
19.41802 | 220.8059 | 246.3636 |
19.40831 | 221.34 | 247.2727 |
19.39963 | 221.8833 | 248.1818 |
19.39204 | 222.4354 | 249.0909 |
19.38561 | 222.9961 | 250 |
19.38039 | 223.5649 | 250.9091 |
19.37644 | 224.1416 | 251.8182 |
19.37383 | 224.726 | 252.7273 |
19.37259 | 225.3179 | 253.6364 |
19.37279 | 225.9169 | 254.5455 |
19.37448 | 226.523 | 255.4545 |
19.3777 | 227.1359 | 256.3636 |
19.38251 | 227.7555 | 257.2727 |
19.38895 | 228.3815 | 258.1818 |
19.39707 | 229.0139 | 259.0909 |
19.40691 | 229.6525 | 260 |
【解决方法】
只有三类数字是不可能画出三维图的,需要插值转换一下,才能画出网格图或者更好的插值方法得到更光滑的曲面图。
【Matlab代码】
clc; clear all; close all;
A=xlsread('D:BackupMyDocumentsMATLABdata.xls');%读入数据
x=A(:,1);y=A(:,2);z=A(:,3);
t1=linspace(min(x),max(x));
t2=linspace(min(y),max(y));
[X,Y]=meshgrid(t1,t2);
Z=griddata(x,y,z,X,Y,'v4');
figure,surf(X,Y,Z),axis([19.37,19.92,209.3,229.7,220,260]);%三维曲面
xlabel('sigma/mm');grid on;
ylabel('Maximum Stress/MPa');grid on;
zlabel('h1/mm');grid on;
title('关系图');
【最终效果图】