# Surface plots inside loop

20 views (last 30 days)
Mahesh M S on 19 Aug 2019
Commented: Bruno Luong on 20 Aug 2019
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.

#### 1 Comment

darova on 19 Aug 2019

darova on 19 Aug 2019
Just collected all data and used griddata()

Show 1 older comment
darova on 19 Aug 2019
SOrry, attached wrong script. Try now
Mahesh M S on 20 Aug 2019
Thank you for the code. It works fine for the above case.
But, the dummy code I provided was a bit specific. I tried to run the code with necessary modifications on a more general case and I am stuck with an error. Kindly help me rectify it.
The code is given below:
%The whole domain is a square with x_min = 0.2, y_min = 0.2, x_max = pi/2 &
%y_max = pi/2
%The whole domain is divided into 7 parts (sample code) and F{i}
%corresponds to the function values of each subdomain
%Here, F{1} is the subdomain bounded by X{1} and Y{1}
% F{2} is the subdomain bounded by X{2} and Y{2}
% F{3} is the subdomain bounded by X{3} and Y{3}
% F{4} is the subdomain bounded by X{4} and Y{4}
% F{5} is the subdomain bounded by X{5} and Y{5}
% F{6} is the subdomain bounded by X{6} and Y{6}
% F{7} is the subdomain bounded by X{7} and Y{7}
X{1} = xdata(1,1:20); X{2} = xdata(2,1:20); X{3} = xdata(3,1:20); X{4} = xdata(4,:);
X{5} = xdata(5,:); X{6} = xdata(6,:); X{7} = xdata(7,:);
Y{1} = ydata(1,1:20); Y{2} = ydata(2,1:20); Y{3} = ydata(3,1:20); Y{4} = ydata(4,:);
Y{5} = ydata(5,:); Y{6} = ydata(6,:); Y{7} = ydata(7,:);
l = 7;
%The requirement is to plot the total domain in a single plot
% Note: The size of X{i} and Y{i} can vary and also value of l can vary.
X = [];
Y = [];
Z = [];
% make all data in one array
for i = 1:7
[X0, Y0] = meshgrid(xdata(i,:),ydata(i,:));
X = [X; X0(:)];
Y = [Y; Y0(:)];
Z = [Z; F{i}(:)];
end
% create new mesh
x1 = linspace(min(xdata(:)), max(xdata(:)),40);
y1 = linspace(min(ydata(:)), max(ydata(:)),40);
[X1, Y1] = meshgrid(x1,y1);
% create set of data
Z1 = griddata(X,Y,Z,X1,Y1);
h = surf(X1,Y1,Z1);
set(h,'LineStyle','none');
I am getting the following error:
Mahesh M S on 20 Aug 2019
Sorry. Modified the code myself. Thank you for your help.

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.

Mahesh M S on 19 Aug 2019
Thanks for the code. But, my code doesnt have gaps as such (as depicted by the top view of the figure shown below) [The figure has been updated. sorry for the error].
Its just discontinuous.
Aso, I can only retrieve data of one subdomain at a time (loop) and thus interpolation is also difficult. A possible way is to extract all data separately from each subdomain and plot later but it takes a lot of time which I am not happy with.
Kinldy help me out with this.
darova on 19 Aug 2019
i can extract it for you
Mahesh M S on 19 Aug 2019
I can provide a dummy code.

Mahesh M S on 19 Aug 2019
Edited: Mahesh M S on 19 Aug 2019
%The whole domain is a square with x_min = 0.2, y_min = 0.2, x_max = pi/2 & y_max = pi/2
%The whole domain is divided into 4 parts (sample code) and F{i}
%corresponds to the function values of each subdomain
%Here, F{1} is the subdomain bounded by X{1} and Y{1}
% F{2} is the subdomain bounded by X{2} and Y{2}
% F{3} is the subdomain bounded by X{3} and Y{3}
% F{4} is the subdomain bounded by X{4} and Y{4}
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,:);
l = 4;
%The requirement is to plot the total domain in a single plot
% Note: The size of X{i} and Y{i} can vary and also value of l can vary.
The reference data has been updated in excel sheet attached herewith.

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
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,:);
% 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)

Bruno Luong on 20 Aug 2019
MATFILE, we are all working with MATLAB here.
Mahesh M S on 20 Aug 2019
The required X, Y and F data ar attached herewith for your reference.
Bruno Luong on 20 Aug 2019
% 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)