home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / DATAFUN.DI$ / DIFF.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.6 KB  |  56 lines

  1. function D = diff(A,nd,arg3)
  2. %DIFF    Differentiate or difference.
  3. %    DIFF may be called with either numeric or symbolic arguments.
  4. %
  5. %    For a numeric vector argument, DIFF computes differences.
  6. %    DIFF(X), for a vector X, is [X(2)-X(1)  X(3)-X(2) ... X(n)-X(n-1)].
  7. %    DIFF(X), for a matrix X, is the matrix of column differences,
  8. %       [X(2:n,:) - X(1:n-1,:)].
  9. %    DIFF(X,n) is the n-th difference function.
  10. %
  11. %    For a symbolic (string) argument, DIFF uses the Maple Symbolic Toolbox
  12. %    to compute derivatives.
  13. %    DIFF(S) differentiates S with respect to its free variable.
  14. %    DIFF(S,'v') differentiates S with respect to 'v'.
  15. %    DIFF(S,n) and DIFF(S,'v',n) differentiate S n times.
  16. %    DIFF, with no arguments, differentiates the previous expression.
  17. %
  18. %    Examples;
  19. %        With numeric arguments:
  20. %        h = .001; x = 0:h:pi;
  21. %        diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x
  22. %        diff((1:10).^2) is 3:2:19
  23. %
  24. %        With symbolic arguments and the Maple Symbolic Toolbox:
  25. %        diff('sin(x^2)') is '2*cos(x^2)*x'
  26. %        diff sin(x^2) is also '2*cos(x^2)*x'
  27. %        diff('t^6',6) is '720'.
  28. %
  29. %    See also GRADIENT, DEL2, INT, SYMVAR.
  30.  
  31. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  32.  
  33. if nargin == 0, A = '"'; end
  34.  
  35. if ~isstr(A)
  36.    if nargin == 1, nd = 1; end
  37.    for k = 1:nd
  38.       [m,n] = size(A);
  39.       if m == 1
  40.           D = A(2:n) - A(1:n-1);
  41.       else
  42.           D = A(2:m,:) - A(1:m-1,:);
  43.       end
  44.    end
  45.  
  46. else
  47.    % This branch requires the Maple Symbolic Toolbox
  48.    if nargin <= 1
  49.       eval('D = symdiff(A);')
  50.    elseif nargin == 2
  51.       eval('D = symdiff(A,nd);')
  52.    elseif nargin == 3
  53.       eval('D = symdiff(A,nd,arg3);')
  54.    end
  55. end
  56.