3d Plotting Presentation-V3
3d Plotting Presentation-V3
1
Line Plots in 3D
• Points in space are usually
given by ordered triples
(x ,y ,z ). One could view
this as "position vectors"
starting at the point
(0, 0, 0) and ending at the
points (x, y, z). Then a line
is just a succession of
these end points.
• Succession of points
producing lines in three-
dimensional space can be
plotted with the
plot3(x,y,z)function
.
2
The Code:
% LinePlot3D.m
% Experiments in 3D
3
3D Visualization
• I have the one belief that our education is mostly taught and
learned in 2D (an with scalar mathematics), therefore to
develop a bit of a 3D flavor we need to start by figuring it out
popular images in 2D which may upgrade ‘effortlessly’ to 3D
view, so not to break too hard with our ‘habits’
• In 3D we need (x,y,z):
𝑥 = 𝑟 cos 𝑡
𝑦 = 𝑟 sin(𝑡)
𝑧=𝑡
https://www.youtube.com/
watch?v=3k4EeezUIFQ CirclePlot2D.m
6
3D plot of a Circle
% CirclePlot3D.m % continued
% By parametric equation for the circle xlabel('x-axis');
% Plot a circle with radius=1 ylabel('y-axis');
clc, clear, clf zlabel('z-axis');
r=1; title('z=t');
t=linspace(0,2*pi,360); % [angle] grid on
x=r*cos(t);
y=r*sin(t);
% z(1:numel(x))=3;
% z=1+sin(t); % activate one z statement
% z=t;
plot3(x,y,z);
7
Up and down z:
z=1+sin(t);
Constant z: Increasing z:
z(1:numel(x))=3; z=t;
8
QUIZ
Exercise
Produce a pile of circles of radius=1
and center at (x=0,y=0) at different
levels of z=1,1.25,1.5,1.75,2
9
Spiral Plot: 2D
% SpiralPlot2D.m
% Using parametric equation
% for the modified circle
clc, clear, clf
13
Quiz-Solution
% tresD003.m
clc, clear, clf
t = 0:pi/50:10*pi;
x=exp(-0.02*t).*sin(t);
y=exp(-0.02*t).*cos(t);
z=10*pi-t;
plot3(x,y,z);
%plot3(exp(-0.02*t).*sin(t), exp(-0.02*t).*cos(t), t)
xlabel('x-axis'), ylabel('y-axis'), zlabel('z-axis');
grid on
14
Mesh Plots
The function z = f (x, y) represents a surface when plotted on xyz axes
Construct x and y axes as two vectors:
x = xmin:xspacing:xmax
y = ymin:yspacing:ymax
The mesh(X,Y,Z) function generates the surface
plots.
A grid of points in the xy plane are generated with:
[X,Y] = meshgrid(x,y)
where
x = xmin:xspacing:xmax
y = ymin:yspacing:ymax
Explanation: Coordinates of a rectangular grid with
one corner at (xmin, ymin) and the opposite corner
Resulting 2D matrices X and Y contain the at (xmax, ymax) are generated. Each rectangular
coordinate pairs of every point in the grid. These panel in the grid will have a width equal to xspacing
and a depth equal to yspacing.
pairs are used to evaluate the math function z=f
(x, y) in the form of a 2D array equation: Z=f(X,Y) 15
Example:
Create a surface plot (mesh plot) of the function:
2 2
− 𝑥−𝑦 +𝑦 2
𝑧 = 𝑥𝑒
For
−2 ≤ 𝑥 ≤ 2 and
−2 ≤ 𝑦 ≤ 2 with spacing of 0.1
16
Mesh Plot
% MeshPlot3D.m
The matrix Z in a 3-D view. Z is interpreted as heights with respect to the x-y plane. The X values
correspond to the column indices of Z and the Y values correspond to the row indices of Z.
17
% Construct the grid in the plane x-y INDICES representation into TWO 2D arrays
x=-2:1:2; y=x;
[X,Y] = meshgrid(x,y);
Col-1
Col-2
Col-3
Col-4
Col-5
surface(X,Y,Z);
surfc(X,Y,Z);
19
Contour plot at the bottom of a surface plot
Contour Plots
• Topographic plots show the contours of the land by means of constant elevation
lines.
• These lines are also called contour lines, and such a plot is called a contour plot.
• If you walk along a contour line, you remain at the same elevation. Contour plots
can help you visualize the shape of a function.
• The contour(X,Y,Z) function is used.
• You use this function the same way you use the mesh function; that is, first use
meshgrid to generate the grid [X,Y], after that, generate the function values
[z=f(x,y)], then generate contour(X,Y,Z).
20
Contour
% contourPlot3D_v2.m
clc, clear, clf
% Construct the grid in the plane x-y
x=-2:0.1:2; y=x;
[X,Y] = meshgrid(x,y);
% contour plot
[C,h]=contour(X,Y,Z);
clabel(C,h);
xlabel('x'),ylabel('y'),zlabel('z');
clabel(C,h) labels the current contour plot with rotated text inserted into each contour line. The
contour lines must be long enough to fit the label, otherwise clabel does not insert a label. C is a two
row matrix that contains the data that define the contour lines. Here h is the contour object (lines) 21
Scatter Plot
N = 1000;
x = rand(N,1);
y = rand(N,1);
z1 = 20+0.5*randn(N, 1);
z2 = 10+0.5*randn(N, 1);
figure(1)
scatter3(x, y, z1, '.b')
hold on
scatter3(x, y, z2, '.r')
hold off
grid on
xlabel('x')
ylabel('y')
zlabel('z')
22
fplot3: Used to Plot functions (as opposite to discrete triplets)
% fplot3Example.m
% Plot an spiral
clc, clear, clf
xt=@(t) sin(t);
yt=@(t) cos(t);
zt=@(t) t;
fplot3(xt,yt,zt) % not available in R2015b
SYNTAX
fplot3(funx, funy, funz)
Where funx, funy, funz are function names or function handlers.
Fplot3 makes 3D plots in a default range of [-5,5] of the
arguments.
https://www.mathworks.com/help/matlab/ref/fplot3.html
23
References
On the Parametric Equations
• http://tutorial.math.lamar.edu/Classes/CalcII/ParametricEqn.aspx
On the equations of lines in 3D space:
• http://tutorial.math.lamar.edu/Classes/CalcIII/EqnsOfLines.aspx
24