Wichita State University Logo

Math 451: Computational Mathematics

2.6 Specialized Graphics


3.2.1 Specialized Graphics in MATLAB

Power graphics utilities set the MATLAB integrated development environment apart from many computational environments and languages. The standard MATLAB library contains function for

In [9]:
r = @(x,y,x0,y0) sqrt((x-x0).^2 + (y-y0).^2) + eps;
j0 = @(x,y,x0,y0) sin(r(x,y,x0,y0))./r(x,y,x0,y0);
[x,y] = meshgrid([-8:.5:8]);
z = j0(x,y,1,1);
surf(x,y,z)

       

2.6.2 Surfaces

Functions with two inputs and one output $z = f(x,y)$ are called surfaces because we graph the function by plotting the height $z$ above (or below) each point $(x,y)$ in the $xy$-plane in a three dimensional (3d) coordinate system as above. The locus of all points $(x,y,f(x,y))$ is two dimensional in our 3d coordinate system because there are an entire plane of inputs $x$ and $y$.

The Spherical Bessel Function of Order 0 $J_0$ is plotted above using the 3d surface plotting function mesh . The function meshgrid creates two (2) matrices of inputs for such a function. The blocks of MATLAB statements

x = [ -8:.5:8 ];
y = [ -8:.5:8 ];
[x,y] = meshgrid(x,y);
x = [ -8:.5:8 ];
[x,y] = meshgrid(x,x);
[x,y] = meshgrid([-8:.5:8], [-8:.5:8]);
[x,y] = meshgrid(-8:.5:8);

all produce the same two arrays x and y . Each contains the first or second coordinate of a grid or mesh of data points to use for creating the plots of surfaces. For example

In [6]:
[x,y] = meshgrid(1:5)
plot3(x,y,zeros(size(x)),'r*')
axis([0 5 0 5 0 1])
x =

       1              2              3              4              5       
       1              2              3              4              5       
       1              2              3              4              5       
       1              2              3              4              5       
       1              2              3              4              5       


y =

       1              1              1              1              1       
       2              2              2              2              2       
       3              3              3              3              3       
       4              4              4              4              4       
       5              5              5              5              5       


creates a grid of coordinates $(x(j,k), y(j,k))$ for $1\le j\le 5$ and $1\le k\le 5$. Surface plotting functions such as surf and mesh plot the point $(x,y,z)$ from the three matrix inputs. For a line plot , the MATLAB function plot3 connects each point $(x,y,z)$ with a straight line.

The mesh and surf also connect each point $(x,y,z)$ on the surface with a straight line. The area between each set of grid points and the line connecting each to its neighbors forms a parallelogram called a face of the surface. The MATLAB function surf fills the parallelogram with a solid color from a colormap that specifies a range of colors for each height $z$ above or below the $xy$-plane. The face color can also be specified in the arguments of surf , and the colormap can be changed to one chosen by the programmer.

The MATLAB function mesh does not color the faces. Instead the marker used to plot each point on the surface $(x,y,z)$ and the lines connecting them are pigmented according to the colormap.

In [10]:
[x,y] = meshgrid([-8:.5:8]);
z = j0(x,y,1,1);
mesh(x,y,z)

       

2.6.3 Sections of Surfaces

To obtain a plot of an implicit surface such as

$$ \dfrac{x^2}{a^2} + \dfrac{y^2}{b^2} + \dfrac{z^2}{c^2} = 1 $$
you may need to remove some elements of a grid due to the complex values one obtains when computing

$$ \sqrt{1 - \dfrac{x^2}{a^2} - \dfrac{y^2}{b^2}} $$
if $x$ and/or $y$ are too large. One may set the value of $z$ each time $\dfrac{x^2}{a^2} + \dfrac{y^2}{b^2} > 1$ to zero.

In [17]:
ellipse = @(x,y,a,b,c)  c*sqrt(1 - (x/a).^2 - (y/b).^2);
[x,y] = meshgrid(-8:.5:8);
z = ellipse(x,y,6,8,7);
z( (x.^2/6^2 + y.^2/8^2) > 1 ) = 0;
surf(x,y,z)
axis equal
figure
mesh(x,y,z)

       

2.6.4 Contour Graphs

A contour line , isoline , or level curve is a curve in the $xy$-plane for which the output $z = f(x,y) = C$ is a constant $C$. These level curves or level sets can be plotted on the $xy$-plane for regularly space values of the output $z = C$ creating a contour plot .

The MATLAB function peaks returns a grid of x and y values as well as the values of the output z for each grid point. The peaks function help illustrate the graphics functions in MATLAB.

In [19]:
contour(peaks(101),(-7:.5:8))
figure
meshc(peaks)

       

The MATLAB function contour creates a contour plot of the graph described by the matrices $x$, $y$, and $z$ at each constant value in $-7:.5:.8$, that is each value

>> -7:.5:8

ans =

  Columns 1 through 10

   -7.0000   -6.5000   -6.0000   -5.5000   -5.0000   -4.5000   -4.0000   -3.5000   -3.0000   -2.5000

  Columns 11 through 20

   -2.0000   -1.5000   -1.0000   -0.5000         0    0.5000    1.0000    1.5000    2.0000    2.5000

  Columns 21 through 30

    3.0000    3.5000    4.0000    4.5000    5.0000    5.5000    6.0000    6.5000    7.0000    7.5000

  Column 31

    8.0000

Notice that each level curve is pigmented according to the colormap. The MATLAB function meshc create a mesh plot and a contour plot at the base of the coordinate system.

The MATLAB function contour3 plots the level curves for each constant $z=C$ in the plane $z=C$.

In [20]:
contour3(peaks, -7:.25:8)

       

2.6.5 Trajectories in 3 Dimensions

One plots a trajectory or curve in three dimensions with the MATLAB function plot3 . A curve in one dimensional so in this case each coordinate $x$, $y$, and $z$ are dependent variables. One can model such a function of one independent variable $t$ as a system .

$$ \begin{align*} x(t) &= 3\cos(t) \\ y(t) &= 2\sin(t) \\ z(t) &= t; \end{align*} $$
Alternatively we can think of the output of a function $f(t)$ to be a point in three dimensional space with coordinate functions $x(t)$, $y(t)$, $z(t)$

$$ f(t) = \left( 3\cos(t), 2\sin(t), t \right) = \left(x(t), y(t), z(t)\right) $$
Finally we can think of the output of our function to be a vector in three dimensional space

$$ \mathbf{f}(t) = \left\langle 3\cos(t), 2\sin(t), t \right\rangle = \begin{bmatrix} x(t) \\ y(t) \\ z(t) \end{bmatrix} $$

In [1]:
t = linspace(-10,10,1001);
x = 3*cos(t);
y = 2*sin(t);
z = t/4;
plot3(x,y,z)
axis equal

       

A one dimensional curve in 3d space is called a trajectory to imply motion of an object along the path in the direction defined by the independent variable. We can draw a few velocity vectors on the path above to further indicate motion along the trajectory.

In [8]:
t = linspace(-10,10,1001);
x = 3*cos(t);
y = 2*sin(t);
z = t/4;
plot3(x,y,z)
axis equal
tp = linspace(-10,10, 21);
zt = [ 3*cos(tp); 2*sin(tp); tp/4 ].';
zp = [ -3*sin(tp); 2*cos(tp); ones(size(tp))/4 ].';
arrow3(zt,zp,'m');

       

2.6.6 Bar Charts

One can use MATLAB built-in functions to create two and three dimensional bar charts . By default each row of the input array is grouped, so for an $m\times n$ matrix $A$, the expression

bar(A)

will create a set of $m$ groups of $n$ bars.

In [28]:
data = randi([1 9],6,4);
bar(data)

       

A 3 dimensional bar chart set horizontally can also be achieved using built-in MATLAB functions.

In [9]:
data = randi([1 9],6,4);
bar3h(data)

       

2.6.6 Vector Fields

The built-in function quiver creates a plot of vectors as arrows in the $xy$-plane. Notice that gradient vectors are always orthogonal to the level curve at its base.

In [10]:
[x,y,z] = peaks();
[px,py] = gradient(z);
contour(x,y,z,-7:.5:8)
hold on
quiver(x,y,px,py)

       

Creative Commons Logo - White


Your use of this self-initiated mediated course material is subject to our Creative Commons License .


Creative Commons Attribution-NonCommercial-ShareAlike 4.0

Creative Commons Logo - Black
Attribution
You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

Creative Commons Logo - Black
Noncommercial
You may not use the material for commercial purposes.

Creative Commons Logo - Black
Share Alike
You are free to share, copy and redistribute the material in any medium or format. If you adapt, remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.