在很多时候我们需要将matlab或其他数据得到的3D散点图,变成一个曲面。这里我们介绍如何使用matlab实现surface。
clear all
clc
close all
% generate the data in domain [-1,1]
N2point2 = 500;
X = -1.0+2*rand(1, N2point2);
Y = -1.0+2*rand(1, N2point2);
u = sin(X.^2+Y.^2);
% construct the scatter solution by point pairs
figure('name','udot')
plot3(X,Y, u,'.');
hold on
% Meshing the scatter solution
% The key pointis that getting the mesh by X and Y. Firstly, unique X and Y
Xcoord = unique(X);
Ycoord = unique(Y);
% secondly, obtain the mesh by Xcoord and Ycoord
[meshX,meshY] = meshgrid(Xcoord,Ycoord);
% Finally, 利用griddata来插值,从xyz生成栅格数据
%最后一个为插值方法,包linear cubic natural nearest和v4等方法
%v4方法耗时长但较为准确
mehsU = griddata(X,Y,u,meshX,meshY,'v4');
% sufacing the solution
figure('name','usurf')
surf(meshX,meshY,mehsU);shading interp
hold on
% maeshing the solution
figure('name', 'umesh')
mesh(meshX,meshY,mehsU)
colorbar;
hold on