Path: news.mathworks.com!not-for-mail 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$1@fred.mathworks.com> References: <1606833116.71939.1273004986245.JavaMail.root@gallium.mathforum.org> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-05-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1273019431 19868 172.30.248.35 (5 May 2010 00:30:31 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Wed, 5 May 2010 00:30:31 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:632603 Beaver <marden.nomm@gmail.com> wrote in message <1606833116.71939.1273004986245.JavaMail.root@gallium.mathforum.org>... > 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 (u2-u1)/(z2-z1) and from this you can extend this idea using matlab's 'diff' function to diff(u)./diff(z) 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)./ ... ((x2-x).*(x-x1).*(x2-x1)); 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