Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In MATLAB you can use either the function <a href="https://www.mathworks.com/help/matlab/ref/griddata.html" rel="nofollow noreferrer"><code>griddata</code></a> or the <a href="https://www.mathworks.com/help/matlab/ref/triscatteredinterp.html" rel="nofollow noreferrer"><code>TriScatteredInterp</code> class</a> (Note: as of R2013a <a href="https://www.mathworks.com/help/matlab/ref/scatteredinterpolant-object.html" rel="nofollow noreferrer"><code>scatteredInterpolant</code></a> is the recommended alternative). Both of these allow you to fit a surface of regularly-spaced data to a set of nonuniformly-spaced points (although it appears <code>griddata</code> is no longer recommended in newer MATLAB versions). Here's how you can use each:</p> <ul> <li><p><code>griddata</code>:</p> <pre><code>[XI,YI,ZI] = griddata(x,y,z,XI,YI) </code></pre> <p>where <code>x,y,z</code> each represent vectors of the cartesian coordinates for each point (in this case the points on the contour lines). The row vector <code>XI</code> and column vector <code>YI</code> are the cartesian coordinates at which <code>griddata</code> interpolates the values <code>ZI</code> of the fitted surface. The new values returned for the matrices <code>XI,YI</code> are the same as the result of passing <code>XI,YI</code> to <a href="https://www.mathworks.com/help/matlab/ref/meshgrid.html" rel="nofollow noreferrer"><code>meshgrid</code></a> to create a uniform grid of points.</p></li> <li><p><code>TriScatteredInterp</code> class:</p> <pre><code>[XI,YI] = meshgrid(...); F = TriScatteredInterp(x(:),y(:),z(:)); ZI = F(XI,YI); </code></pre> <p>where <code>x,y,z</code> again represent vectors of the cartesian coordinates for each point, only this time I've used a <a href="https://www.mathworks.com/help/matlab/ref/colon.html" rel="nofollow noreferrer">colon reshaping operation</a> <code>(:)</code> to ensure that each is a <em>column vector</em> (the required format for <code>TriScatteredInterp</code>). The interpolant <code>F</code> is then evaluated using the matrices <code>XI,YI</code> that you must create using <code>meshgrid</code>.</p></li> </ul> <h2>Example &amp; Comparison</h2> <p>Here's some sample code and the resulting figure it generates for reconstructing a surface from contour data using both methods above. The contour data was generated with the <a href="https://www.mathworks.com/help/matlab/ref/contour.html" rel="nofollow noreferrer"><code>contour</code></a> function:</p> <pre><code>% First plot: subplot(2,2,1); [X,Y,Z] = peaks; % Create a surface surf(X,Y,Z); axis([-3 3 -3 3 -8 9]); title('Original'); % Second plot: subplot(2,2,2); [C,h] = contour(X,Y,Z); % Create the contours title('Contour map'); % Format the coordinate data for the contours: Xc = []; Yc = []; Zc = []; index = 1; while index &lt; size(C,2) Xc = [Xc C(1,(index+1):(index+C(2,index)))]; Yc = [Yc C(2,(index+1):(index+C(2,index)))]; Zc = [Zc C(1,index).*ones(1,C(2,index))]; index = index+1+C(2,index); end % Third plot: subplot(2,2,3); [XI,YI] = meshgrid(linspace(-3,3,21)); % Generate a uniform grid ZI = griddata(Xc,Yc,Zc,XI,YI); % Interpolate surface surf(XI,YI,ZI); axis([-3 3 -3 3 -8 9]); title('GRIDDATA reconstruction'); % Fourth plot: subplot(2,2,4); F = TriScatteredInterp(Xc(:),Yc(:),Zc(:)); % Generate interpolant ZIF = F(XI,YI); % Evaluate interpolant surf(XI,YI,ZIF); axis([-3 3 -3 3 -8 9]); title('TriScatteredInterp reconstruction'); </code></pre> <p><a href="https://i.stack.imgur.com/vB8D7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vB8D7.jpg" alt="enter image description here"></a></p> <p>Notice that there is little difference between the two results (at least at this scale). Also notice that the interpolated surfaces have empty regions near the corners due to the sparsity of contour data at those points.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload