Surface plots inside loop
7 views (last 30 days)
Show older comments
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.
Accepted Answer
More Answers (3)
KSSV
on 19 Aug 2019
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.
3 Comments
Bruno Luong
on 20 Aug 2019
Edited: Bruno Luong
on 20 Aug 2019
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 Comments
Bruno Luong
on 20 Aug 2019
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)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!