COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### spline fit & 2nd derivatives

• Follow

```Hi
I have a set of data and what to apply a spline fit to it (been informed high order polynomials have their limitations). Below some sample data of x and y form (this has been simplified, usually 200 data points per sample)

X = [2.54	5.308	8.022	10.685	13.301	15.87	18.394	20.876	23.316	25.716	28.077];

Y = [-0.006	-0.02	 -0.04	-0.06	  -0.079	-0.095	-0.107	-0.115	-0.118	-0.117	-0.114];

I am using the spline function in matlab to apply my fit.
My questions:
I would like to be able to extract the function of the curve so I can find the 1st and 2nd derivative. The reason... I want to find the min/max or inflection points.  Can anyone suggest the best way to do this?

I plan to repeat this process over the many different samples that I have (approximately 1000 data sets of x,y terms) but this is for the future.
```
 0

```You could probably just differentiate numerically. But if you wanted to do it analytically, that's not so hard either. The coefficients over each interval are given to you in the coefs field of the spline structure. You can differentiate these very easily. It turns out like this:

X = [2.54 5.308 8.022 10.685 13.301 15.87 18.394 20.876 23.316 25.716 28.077];
Y = [-0.006 -0.02 -0.04 -0.06 -0.079 -0.095 -0.107 -0.115 -0.118 -0.117 -0.114];

S = spline(X,Y);

% Multiplying the coeffiecients by this matrix M is equivalent to
% differentiation
M = diag(3:-1:1,1);

% First derivative
S1 = S;
S1.coefs = S1.coefs*M;

% Second derivative
S2 = S1;
S2.coefs = S2.coefs*M;

x = linspace(X(1),X(end),1001);

hold all
plot(X,Y,'o-')
plot(x,ppval(S,x),'r')
plot(x,ppval(S1,x),'g')
plot(x,ppval(S2,x),'k')

legend({'Original Data','Spline','First Derivative','Second Derivative'})
```
 0

```"Teja Muppirala" <teja.muppiralaRemoveThis@mathworks.com> wrote in message <ia6h77\$r7k\$1@fred.mathworks.com>...
> You could probably just differentiate numerically. But if you wanted to do it analytically, that's not so hard either. The coefficients over each interval are given to you in the coefs field of the spline structure. You can differentiate these very easily. It turns out like this:
>
>
> X = [2.54 5.308 8.022 10.685 13.301 15.87 18.394 20.876 23.316 25.716 28.077];
> Y = [-0.006 -0.02 -0.04 -0.06 -0.079 -0.095 -0.107 -0.115 -0.118 -0.117 -0.114];
>
> S = spline(X,Y);
>
> % Multiplying the coeffiecients by this matrix M is equivalent to
> % differentiation
> M = diag(3:-1:1,1);
>
>
> % First derivative
> S1 = S;
> S1.coefs = S1.coefs*M;
>
>
> % Second derivative
> S2 = S1;
> S2.coefs = S2.coefs*M;
>
> x = linspace(X(1),X(end),1001);
>
> hold all
> plot(X,Y,'o-')
> plot(x,ppval(S,x),'r')
> plot(x,ppval(S1,x),'g')
> plot(x,ppval(S2,x),'k')
>
> legend({'Original Data','Spline','First Derivative','Second Derivative'})

Thank you for the response and apologies with the delay in sending this...internet issues.

I am not familiar with numerical differentiations, how would the matrix (M=diag(3:-1:1,1)) change is if had X and Y data terms of 1:120?
```
 0

```"Dave400 " <dave.rogers@hotmail.co.uk> wrote in message <ia6519\$nd2\$1@fred.mathworks.com>...
> Hi
> I have a set of data and what to apply a spline fit to it (been informed high order polynomials have their limitations). Below some sample data of x and y form (this has been simplified, usually 200 data points per sample)
>
> X = [2.54	5.308	8.022	10.685	13.301	15.87	18.394	20.876	23.316	25.716	28.077];
>
> Y = [-0.006	-0.02	 -0.04	-0.06	  -0.079	-0.095	-0.107	-0.115	-0.118	-0.117	-0.114];
>
> I am using the spline function in matlab to apply my fit.
> My questions:
> I would like to be able to extract the function of the curve so I can find the 1st and 2nd derivative. The reason... I want to find the min/max or inflection points.  Can anyone suggest the best way to do this?
>
> I plan to repeat this process over the many different samples that I have (approximately 1000 data sets of x,y terms) but this is for the future.

There is NO single "function" that you can extract.
A spline is a collection of MANY piecewise polynomial
segments, that connect together into a single well
behaved function.

You can differentiate them easily with the splines
toolbox (now part of the curvefitting toolbox.) Use
fnder for this.

Alternatively, if you use my SLM tools, I provide code
to differentiate a spline created by that toolbox, and to
yield various computations on a spline. The slmpar
function in my tools will return the overall min or max
value of the spline, as well as where that value occurs.

Otherwise, there is no alternative to your learning how
to work with the polynomial segments yourself.

John
```
 0

3 Replies
492 Views

Similiar Articles:

7/22/2012 4:19:11 PM