From: "Wen Zhe " <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Finding gradient on curve
Date: Wed, 31 Mar 2010 08:20:31 +0000 (UTC)
Organization: University of Manchester
Lines: 27
Message-ID: <hov0kf$sh2$>
References: <hor82t$e7r$> <horci0$er4$>
Reply-To: "Wen Zhe " <>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1270023631 29218 (31 Mar 2010 08:20:31 GMT)
NNTP-Posting-Date: Wed, 31 Mar 2010 08:20:31 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2157639
Xref: comp.soft-sys.matlab:622167

Thanks for the reply Roger.

So which one would you recommend that i use? grad() or diff()?


"Roger Stafford" <> wrote in message <horci0$er4$>...
> "Wen Zhe " <> wrote in message <hor82t$e7r$>...
> > Dear all,
> > 
> > I have a function u(t,x), which is dependent on both t and x. Now, i plotted a graph of u versus x, at various time points (i.e. i get several curves of u over x at different time points on the same plot).
> > 
> > The question is: how am i to find the gradient at a particular point on the graph? Let's say at u(t,2) and at all the time points? 
> > 
> > I'm thinking of diff() and grad(). But what is the difference and how would i be able to set this up in my code?
> > 
> > Any help appreciated. Thankies!
> > 
> > Wendy
>   The functions, 'diff' and 'gradient', are considerably different.  The 'gradient' function allows you to specify variable-length intervals within your two independent variables, t and x in this case, if you so choose.  They do not have to be uniformly spaced apart.  Also it returns both (all) components of a gradient calculation.  However, be sure you read the description - the first component returned is for the second argument and visa-versa.
>   I can't be certain, but I suspect the approximations made by 'gradient' are of a higher than first order, possibly a cubic.  If not, there are undoubtedly some third-order gradients available in the file exchange.  I recall I wrote one for one of these threads quite some time ago, but I can't remember how to locate it now.
>   On the other hand 'diff' returns differences in only one direction - for you, in either the t direction or the x direction.  Also it assumes that the corresponding spacing is always in unit-length intervals.  In other words the values returned by 'diff' are strictly successive differences.  Consequently there are one fewer of them in whatever direction it is taken than in the input variable, whereas the vector or matrix returned by 'gradient' is always the same size as its input argument.
> Roger Stafford