home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / DELSQ.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  853 b   |  34 lines

  1. function D = delsq(G)
  2. %DELSQ    Construct five-point finite difference Laplacian.
  3. %    delsq(G) is the sparse form of the two-dimensional,
  4. %    5-point discrete negative Laplacian on the grid G.
  5. %    The grid G can be generated by NUMGRID or NESTED.
  6. %
  7. %    See also NUMGRID, DEL2, DELSQDEMO.
  8.  
  9. %    C. Moler, 7-16-91.
  10. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  11.  
  12. [m,n] = size(G);
  13.  
  14. % Indices of interior points
  15. p = find(G);
  16.  
  17. % Connect interior points to themselves with 4's.
  18. i = G(p);
  19. j = G(p);
  20. s = 4*ones(size(p));
  21.  
  22. % for k = north, east, south, west
  23. for k = [-1 m 1 -m]
  24.    % Possible neighbors in k-th direction
  25.    Q = G(p+k);
  26.    % Index of points with interior neighbors
  27.    q = find(Q);
  28.    % Connect interior points to neighbors with -1's.
  29.    i = [i; G(p(q))];
  30.    j = [j; Q(q)];
  31.    s = [s; -ones(length(q),1)];
  32. end
  33. D = sparse(i,j,s);
  34.