MATLAB Answers

0

How do I plot a plane that has points of different values?

Asked by Jing Ci Neo on 5 Jul 2018
Latest activity Commented on by Jing Ci Neo on 6 Jul 2018
I have a 16 by 20 set of points on a plane in 3D, which each has x, y and z coordinates and a slip value. I want to plot the plane with colours at each point representing the slip value. How can I do that?

  0 Comments

Sign in to comment.

Products


Release

R2016a

1 Answer

Answer by Anton Semechko on 5 Jul 2018
 Accepted Answer

Actually, based on the sample picture you provided, you have a total of 17x21 grid points, and total number of cells in the grid is 16x20. You can use 'patch' function to obtain visualization similar to your sample image. Here is an example:
% Generate a M-by-N grid
[M,N]=deal(16,20);
x=linspace(-3,3,N+1);
y=linspace(-3,3,M+1);
[X,Y]=meshgrid(x,y);
V=[X(:) Y(:)]; % xy-coordinates of the grid-points
% Define connectivity of the inidividual cells making-up the grid
Np=numel(X); % total number of grid points
id=reshape(1:Np,[M+1 N+1]); % grid-point indices
i1=id(1:M,1:N);
i2=id(2:(M+1),1:N);
i3=id(2:(M+1),2:(N+1));
i4=id(1:M,2:(N+1));
F=[i1(:) i2(:) i3(:) i4(:)];
% Suppose these are the values of a scalar field measured at the grid points
Q=sinc(sqrt((X/2).^2+Y.^2))+sinc(sqrt((X).^2+(Y/3).^2));
Q=Q(:);
% Simulated grid currently lies in the xy-plane. Lets rotate it to make it
% look more like the grid in your image.
[~,Rz,~,Rx]=xyzRmat(pi*[45 0 30]/180);
V(:,3)=0;
V2=(Rx*Rz*V')';
% Here is one visualization appoach; values at the grid points will be
% interpolated to the interior of the cells
figure('color','w')
subplot(1,2,1)
patch('Faces',F,'Vertices',V2,'EdgeColor','k','FaceVertexCData',Q,'FaceColor','interp');
set(gca,'box','on')
axis tight
view([30 30])
camlight('headlight'), lighting phong
colorbar('Location','NorthOutside')
% Here is another visualization appoach; each grid cell will have a
% constrant value, corresponding to the average of its vertices
subplot(1,2,2)
patch('Faces',F,'Vertices',V2,'EdgeColor','k','FaceVertexCData',Q,'FaceColor','flat');
set(gca,'box','on')
axis tight
view([30 30])
camlight('headlight'), lighting phong
colorbar('Location','NorthOutside')
'xyzRmat' function used in this example is included as an attachment.

  3 Comments

Hello Anton, thank you for the really detailed answer, you're really amazing! I don't really understand the xyzRmat function though. I tried toggling with the inputs and put in ax=90; ay=0; az=0; (updating the code appropriately) but it didn't rotate by 90 degrees about the x axis to become a xz plane as expected. Could you explain the rotation part to me?
'xyzRmat' computes rotation matrices about x-, y-, and z-axes. For example, lets say {Rx,Ry,Rz} are rotation matrices corresponding to rotations of (a_x,a_y,a_z) radians about (x,y,z) axes. You can obtain arbitrary rotations in 3D space using composition of these 3 rotation matrices (i.e., R=Rz*Ry*Rx). Note that the order of multiplication is very important. In this example, a point is first rotated about the x-axis by a_x, then by a_y about the y-axis, and finally by a_z about z-axis. To obtain rotation of 90 degrees (i.e., pi/2 radians) about the x-axis you would use command:
[R,Rz,Ry,Rx]=xyzRmat(pi*[90 0 0]/180);
In this particular case, Rz and Ry will be identity matrices, and resulting rotation matrix will be R = Rz*Ry*Rx = Rx. Applying R to points in xy-plane will map these points to xz-plane, as expected; make sure you are using correct syntax when calling 'xyzRmat'. For example, new coordinates of point (1,1,0) will be:
R=xyzRmat(pi*[90 0 0]/180);
(R*[1;1;0])'
ans =
1 6.12323399573677e-17 1
It's so weird because I typed the exact commands in but it gives a tilted plane (see attached). It's a minor problem (I have what I need already), but I'm curious as to what the problem is.

Sign in to comment.