Contour plot for a non rectangular object

I have this case that I worked out in another software.
In the attached file, you can see the X,Y coordinates. The third column represents the results that I need to plot in a contour silimar to the one in the image above.
I tried several codes, but all of them plot on a rectagular area and/or not showing the same shape for the contour area. For example, the following code:
xyz = xlsread('data.xlsx');
x=xyz(2:end,1);
y=xyz(2:end,2);
z=xyz(2:end,3);
[X,Y]=meshgrid(min(x):max(x),min(y):max(y));
Z=griddata(x,y,z,X,Y);
contour(X,Y,Z)

 Accepted Answer

[X,Y]=meshgrid(x,y) makes a grid where all the x values are the same across the grid, and likewise for yhe y values.
You can fix it by making your own X and Y instead of using meshgrid.
Nx=41; Ny=9;
dx=1; dy=1; %adjust as desired
x=((0:Nx-1)-(Nx-1)/2)*dx; y=((0:Ny-1)-(Ny-1)/2)*dy;
[X,Y]=meshgrid(x,y); %initial approximation
Z1=X+2*Y;
subplot(211); surf(X,Y,Z1);
xlabel('X'); ylabel('Y'); axis equal; view(0,90)
%next: adjust the grid Y-values
Yadj=Y;
for i=1:Nx
if i<=5||i>=37
ymult=1;
elseif i>=32
ymult=(i-25)/12;
elseif i<=10
ymult=(17-i)/12;
else
ymult=0.5;
end
Yadj(:,i)=ymult*Y(:,i);
end
subplot(212); surf(X,Yadj,Z1);
xlabel('X'); ylabel('Yadj'); axis equal; view(0,90)
It works. The code to adjust the y-values is not very elegant, but it demonstrates the possibilities for creating a non-retangular grid.

4 Comments

@Ghazwan, you can use this approach to make a surface plot with polar coordinates:
Nh=36; Nr=9; %grid dimensions: angle, radius
R=2; %max radius, adjust as desired
dr=R/(Nr-1); %delta r
theta=(0:Nh)*2*pi/Nh; %vector of angles
r=0:dr:R; %vector of radii
[Radius,Theta]=meshgrid(r,theta); %grid arrays
Z=cos(Theta).*sin(pi*Radius/R)/2; %compute Z=Z(r,theta)
X=Radius.*cos(Theta); %X cooridnates for plotting
Y=Radius.*sin(Theta); %Y coordinates for plotting
surf(X,Y,Z); %plot the surface
xlabel('X'); ylabel('Y'); zlabel('Z'); axis equal;
It is a start for polar grid plot.
You can also make a grid on an annulus, by a slight modificaiton of the code above:
Nh=72; Nr=9; %grid dimensions: angle, radius
Rin=1; Rout=2; %max radius, adjust as desired
dr=(Rout-Rin)/(Nr-1); %delta r
theta=(0:Nh)*2*pi/Nh; %vector of angles
r=Rin:dr:Rout; %vector of radii
[Radius,Theta]=meshgrid(r,theta); %grid arrays
Z=cos(2*Theta).*sin(pi*(Radius-Rin)/(Rout-Rin)); %compute Z=Z(r,theta)
X=Radius.*cos(Theta); %X cooridnates for plotting
Y=Radius.*sin(Theta); %Y coordinates for plotting
surf(X,Y,Z,'EdgeColor','none'); %plot surface
xlabel('X'); ylabel('Y'); zlabel('Z'); axis equal; view(0,90)
Try it.
Thank you, William. This is excellent.
Your code gave me a good idea of what to do next. After I generate the proper coordinate array, I will interpolate the original contour data into the new array. This way, I can ensure that the contour data will get appropriately mapped into the new coordinate points.
@Ghazwan, you are welcome. Your plan sounds good.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!