"Peta " <petie4u@hotmail.com> wrote in message <hsdijs$sp3$1@fred.mathworks.com>...
> Hello,
> I'm having some trouble differentiating a curve I have. I don't really understand the command but I've got a plot of the Density in a lake and I need to find the derivative at a certain point. I've fit a ninth order polynomial to it but it's a horrbile fit, yet nothing else gets close and I need this to use this for the 'differentiate' command.
>
> If anyone knows a little about this would you please give me a little guidance.
> Thanks heaps
> Peta
If your data involves unequal intervals between points on your curve, you can use methods that fit a polynomial to values in the immediate vicinity of a point to estimate its derivative at that point. In a recent cssm thread at
http://www.mathworks.com/matlabcentral/newsreader/view_thread/281297
I showed a method for second order derivative approximation which I will quote here:
"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 [row] vectors (of the same length,) do this:
x1 = x([3,1:end1]); x2 = x([2:end,end2]);
y1 = y([3,1:end1]); y2 = y([2:end,end2]);
dydx = ((y2y).*(xx1).^2+(yy1).*(x2x).^2)./ ...
((x2x).*(xx1).*(x2x1));
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."
In a Jan. '09 thread I gave a formula for derivative approximation using a fourth order polynomial running through five successive points. It is located at:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242833
There I said, "Here is the quartic formula for first derivative estimates. If a curve goes through the five points (x1,y1), (x2,y2), (x3,y3), (x4,y4), and (x5,y5), (presumably with x1 < x2 < x3 < x4 < x5), then the quartic estimate for the derivative dy/dx at x = x3 is:
x21 = x2x1; x32 = x3x2; x43 = x4x3; x54 = x5x4;
x31 = x3x1; x42 = x4x2; x53 = x5x3;
x41 = x4x1; x52 = x5x2; x51 = x5x1;
w21 = x32*x43*x53/x31/x41/x51;
w32 = (x31*x41*(x31+x52)+x32*x52*(x31+x42))*x43*x53/x31/x42/x41/x52/x51;
w43 = (x53*x52*(x53+x41)+x43*x41*(x53+x42))*x32*x31/x53/x42/x41/x52/x51;
w54 = x32*x43*x31/x53/x52/x51;
dy/dx3 = w21*(y2y1)/x21+w32*(y3y2)/x32+w43*(y4y3)/x43+w54*(y5y4)/x54;
It is easy to write this in vectorized form for vectors. At the endpoints it is not necessary to have x1 through x5 in ascending order. The same formula works with them out of order, so for example you can have x3 < x4 < x5 < x1 < x2 and get a valid fourth order estimate for the derivative at x3."
Perhaps one of these might be of use to you. You will note that this is different from fitting single polynomials to entire curves. These methods I describe simply fit polynomials to points in the vicinity of a point where the derivative is to be found. For different points there will be different polynomials. Usually this point of interest is centered among surrounding points, but at the endpoints of a curve it need not be  the same formula still applies.
Roger Stafford
