How can I plot a 3D-plane in Matlab? - matlab

I would like to plot a plane using a vector that I calculated from 3 points where:
pointA = [0,0,0];
pointB = [-10,-20,10];
pointC = [10,20,10];
plane1 = cross(pointA-pointB, pointA-pointC)
How do I plot 'plane1' in 3D?

Here's an easy way to plot the plane using fill3:
points=[pointA' pointB' pointC']; % using the data given in the question
fill3(points(1,:),points(2,:),points(3,:),'r')
grid on
alpha(0.3)

You have already calculated the normal vector. Now you should decide what are the limits of your plane in x and z and create a rectangular patch.
An explanation : Each plane can be characterized by its normal vector (A,B,C) and another coefficient D. The equation of the plane is AX+BY+CZ+D=0. Cross product between two differences between points, cross(P3-P1,P2-P1) allows finding (A,B,C). In order to find D, simply put any point into the equation mentioned above:
D = -Ax-By-Cz;
Once you have the equation of the plane, you can take 4 points that lie on this plane, and draw the patch between them.
normal = cross(pointA-pointB, pointA-pointC); %# Calculate plane normal
%# Transform points to x,y,z
x = [pointA(1) pointB(1) pointC(1)];
y = [pointA(2) pointB(2) pointC(2)];
z = [pointA(3) pointB(3) pointC(3)];
%Find all coefficients of plane equation
A = normal(1); B = normal(2); C = normal(3);
D = -dot(normal,pointA);
%Decide on a suitable showing range
xLim = [min(x) max(x)];
zLim = [min(z) max(z)];
[X,Z] = meshgrid(xLim,zLim);
Y = (A * X + C * Z + D)/ (-B);
reOrder = [1 2 4 3];
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'b');
grid on;
alpha(0.3);

Here's what I came up with:
function [x, y, z] = plane_surf(normal, dist, size)
normal = normal / norm(normal);
center = normal * dist;
tangents = null(normal') * size;
res(1,1,:) = center + tangents * [-1;-1];
res(1,2,:) = center + tangents * [-1;1];
res(2,2,:) = center + tangents * [1;1];
res(2,1,:) = center + tangents * [1;-1];
x = squeeze(res(:,:,1));
y = squeeze(res(:,:,2));
z = squeeze(res(:,:,3));
end
Which you would use as:
normal = cross(pointA-pointB, pointA-pointC);
dist = dot(normal, pointA)
[x, y, z] = plane_surf(normal, dist, 30);
surf(x, y, z);
Which plots a square of side length 60 on the plane in question

I want to add to the answer given by Andrey Rubshtein, his code works perfectly well except at B=0. Here is the edited version of his code
Below Code works when A is not 0
normal = cross(pointA-pointB, pointA-pointC);
x = [pointA(1) pointB(1) pointC(1)];
y = [pointA(2) pointB(2) pointC(2)];
z = [pointA(3) pointB(3) pointC(3)];
A = normal(1); B = normal(2); C = normal(3);
D = -dot(normal,pointA);
zLim = [min(z) max(z)];
yLim = [min(y) max(y)];
[Y,Z] = meshgrid(yLim,zLim);
X = (C * Z + B * Y + D)/ (-A);
reOrder = [1 2 4 3];
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'r');
grid on;
alpha(0.3);
Below Code works when C is not 0
normal = cross(pointA-pointB, pointA-pointC);
x = [pointA(1) pointB(1) pointC(1)];
y = [pointA(2) pointB(2) pointC(2)];
z = [pointA(3) pointB(3) pointC(3)];
A = normal(1); B = normal(2); C = normal(3);
D = -dot(normal,pointA);
xLim = [min(x) max(x)];
yLim = [min(y) max(y)];
[Y,X] = meshgrid(yLim,xLim);
Z = (A * X + B * Y + D)/ (-C);
reOrder = [1 2 4 3];
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'r');
grid on;
alpha(0.3);

Related

Plotting a 2d vector on 3d axes in matlab

I have three vectors x,y,t. For each combination x,y,t there is a (u,v) value associated with it. How to plot this in matlab? Actually I'm trying to plot the solution of 2d hyperbolic equation
vt = A1vx + A2vy where A1 and A2 are 2*2 matrices and v is a 2*1 vector. I was trying scatter3 and quiver3 but being new to matlab I'm not able to represent the solution correctly.
In the below code I have plot at only a particular time-level. How to show the complete solution in just one plot? Any help?
A1 = [5/3 2/3; 1/3 4/3];
A2 = [-1 -2; -1 0];
M = 10;
N = 40;
delta_x = 1/M;
delta_y = delta_x;
delta_t = 1/N;
x_points = 0:delta_x:1;
y_points = 0:delta_y:1;
t_points = 0:delta_t:1;
u = zeros(M+1,M+1,N+1,2);
for i=1:M+1,
for j=1:M+1,
u(i,j,1,1) = (sin(pi*x_points(i)))*sin(2*pi*y_points(j)) ;
u(i,j,1,2) = cos(2*pi*x_points(i));
end
end
for j=1:M+1,
for t=1:N+1,
u(M+1,j,t,1) = sin(2*t);
u(M+1,j,t,2) = cos(2*t);
end
end
for i=1:M+1
for t=1:N+1
u(i,1,t,1) = sin(2*t);
u(i,M+1,t,2) = sin(5*t) ;
end
end
Rx = delta_t/delta_x;
Ry = delta_t/delta_y;
for t=2:N+1
v = zeros(M+1,M+1,2);
for i=2:M,
for j=2:M,
A = [(u(i+1,j,t-1,1) - u(i-1,j,t-1,1)) ; (u(i+1,j,t-1,2) - u(i-1,j,t-1,2))];
B = [(u(i+1,j,t-1,1) -2*u(i,j,t-1,1) +u(i-1,j,t-1,1)) ; (u(i+1,j,t-1,2) -2*u(i,j,t-1,2) +u(i-1,j,t-1,2))];
C = [u(i,j,t-1,1) ; u(i,j,t-1,2)];
v(i,j,:) = C + Rx*A1*A/2 + Rx*Rx*A1*A1*B/2;
end
end
for i=2:M,
for j=2:M,
A = [(v(i,j+1,1) - v(i,j-1,1)) ; (v(i,j+1,2) - v(i,j-1,2)) ];
B = [(v(i,j+1,1) - 2*v(i,j,1) +v(i,j-1,1)) ; (v(i,j+1,2) - 2*v(i,j,2) +v(i,j-1,2))];
C = [v(i,j,1) ; v(i,j,2)];
u(i,j,t,:) = C + Ry*A2*A/2 + Ry*Ry*A2*A2*B/2;
end
end
if j==2
u(i,1,t,2) = u(i,2,t,2);
end
if j==M
u(i,M+1,t,1) = u(i,M,t,1);
end
if i==2
u(1,j,t,:) = u(2,j,t,:) ;
end
end
time_level = 2;
quiver(x_points, y_points, u(:,:,time_level,1), u(:,:,time_level,2))
You can plot it in 3D, but personally I think it would be hard to make sense of.
There's a quiver3 equivalent for your plotting function. z-axis in this case would be time (say, equally spaced), and z components of the vectors would be zero. Unlike 2D version of this function, it does not support passing in coordinate vectors, so you need to create the grid explicitly using meshgrid:
sz = size(u);
[X, Y, Z] = meshgrid(x_points, y_points, 1:sz(3));
quiver3(X, Y, Z, u(:,:,:,1), u(:,:,:,2), zeros(sz(1:3)));
You may also color each timescale differently by plotting them one at a time, but it's still hard to make sense of the results:
figure(); hold('all');
for z = 1:sz(3)
[X, Y, Z] = meshgrid(x_points, y_points, z);
quiver3(X, Y, Z, u(:,:,z,1), u(:,:,z,2), zeros([sz(1:2),1]));
end

Matlab: PDE toolbox, get length of each element on the boundary

I setup and meshed a domain using Matlab's PDE toolbox. Along the boundary, is there someway to get the length of each element of the mesh? And the flux in the normal direction? (the bc are Dirichlet)
Edit:
See example code
RMax = 20;
RL = 1;
RU = 0.5;
HN = 5;
HL = 2;
HTT = 3;
HU = 1.5;
VL = -150;
p = [RL,0;RL,HN;0,HN+HL;0,HN+HL+HTT;RU,HN+HL+HTT+HU;RMax,HN+HL+HTT+HU;RMax,0];
t = [1;1;0;1;1;0;0];
v = [VL;VL;0;0;0;0;0];
dx = 0.5;
dy = 0.5;
bc = cell(size(t));
for i = 1:length(t)
if t(i) == 0
bc{i} = {'u', v(i)};
elseif t(i) == 1
bc{i} = {'g', v(i), 'q', 1};
else
error('Unrecognized boundary condition type.')
end
end
model = createpde;
gd = [2; size(p,1); p(:,1) ; p(:,2)];
ns = char('domain')';
sf = 'domain';
g = decsg(gd,sf,ns);
geometryFromEdges(model,g);
generateMesh(model, 'Hmax', min([dx,dy])/3, 'MesherVersion','R2013a');
for i = 1:size(bc,1)
applyBoundaryCondition(model, 'Edge', i, bc{i}{:});
end
u = assempde( model , 'x' , 0 , 0 );
pdemesh(model)
Edit: 2015-12-17 18:54 GMT
There are 2 points in the e shown at 1 and 2 in the figure below. I want to know the coordinate of 3 so I know which direction is into the domain.
You can answer your first question by using meshToPet to convert to [P,E,T] form:
[p,e,t] = meshToPet(model.Mesh);
x1 = p(1,e(1,:)); % x-coordinates of first point in each mesh edge
x2 = p(1,e(2,:)); % x-coordinates of second point in each mesh edge
y1 = p(2,e(1,:)); % y-coordinates of first point in each mesh edge
y2 = p(2,e(2,:)); % y-coordinates of second point in each mesh edge
% Plot first points of mesh edge
plot(x1,y1,'b.-',x1(1),y1(1),'go',x1(end),y1(end),'ro');
% Euclidean distance between first and second point in each edge
d = sqrt((x1-x2).^2+(y1-y2).^2);
I'm assuming you just want the lengths of the mesh edge/boundary. You can use similar methods to get the lengths of every single triangle using the t matrix.
As far as flux goes, there's pdecgrad. I think the following may work:
...
c = 'x';
u = assempde(model, c, 0, 0);
[p,e,t] = meshToPet(model.Mesh);
[cgxu,cgyu] = pdecgrad(p,t,c,u);

Sequence of dots in matlab and psychotoolbox

How would i display one by one dots that are in a 3x3 matrix such as in the code below?
I would like to have dot1 appears in position [x1,y1] of the grid for a time t1, then dot2 to appears in position [x2,y2] of the grid for a time t2. Only one dot is being shown at each time.
Thanks for help
%grid
dim = 1
[x, y] = meshgrid(-dim:1:dim, -dim:1:dim);
pixelScale = screenYpixels / (dim * 2 + 2);
x = x .* pixelScale;
y = y .* pixelScale;
% Calculate the number of dots
numDots = numel(x);
% Make the matrix of positions for the dots.
dotPositionMatrix = [reshape(x, 1, numDots); reshape(y, 1, numDots)];
% We can define a center for the dot coordinates to be relaitive to.
dotCenter = [xCenter yCenter];
dotColors = [1 0 0];
dotSizes = 20;
Screen('DrawDots', window, dotPositionMatrix,...
dotSizes, dotColors, dotCenter, 2);
I think you want something like this?
%positions of each successive dots:
x_vec = [1,2,3,1,2,3,1,2,3];
y_vec = [1,1,1,2,2,2,3,3,3];
%wait times in sec for each dot:
wait_times = [1,1,2,1,1,2,1,1,2]
dotColor = [1 0 0];
dotSize = 400;
num_dots = length(x_vec);
for i = 1:num_dots
scatter(x_vec(i),y_vec(i),dotSize,dotColor,'filled');
xlim([0,max(x_vec)])
ylim([0,max(y_vec)])
pause(wait_times(i));
end

How do I plot an Ellipsoid using Matlab? by parametric equation

I'm trying to solve this problems:
Build the surf plot of the ellipsoid when a=1, b=1.5, c=2 with:
z = c*(1-(x^2)/(a^2)-(y^2)/(b^2))^0.5;
Use the coordinate transformation when (0a and b and 22 values of t.
x=a*cos(t);
y=b*sin(t);
a = 1;
b = 0.5;
c = 2;
t = linspace(0,2*pi,22);
p = linspace(0,pi,22);
[T,P] = meshgrid(t,p);
x = a*cos(T).*cos(P);
y = b*cos(T).*sin(P);
z = c*sin(T);
figure,surf(x,y,z)
This solution uses the parameterization for the ellipsoid. 0<=t<=2*pi and 0<=p<=pi
x = acos(t)cos(p)
y = bcos(t)sin(p)
z = cos(t)

Finding the intersection points of ray-5th order polynomial

I am doing ray tracing and I have A screen described in the world coordinates as Matrices(I had before the X,Y,Z in the screen coordinates and by using transformation and rotation I got it in the world coordinates)
Xw (NXM Matrix)
Yw (NXM Matrix)
Zw (I have got this polynomial (5th order polynomial)by fitting the 3D data Xw and Yw. I have it as f(Xw,Yw))
I have the rays equations too described as usual:
X = Ox + t*Dx
Y = Oy + t*Dy
Z = Oz + t*Dz %(O is the origin point and D is the direction)
So what I did is that I replaced the X and Y in the Polynomial equation f(Xw,Yw) and solved it for t so I can then get the intersection point.
But apparently the method that I used is wrong(The intersection points that I got were somewhere else).
Could any one please help me and tell me what is the mistake. Please support me.
Thanks
This is part of the code:
X_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(1,1) + ScreenYCoordinates.*Rotation_matrix_screen(1,2) + ScreenZCoordinates.*Rotation_matrix_screen(1,3) + Zerobase_scr(1);
Y_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(2,1) + ScreenYCoordinates.*Rotation_matrix_screen(2,2) + ScreenZCoordinates.*Rotation_matrix_screen(2,3) + Zerobase_scr(2);
Z_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(3,1) + ScreenYCoordinates.*Rotation_matrix_screen(3,2) + ScreenZCoordinates.*Rotation_matrix_screen(3,3) + Zerobase_scr(3); % converting the screen coordinates to the world coordinates using the rotation matrix and the translation vector
polymodel = polyfitn([X_World_coordinate_scr(:),Y_World_coordinate_scr(:)],Z_World_coordinate_scr(:),5); % using a function from the MAtlab file exchange and I trust this function. I tried it different data and it gives me the f(Xw,Yw).
ScreenPoly = polyn2sym(polymodel); % Function from Matlab file exchange to give the symbolic shape of the polynomial.
syms X Y Z t Dx Ox Dy Oy oz Dz z;
tsun = matlabFunction(LayerPoly, 'vars',[X,Y,Z]); % just to substitue the symboles from X , Y and Z to (Ox+t*Dx) , (Oy+t*Dy) and (Oz+t*Dz) respectively
Equation = tsun((Ox+t*Dx),(Oy+t*Dy),(Oz+t*Dz));
Answer = solve(Equation,t); % solving it for t but the equation that it is from the 5th order and the answer is RootOf(.... for z)
a = char(Answer); % preparing it to find the roots (Solutions of t)
R = strrep(a,'RootOf(','');
R1 = strrep(R,', z)','');
b = sym(R1);
PolyCoeffs = coeffs(b,z); % get the coefficient of the polynomail
tfun = matlabFunction(PolyCoeffs, 'vars',[Ox,Oy,oz,Dx,Dy,Dz]);
tCounter = zeros(length(Directions),1);
NaNIndices = find(isnan(Surface(:,1))==1); %I have NaN values and I am taking them out
tCounter(NaNIndices) = NaN;
NotNaNIndices = find(isnan(Surface(:,1))==0);
for i = NotNaNIndices' % for loop to calc
OxNew = Surface(i,1);
OyNew = Surface(i,2);
OzNew = Surface(i,3);
DxNew = Directions(i,1);
DyNew = Directions(i,2);
DzNew = Directions(i,3);
P = tfun(OxNew,OyNew,OzNew ,DxNew,DyNew,DzNew);
t = roots(P);
t(imag(t) ~= 0) = []; % getting rid of the complex solutions
tCounter(i) = t;
end
Please support
Thanks in advance