14.2: Plot 3D Surface and Contour Plots (2024)

  1. Last updated
  2. Save as PDF
  • Page ID
    85207
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

    \( \newcommand{\vectorC}[1]{\textbf{#1}}\)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}}\)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}\)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    By Carey A. Smith

    meshgrid() function

    Surface plots need 2-dimensional grids of x and y coordinates. These are made from 1-dimensional vectors of x and y coordinates using the meshgrid() function. The syntax of meshgrid() is:

    [x2D, y2D] = meshgrid(x1D,y1D);

    Example \(\PageIndex{1}\) Simple meshgrid() Example

    x1D = [0, 1, 2 ,3];
    y1D = [0, 2, 4];
    [x2D, y2D] = meshgrid(x1D,y1D)

    Solution

    x2D =
    0 1 2 3
    0 1 2 3
    0 1 2 3
    y2D =
    0 0 0 0
    2 2 2 2
    4 4 4 4

    As can be seen:

    The x coordinates are repeated in all the rows.

    The x coordinates are repeated in all the columns.

    Watch this video about meshgrid. Note that the presenter uses lower case x & y for 1-dimensional values and upper case X & Y for 1-dimensional values. It is not good practice to use variables names that only differ by lower and upper case. Better is to use different name, like x1D, y1D and x2D, y2D. The important part is 0 to 4:20 minutes.

    .

    mesh() 3D Plot

    The mesh() function creates a wire-frame 3-dimensional plot.

    Example \(\PageIndex{2}\) mesh() 3D Plot

    %% Create a 3D graph of r^2 = x^2 + y^2 over a grid of points
    % 1. Set the x and y axis 1D vectors that define this rectangular grid:
    x1D = -3 : 0.5 :3; % 1x13 vector
    y1D = -2.5: 0.5 :2.5; % 1x11 vector

    %% 2. Matlab's meshgrid() function uses these 1D vectors
    % to create 2D arrays of x & y at each point of the rectangular grid.
    [x2D, y2D] = meshgrid(x1D,y1D);

    %% 3. Compute z(x,y) at each point in the grid.
    z = x2D.^2 + 1*x2D + y2D.^2 + 2*y2D;

    %% mesh(x,y,z) creates a 3D wire frame plot.
    figure;
    mesh(x2D, y2D, z);
    title('mesh(x2D, y2D, Z)');
    colormap jet % This selects a colormap
    colorbar; % This displays the color scale
    xlabel('x') % This identifies x-axis
    ylabel('y')
    zlabel('z')

    Solution14.2: Plot 3D Surface and Contour Plots (2)

    The resulting plot is shown here.

    Figure \(\PageIndex{1}\): mesh() 3D plot

    .

    surf() 3D plot

    The surf() function also creates a continous surface 3-dimensional plot. Its usage is the same as the mesh() function.

    Example \(\PageIndex{3}\) surf() 3D Plot

    format short g; format compact; clear all; close all; clc;

    %% Create a 3D graph of r^2 = x^2 + y^2 over a grid of points
    % 1. Set the x and y axis 1D vectors that define this rectangular grid:
    x1D = -3 : 0.5 :3; % 1x13 vector
    y1D = -2.5: 0.5 :2.5; % 1x11 vector

    %% 2. Matlab's meshgrid() function uses these 1D vectors
    % to create 2D arrays of x & y at each point of the rectangular grid.
    [x2D, y2D] = meshgrid(x1D,y1D);

    %% 3. Compute z(x,y) at each point in the grid.
    z = x2D.^2 + 1*x2D + y2D.^2 + 2*y2D;

    %% surf(x,y,z) creates a 3D continous surface plot.
    figure;
    surf(x2D, y2D, z);
    title('mesh(x2D, y2D, Z)');
    colormap hot % This selects a colormap
    colorbar; % This displays the color scale
    xlabel('x') % This identifies x-axis
    ylabel('y')
    zlabel('z')
    14.2: Plot 3D Surface and Contour Plots (3)

    Solution

    The resulting plot is shown here.

    Figure \(\PageIndex{2}\): surf() 3D plot

    See this attached file for images of the colormap options.: colormap_options.pdf

    .

    contour() Plot

    Contour plots are like topographical maps. They display 3D data in a 2D format. The basic syntax is:

    contour(x2D, y2D, z);

    This can be improved by specifying the contour levels and showing their values:

    contour(x2D, y2D, z, levels, 'ShowText','on')); % This creates specified contours and shows their values.

    Example \(\PageIndex{4}\) contour() Plot

    format short g; format compact; clear all; close all; clc;

    %% Create a 3D graph of r^2 = x^2 + y^2 over a grid of points
    % 1. Set the x and y axis 1D vectors that define this rectangular grid:
    x1D = -3 : 0.5 :3; % 1x13 vector
    y1D = -2.5: 0.5 :2.5; % 1x11 vector

    %% 2. Matlab's meshgrid() function uses these 1D vectors
    % to create 2D arrays of x & y at each point of the rectangular grid.
    [x2D, y2D] = meshgrid(x1D,y1D);

    %% 3. Compute z(x,y) at each point in the grid.
    z = x2D.^2 + 1*x2D + y2D.^2 + 2*y2D;

    %% contour(x,y,z) creates a continous surface plot.
    figure;
    levels = [-4: 2: 24]; % Specify the contours
    contour(x2D, y2D, z, levels, 'ShowText','on');
    title('contour(x2D, y2D, Z), levels, ''ShowText'',''on'')');
    colorbar; % This displays the color scale
    14.2: Plot 3D Surface and Contour Plots (4)

    Solution

    Resulting plot:

    Figure \(\PageIndex{3}\): contour() plot

    .

    .

    colormap Options

    jet

    14.2: Plot 3D Surface and Contour Plots (5)

    parula

    14.2: Plot 3D Surface and Contour Plots (6)

    hsv

    14.2: Plot 3D Surface and Contour Plots (7)

    hot

    14.2: Plot 3D Surface and Contour Plots (8)

    cool

    14.2: Plot 3D Surface and Contour Plots (9)

    spring

    14.2: Plot 3D Surface and Contour Plots (10)

    summer

    14.2: Plot 3D Surface and Contour Plots (11)

    autumn

    14.2: Plot 3D Surface and Contour Plots (12)

    winter

    14.2: Plot 3D Surface and Contour Plots (13)

    gray

    14.2: Plot 3D Surface and Contour Plots (14)

    bone

    14.2: Plot 3D Surface and Contour Plots (15)

    copper

    14.2: Plot 3D Surface and Contour Plots (16)

    prism

    14.2: Plot 3D Surface and Contour Plots (17)

    flag

    14.2: Plot 3D Surface and Contour Plots (18)

    colorcube

    14.2: Plot 3D Surface and Contour Plots (19)

    lines

    14.2: Plot 3D Surface and Contour Plots (20)
    14.2: Plot 3D Surface and Contour Plots (2024)
    Top Articles
    Hamburg & Amsterdam Adventure from Tilbury, 6 January 2026 | 6 Nt | Ambition | 06 January 2026 | Ambassador Cruise Line
    Her Imports Greenbelt Md
    Average Jonas Wife
    Junk Cars For Sale Craigslist
    How To Do A Springboard Attack In Wwe 2K22
    Team 1 Elite Club Invite
    Gameplay Clarkston
    Flat Twist Near Me
    Danielle Longet
    Walgreens On Nacogdoches And O'connor
    C Spire Express Pay
    Where does insurance expense go in accounting?
    Zalog Forum
    Sni 35 Wiring Diagram
    Weepinbell Gen 3 Learnset
    Air Quality Index Endicott Ny
    Bidevv Evansville In Online Liquid
    Bn9 Weather Radar
    Panolian Batesville Ms Obituaries 2022
    27 Modern Dining Room Ideas You'll Want to Try ASAP
    Ou Football Brainiacs
    Ascensionpress Com Login
    Ocala Craigslist Com
    Ultra Ball Pixelmon
    Play It Again Sports Forsyth Photos
    Dl.high Stakes Sweeps Download
    Dumb Money, la recensione: Paul Dano e quel film biografico sul caso GameStop
    Today's Final Jeopardy Clue
    Devotion Showtimes Near Mjr Universal Grand Cinema 16
    Pillowtalk Podcast Interview Turns Into 3Some
    Autozone Locations Near Me
    Streameast.xy2
    Ashoke K Maitra. Adviser to CMD&#39;s. Received Lifetime Achievement Award in HRD on LinkedIn: #hr #hrd #coaching #mentoring #career #jobs #mba #mbafreshers #sales…
    Cranston Sewer Tax
    301 Priest Dr, KILLEEN, TX 76541 - HAR.com
    Lovein Funeral Obits
    If You're Getting Your Nails Done, You Absolutely Need to Tip—Here's How Much
    Lucyave Boutique Reviews
    Costco Gas Foster City
    R: Getting Help with R
    Natasha Tosini Bikini
    Reilly Auto Parts Store Hours
    Caphras Calculator
    Gw2 Support Specter
    Waco.craigslist
    Ronnie Mcnu*t Uncensored
    Mail2World Sign Up
    Bbwcumdreams
    Walmart Front Door Wreaths
    2121 Gateway Point
    Duffield Regional Jail Mugshots 2023
    All Obituaries | Roberts Funeral Home | Logan OH funeral home and cremation
    Latest Posts
    Article information

    Author: Nathanael Baumbach

    Last Updated:

    Views: 6292

    Rating: 4.4 / 5 (75 voted)

    Reviews: 90% of readers found this page helpful

    Author information

    Name: Nathanael Baumbach

    Birthday: 1998-12-02

    Address: Apt. 829 751 Glover View, West Orlando, IN 22436

    Phone: +901025288581

    Job: Internal IT Coordinator

    Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

    Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.