Surface plots inside loop
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I am working on a code that approximates a general bivariate function using a piecewise linear bivariate function. I am able to retrieve the data and plot it, my code works as follows:
It plots one subdomain at a time, as a surface plot. The plot is put inside a loop and so during each, iteration a new subdomain is plotted.
The issue here is that the final plot is displayed with discontinuities as shown in the figure attached.
Kindly help me remove those discontinuities, so as to make the final plot solid or suggest any alternate idea that would work.
采纳的回答
更多回答(3 个)
KSSV
2019-8-19
YOu can follow a samll demo code given here:
[X,Y,Z] = peaks(100) ;
surf(X,Y,Z)
%% Make surface discontinuous for demo
idx = meshgrid(10:10:100) ;
for i = 1:idx
Z(idx(:,i),:) = NaN ;
Z(:,idx(:,i)) = NaN ;
end
surf(X,Y,Z)
%% Have a continuous (x,y,z) data
idx = ~isnan(Z) ;
c = [X(idx) Y(idx) Z(idx)] ;
%% Make c continuous
x = c(:,1) ; y = c(:,2) ; z = c(:,3) ;
xi = linspace(min(x),max(x),100) ;
yi = linspace(min(y),max(y),100) ;
[X,Y] = meshgrid(xi,yi) ;
Z = NaN(size(X)) ;
% get nearest neighbors of new X, Y from (x,y)
idx = knnsearch([X(:) Y(:)],[x y]) ;
Z(idx) = z ;
%% fill gaps by interpolation
F = scatteredInterpolant(x,y,z) ;
idx = isnan(Z) ;
Z(idx) = F(X(idx),Y(idx)) ;
figure
surf(X,Y,Z)
YOu can also use griddata, fillgaps, fillmissing.
Bruno Luong
2019-8-20
编辑:Bruno Luong
2019-8-20
Assuming your xdata and ydata are sorted. My code doest not make new resampling of x and y, or interpolation, I just stitch them together
% Read your data
xdata = xlsread('debug.xlsx','B1:U4');
ydata = xlsread('debug.xlsx','B6:U9');
X{1} = xdata(1,:); X{2} = xdata(2,:); X{3} = xdata(3,:); X{4} = xdata(4,:);
Y{1} = ydata(1,:); Y{2} = ydata(2,:); Y{3} = ydata(3,:); Y{4} = ydata(4,:);
F{1} = xlsread('debug.xlsx','B11:U30');
F{2} = xlsread('debug.xlsx','B32:U51');
F{3} = xlsread('debug.xlsx','B53:U72');
F{4} = xlsread('debug.xlsx','B74:U93');
% Stitching the sub-rectangule data
Xu = unique([X{:}]);
Yu = unique([Y{:}]);
Fu = nan(length(Yu),length(Yu));
for k=1:length(F)
[~,ix] = ismember(X{k},Xu);
[~,iy] = ismember(Y{k},Yu);
Fu(iy,ix) = F{k};
end
figure;
surf(Xu,Yu,Fu)
6 个评论
Bruno Luong
2019-8-20
load('datareqd.mat')
% Stitching the sub-rectangule data
Xu = unique([X{:}]);
Yu = unique([Y{:}]);
Fu = nan(length(Yu),length(Yu));
for k=1:length(F)
[~,ix] = ismember(X{k},Xu);
[~,iy] = ismember(Y{k},Yu);
Fu(iy,ix) = F{k};
end
Fu = fillmissing(Fu,'linear');
close all
surf(Xu,Yu,Fu)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!