Representing Data as a Surface - MATLAB & Simulink (2024)

Representing Data as a Surface

Functions for Plotting Data Grids

MATLAB® graphics defines a surface by the z-coordinates of points above a rectangular grid in the x-y plane. The plot is formed by joining adjacent points with straight lines. Surface plots are useful for visualizing matrices that are too large to display in numerical form and for graphing functions of two variables.

MATLAB can create different forms of surface plots. Mesh plots are wire-frame surfaces that color only the lines connecting the defining points. Surface plots display both the connecting lines and the faces of the surface in color. This table lists the various forms.

Function

Used to Create

mesh, surf

Surface plot

meshc, surfc

Surface plot with contour plot beneath it

meshz

Surface plot with curtain plot (reference plane)

pcolor

Flat surface plot (value is proportional only to color)

surfl

Surface plot illuminated from specified direction

surface

Low-level function (on which high-level functions are based) for creating surface graphics objects

Functions for Gridding and Interpolating Data

These functions are useful when you need to restructure and interpolate data so that you can represent this data as a surface.

Function

Used to Create

meshgrid

Rectangular grid in 2-D and 3-D space

griddata

Interpolate scattered data

griddedInterpolant

Interpolant for gridded data

scatteredInterpolant

Interpolate scattered data

For a discussion of how to interpolate data, see Interpolating Gridded Data and Interpolating Scattered Data.

Mesh and Surface Plots

The mesh and surf commands create 3-D surface plots of matrix data. If Z is a matrix for which the elements Z(i,j) define the height of a surface over an underlying (i,j) grid, then

mesh(Z)

generates a colored, wire-frame view of the surface and displays it in a 3-D view. Similarly,

surf(Z)

generates a colored, faceted view of the surface and displays it in a 3-D view. Ordinarily, the facets are quadrilaterals, each of which is a constant color, outlined with black mesh lines, but the shading command allows you to eliminate the mesh lines (shading flat) or to select interpolated shading across the facet (shading interp).

Surface object properties provide additional control over the visual appearance of the surface. You can specify edge line styles, vertex markers, face coloring, lighting characteristics, and so on.

Visualizing Functions of Two Variables

  1. To display a function of two variables, z = f(x,y), generate X and Y matrices consisting of repeated rows and columns, respectively, over the domain of the function. You will use these matrices to evaluate and graph the function.

  2. The meshgrid function transforms the domain specified by two vectors, x and y, into matrices X and Y. You then use these matrices to evaluate functions of two variables: The rows of X are copies of the vector x and the columns of Y are copies of the vector y.

Example1.Example: Illustrating the Use of meshgrid

To illustrate the use of meshgrid, consider the sin(r)/r or sinc function. To evaluate this function between -8 and 8 in both x and y, you need pass only one vector argument to meshgrid, which is then used in both directions.

[X,Y] = meshgrid(-8:.5:8);R = sqrt(X.^2 + Y.^2) + eps;

The matrix R contains the distance from the center of the matrix, which is the origin. Adding eps prevents the divide by zero (in the next step) that produces Inf values in the data.

Forming the sinc function and plotting Z with mesh results in the 3-D surface.

Z = sin(R)./R;figuremesh(X,Y,Z)

Representing Data as a Surface- MATLAB & Simulink (1)

Hidden Line Removal

By default, MATLAB removes lines that are hidden from view in mesh plots, even though the faces of the plot are not filled. You can disable hidden line removal and allow the faces of a mesh plot to be transparent with the hidden command:

hidden off

Representing Data as a Surface- MATLAB & Simulink (2)

Emphasizing Surface Shape

MATLAB provides a number of techniques that can enhance the information content of your graphs. For example, this graph of the sinc function uses the same data as the previous graph, but employs lighting, view adjustments, and a different colormap to emphasize the shape of the graphed function (daspect, axis, view, camlight).

figurecolormap hsvsurf(X,Y,Z,'FaceColor','interp',... 'EdgeColor','none',... 'FaceLighting','gouraud')daspect([5 5 1])axis tightview(-50,30)camlight left

Representing Data as a Surface- MATLAB & Simulink (3)

See the surf function for more information on surface plots.

Surface Plots of Nonuniformly Sampled Data

You can use meshgrid to create a grid of uniformly sampled data points at which to evaluate and graph the sinc function. MATLAB then constructs the surface plot by connecting neighboring matrix elements to form a mesh of quadrilaterals.

To produce a surface plot from nonuniformly sampled data, use scatteredInterpolant to interpolate the values at uniformly spaced points, and then use mesh and surf in the usual way.

Example – Displaying Nonuniform Data on a Surface

This example evaluates the sinc function at random points within a specific range and then generates uniformly sampled data for display as a surface plot. The process involves these tasks:

  • Use linspace to generate evenly spaced values over the range of your unevenly sampled data.

  • Use meshgrid to generate the plotting grid with the output of linspace.

  • Use scatteredInterpolant to interpolate the irregularly sampled data to the regularly spaced grid returned by meshgrid.

  • Use a plotting function to display the data.

  1. Generate unevenly sampled data within the range [-8, 8] and use it to evaluate the function:

    x = rand(100,1)*16 - 8;y = rand(100,1)*16 - 8;r = sqrt(x.^2 + y.^2) + eps;z = sin(r)./r;
  2. The linspace function provides a convenient way to create uniformly spaced data with the desired number of elements. The following statements produce vectors over the range of the random data with the same resolution as that generated by the -8:.5:8 statement in the previous sinc example:

    xlin = linspace(min(x),max(x),33);ylin = linspace(min(y),max(y),33);
  3. Now use these points to generate a uniformly spaced grid:

    [X,Y] = meshgrid(xlin,ylin);
  4. The key to this process is to use scatteredInterpolant to interpolate the values of the function at the uniformly spaced points, based on the values of the function at the original data points (which are random in this example). This statement uses the default linear interpolation to generate the new data:

    f = scatteredInterpolant(x,y,z);Z = f(X,Y);
  5. Plot the interpolated and the nonuniform data to produce:

    figuremesh(X,Y,Z) %interpolatedaxis tight; hold onplot3(x,y,z,'.','MarkerSize',15) %nonuniform

    Representing Data as a Surface- MATLAB & Simulink (4)

Reshaping Data

Suppose you have a collection of data with the following (X, Y, Z) triplets:

XYZ
11152
2189
31100
41100
51100
12103
220
32100
42100
52100
1389
2313
33100
43100
53100
14115
24100
34187
44200
54111
15100
2585
35111
4597
5548

You can represent data that is in vector form using various MATLAB graph types, such as surf, contour, and stem3, by first restructuring the data. Use the (X, Y) values to define the coordinates in an x-y plane at which there is a Z value. The reshape and transpose functions can restructure your data so that the (X, Y, Z) triplets form a rectangular grid:

x = reshape(X,5,5)';y = reshape(Y,5,5)';z = reshape(Z,5,5)';

Reshaping results in three 5–by-5 arrays:

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 5y = 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5z = 152 89 100 100 100 103 0 100 100 100 89 13 100 100 100 115 100 187 200 111 100 85 111 97 48

You can now represent the values of Z with respect to X and Y. For example, create a 3–D stem graph:

stem3(x,y,z,'MarkerFaceColor','g')

Representing Data as a Surface- MATLAB & Simulink (5)

Parametric Surfaces

The functions that draw surfaces can take two additional vector or matrix arguments to describe surfaces with specific x and y data. If Z is an m-by-n matrix, x is an n-vector, and y is an m-vector, then

mesh(x,y,Z,C)

describes a mesh surface with vertices having color C(i,j) and located at the points

(x(j), y(i), Z(i,j))

where x corresponds to the columns of Z and y to its rows.

More generally, if X, Y, Z, and C are matrices of the same dimensions, then

mesh(X,Y,Z,C)

describes a mesh surface with vertices having color C(i,j) and located at the points

(X(i,j), Y(i,j), Z(i,j))

This example uses spherical coordinates to draw a sphere and color it with the pattern of pluses and minuses in a Hadamard matrix, an orthogonal matrix used in signal processing coding theory. The vectors theta and phi are in the range -π ≤ theta ≤ π and -π/2phi ≤ π/2. Because theta is a row vector and phi is a column vector, the multiplications that produce the matrices X, Y, and Z are vector outer products.

figurek = 5;n = 2^k-1;theta = pi*(-n:2:n)/n;phi = (pi/2)*(-n:2:n)'/n;X = cos(phi)*cos(theta);Y = cos(phi)*sin(theta);Z = sin(phi)*ones(size(theta));colormap([0 0 0;1 1 1])C = hadamard(2^k); surf(X,Y,Z,C)axis square

Representing Data as a Surface- MATLAB & Simulink (6)

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Representing Data as a Surface- MATLAB & Simulink (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Representing Data as a Surface
- MATLAB & Simulink (2024)

FAQs

How to define a surface in MATLAB? ›

MATLAB® graphics defines a surface by the z-coordinates of points above a rectangular grid in the x-y plane. The plot is formed by joining adjacent points with straight lines. Surface plots are useful for visualizing matrices that are too large to display in numerical form and for graphing functions of two variables.

How to plot response surface in MATLAB? ›

To create a response surface plot, you have to provide Z as a matrix. Specifically, if two vector arguments X & Y defined such that length(X) = n and length(Y) = m then, size(Z) should be [m,n]. In order to plot the vertices of the surface patches as the triples (X(j), Y(i), Z(i,j)).

What is the difference between surface and mesh in MATLAB? ›

mesh produces wireframe surfaces that color only the lines connecting the defining points. surf displays both the connecting lines and the faces of the surface in color. The figure colormap and figure properties determine how MATLAB colors the surface.

How is a surface defined? ›

: the exterior or upper boundary of an object or body. on the surface of the water. the earth's surface. 2. : a plane or curved two-dimensional locus of points (such as the boundary of a three-dimensional region)

What is the surface area in MATLAB? ›

Share 'Surface area'

[totalArea, areas, centroid] = surfarea(h), where h is a handle to a Surface object. surfarea calculates the total surface area as well as returns the areas of each individual cell, along with the area centroid of the surface. Example: Plot peaks, coloring each cell with its own area.

How to sample data in Simulink? ›

Simulink provides a way to do this by allowing you to set the sample time for discrete, continuous, and multi-rate components. This model of a fuel control system has sample times set to control when the discrete, continuous, and multi-rate components are updated. Sample time is set as a parameter in the block dialog.

What is Simulink used for in MATLAB? ›

Simulink is the platform for Model-Based Design that supports system-level design, simulation, automatic code generation, and continuous test and verification of embedded systems. Key capabilities include: A graphical editor for modeling all components of a system.

How do MATLAB and Simulink work together? ›

In this page we will examine three of the ways in which Simulink can interact with MATLAB. Block parameters can be defined from MATLAB variables. Signals can be exchanged between Simulink and MATLAB. Entire systems can be extracted from Simulink into MATLAB.

What is a surface plot? ›

Introduction. Surface plots are diagrams of three-dimensional data. Rather than showing the individual data points, surface plots show a functional relationship between a designated dependent variable (Y), and two independent variables (X and Z). The plot is a companion plot to the contour plot.

What is the code for surface plot in MATLAB? ›

surf( X , Y , Z , C ) additionally specifies the surface color. surf( Z ) creates a surface plot and uses the column and row indices of the elements in Z as the x- and y-coordinates. surf( Z , C ) additionally specifies the surface color. surf( ax ,___) plots into the axes specified by ax instead of the current axes.

How do you interpret a response surface plot? ›

Interpret the key results for Analyze Response Surface Design
  1. Step 1: Determine which terms contribute the most to the variability in the response.
  2. Step 2: Determine whether the association between the response and the term is statistically significant.
  3. Step 3: Determine how well the model fits your data.

What is the surface function in MATLAB? ›

surface( X , Y , Z ) creates a primitive, three-dimensional surface plot. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y . The color of the surface varies according to the heights specified by Z .

How do you convert surface to mesh? ›

To convert 3D Face objects into a single mesh object follow the steps:
  1. Use the Convert to Surface command (CONVTOSSURFACE) to convert the 3D faces into surfaces.
  2. Use the UNION command to join the surfaces together.
  3. Use the Smooth Object command (MESHSMOOTH) to create a mesh from the resulting surface.
Oct 8, 2023

Can you run MATLAB on a surface? ›

Yes, at least for the 5th generation onwards .

How do you define properties in MATLAB? ›

Property access syntax is like MATLAB structure field syntax. For example, if obj is an object of a class, then you can get the value of a property by referencing the property name. Assign values to properties by putting the property reference on the left side of the equal sign.

How do you draw a surface sphere in MATLAB? ›

To draw the sphere using the returned coordinates, use the surf or mesh functions. [X,Y,Z] = sphere( n ) returns the x-, y-, and z- coordinates of a sphere with a radius equal to 1 and n -by- n faces. The function returns the x-, y-, and z- coordinates as three (n+1) -by- (n+1) matrices.

How do you define a system in MATLAB? ›

Define Basic System Objects
  1. In MATLAB, on the Editor tab, select New > System Object > Basic. A simple System object template opens.
  2. Subclass your object from matlab. System . Replace Untitled with AddOne in the first line of your file. classdef AddOne < matlab.System. ...
  3. Save the file and name it AddOne. m .

Top Articles
25+ Best Vegan Air Fryer Recipes
Keto Soup Recipes For Cozy Low-Carb Meals
This website is unavailable in your location. – WSB-TV Channel 2 - Atlanta
Ffxiv Act Plugin
Katmoie
Online Reading Resources for Students & Teachers | Raz-Kids
Bucks County Job Requisitions
Top 10: Die besten italienischen Restaurants in Wien - Falstaff
Sissy Hypno Gif
Plus Portals Stscg
Craigslist - Pets for Sale or Adoption in Zeeland, MI
Gw2 Legendary Amulet
Jesse Mckinzie Auctioneer
Roblox Character Added
OSRS Dryness Calculator - GEGCalculators
Overton Funeral Home Waterloo Iowa
Classic | Cyclone RakeAmerica's #1 Lawn and Leaf Vacuum
Vigoro Mulch Safe For Dogs
Eine Band wie ein Baum
Aldi Bruce B Downs
Big Lots Weekly Advertisem*nt
Keci News
Craigslist Lewes Delaware
What Is The Lineup For Nascar Race Today
Craigslistodessa
Construction Management Jumpstart 3Rd Edition Pdf Free Download
Tire Plus Hunters Creek
Saxies Lake Worth
Craigslist Comes Clean: No More 'Adult Services,' Ever
Ixl Lausd Northwest
Personalised Handmade 50th, 60th, 70th, 80th Birthday Card, Sister, Mum, Friend | eBay
Chs.mywork
Scanning the Airwaves
USB C 3HDMI Dock UCN3278 (12 in 1)
Cl Bellingham
Dynavax Technologies Corp (DVAX)
Bismarck Mandan Mugshots
Boone County Sheriff 700 Report
Cal Poly 2027 College Confidential
Bones And All Showtimes Near Johnstown Movieplex
Lonely Wife Dating Club בקורות וחוות דעת משתמשים 2021
Mbfs Com Login
BCLJ July 19 2019 HTML Shawn Day Andrea Day Butler Pa Divorce
Pickwick Electric Power Outage
Wood River, IL Homes for Sale & Real Estate
Colin Donnell Lpsg
antelope valley for sale "lancaster ca" - craigslist
The Goshen News Obituary
Mkvcinemas Movies Free Download
Where To Find Mega Ring In Pokemon Radical Red
When Is The First Cold Front In Florida 2022
Bellin Employee Portal
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 5502

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.