From: Peta on
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
From: Sean on
"Peta " <petie4u(a)hotmail.com> wrote in message <hsdijs$sp3$1(a)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 it's a horrible fit then the fit and corresponding derivative are probably meaningless. I'm assuming you have data points so calculate the numerical first derivative at the point you need.

>>help diff
>>help gradient
From: EE Student on
"Peta " <petie4u(a)hotmail.com> wrote in message <hsdijs$sp3$1(a)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

Did you use poly fit to fit the curve? If so the model for the output of poly fit is

y=p(1)*x^n+p(2)*x^n-1....p(n)x+p(n+1)

You could just take the analytical derivative of this model .

Or use a differentiating filter. Of diff or gradient as was suggested by another poster.
From: Roger Stafford on
"Peta " <petie4u(a)hotmail.com> wrote in message <hsdijs$sp3$1(a)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: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."

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 = x2-x1; x32 = x3-x2; x43 = x4-x3; x54 = x5-x4;
x31 = x3-x1; x42 = x4-x2; x53 = x5-x3;
x41 = x4-x1; x52 = x5-x2; x51 = x5-x1;
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*(y2-y1)/x21+w32*(y3-y2)/x32+w43*(y4-y3)/x43+w54*(y5-y4)/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
From: Roger Stafford on
> "Peta " <petie4u(a)hotmail.com> wrote in message <hsdijs$sp3$1(a)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

Just in case you are interested in that fourth order derivative algorithm from a year ago, I have put it into vectorized form and retested it. You can also test it yourself by creating random coefficients for quartic (fourth order) polynomials and checking that the quantity 'dydx' agrees precisely (up to round off error) with the polynomials' theoretical derivatives over the entire range of the independent variable that you evaluate, including the endpoints. (Of course with other kinds of data you would not get such precise results.)

Let x be a vector of your independent variable values, which may be unevenly spaced, though monotone - that is, the one you are differentiating with respect to - and let y be a vector of the same size with the corresponding quantity you are finding the derivative of. These vectors must possess at least five elements. Then execute the following code. The quantity dydx will be the same size as x and y and is your derivative approximation at the corresponding points.

x1 = x([4,5,1:end-2]); y1 = y([4,5,1:end-2]);
x2 = x([5,1:end-1]); y2 = y([5,1:end-1]);
x3 = x; y3 = y;
x4 = x([2:end,end-4]); y4 = y([2:end,end-4]);
x5 = x([3:end,end-4,end-3]); y5 = y([3:end,end-4,end-3]);
x21 = x2-x1; x32 = x3-x2; x43 = x4-x3; x54 = x5-x4;
x31 = x3-x1; x42 = x4-x2; x53 = x5-x3;
x41 = x4-x1; x52 = x5-x2; x51 = x5-x1;
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;
dydx = w21.*(y2-y1)./x21+w32.*(y3-y2)./x32+ ...
w43.*(y4-y3)./x43+w54.*(y5-y4)./x54;

As you can see in this last expression, the derivative is being approximated by a certain optimum linear combination of the four successive chord-line slopes connecting the five successive points, where the weights, which must add up to one, are determined by the relative spacings of the x-coordinates of those five points.

Roger Stafford