From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Problem
Date: Wed, 5 May 2010 00:30:31 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 33
Message-ID: <hrqe77$jcs$>
References: <>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1273019431 19868 (5 May 2010 00:30:31 GMT)
NNTP-Posting-Date: Wed, 5 May 2010 00:30:31 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:632603

Beaver <> wrote in message <>...
> My problem is following:
> I have velocity u and depth z data matrix (1x100). And have to find answer to the du/dz.
> I know the answer when I have u like a equation - u=2*z+21. But how i manage this problem when I have numerical input data.
> Thanks
- - - - - - - -
  As you can gather from the previous remarks, you can only obtain an approximate derivative from discrete data.  The closeness of the approximation will depend on the fineness and accuracy of the data.

  If you had only two points (z1,u1) and (z2,u2), your best guess would be the straight line slope between the points


and from this you can extend this idea using matlab's 'diff' function to


for longer vectors.  Unfortunately this rather crude method gives one less derivative approximation than the number of values in the vectors, since each one represents a derivative approximation for a point midway between data points, and also it gives only a linear type of approximation.

  I will now give you a formula that produces as many derivative approximations as in the given vectors with each one centered over a corresponding point in those vectors, and which in addition amounts to a second order approximation.  That is, if the one vector's values are a second order polynomial function of values in the other vector, these derivatives would be exact (save for round off error, of course.)  I will give it in terms of variables x and y rather than your z and u.  If x and y are the given vectors (of the same length,) do this:

 x1 = x([3,1:end-1]); x2 = x([2:end,end-2]);
 y1 = y([3,1:end-1]); y2 = y([2:end,end-2]);
 dydx = ((y2-y).*(x-x1).^2+(y-y1).*(x2-x).^2)./ ...

The 'dydx' vector will be an approximation to the derivative dy/dx.  One requirement for using this formula is that the vectors must possess at least three points.

  There are of course higher order formulas with correspondingly more complicated formulas.  I trust this one will be adequate for your purposes.

Roger Stafford