how plot a correct interpolation with surf plot?

27 views (last 30 days)
Im using the code:
load A_PRE.mat; %find it attached
load Aelec.mat; %find it attached
figure(1)
X=A_PRE(:,1); Y=A_PRE(:,4); Z=A_PRE(:,5);
[xq,yq]=meshgrid(linspace(min(X),max(X),100),linspace(min(Y),max(Y),100));
zq=griddata(X,Y,Z,xq(:),yq(:),'linear');
[c,h]=contourf(xq,yq,reshape(zq,100,100),'LineStyle','none','LevelStep',100);
hold on
plot(cotas(:,1),cotas(:,2),'k','linewidth',4) %elevation data
hold on
scatter(X,Y,'filled','k')
figure(2)
plot3(Aelec(:,2),Aelec(:,3),Aelec(:,4),'k','linewidth',2);
hold on
scatter3(A_PRE(:,2),A_PRE(:,3),A_PRE(:,4),'filled','k')
hold on
level=A_PRE;
x = level(:,2); y = level(:,3); z = level(:,4); c = level(:,5);
yv = linspace(min(level(:,3)), max(level(:,3)), 100);
zv = linspace(min(level(:,4)), max(level(:,4)), 100);
[Y,Z] = meshgrid(yv, zv);
X = griddata(y,z,x,Y,Z,'cubic');
C = griddata(y,z,c,Y,Z,'cubic');
yA = Aelec(:,3); zA = Aelec(:,4);
in = inpolygon(Y,Z,yA(1:end-1),zA(1:end-1));
Z(in) = nan;
surf(X, Y, Z, 'cdata',C);
shading interp
for plotting the contour:
using surf function and using X and Y coordinates for its location, but when a use the same view of the 2D contour it seems to be interpolated wrong, any ideas about what could be my mistake? how could a I get a contour plot for figure 2 as similar than figure 1.
As you see, the yellow values seems to be affected with the other data using surf plot, any idea about how to obtain the interpolation plot as similar as fig 1? Thanks in advance

Accepted Answer

darova
darova on 31 Mar 2020
Edited: darova on 31 Mar 2020
It happens because of different scales of data. Don't know why MATLAB get confused about it (even linear interpolation)
scale and center your data before interpolation
sc = (max(z)-min(z))/(max(y)-min(y));
y = y - mean(y);
x = x - mean(x);
y = y*sc;
x = x*sc;
yv = linspace(min(y), max(y), 50);
zv = linspace(min(z), max(z), 50);
[Y,Z] = meshgrid(yv, zv);
X = griddata(y,z,x,Y,Z,'cubic');
C = griddata(y,z,c,Y,Z,'cubic');
  3 Comments
darova
darova on 31 Mar 2020
You can scale them back
sc = (max(z)-min(z))/(max(y)-min(y));
y0 = mean(y);
x0 = mean(x);
y = (y-y0)*sc;
x = (x-x0)*sc;
yv = linspace(min(y), max(y), 50);
zv = linspace(min(z), max(z), 50);
[Y,Z] = meshgrid(yv, zv);
X = griddata(y,z,x,Y,Z,'cubic'); % made a mistake before
C = griddata(y,z,c,Y,Z,'cubic'); % made a mistkae before
Y = Y/sc + y0; % scale back
X = X/sc + x0;
surf(X,Y,Z,'cdata',C) % plot original
Philippe Corner
Philippe Corner on 31 Mar 2020
Oh man, as we say in my country. you are crack! haha you rock man, amazing, thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!