Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
spline fit & 2nd derivatives

Subject: spline fit & 2nd derivatives

From: Dave400

Date: 26 Oct, 2010 08:55:05

Message: 1 of 4

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.

Subject: spline fit & 2nd derivatives

From: Teja Muppirala

Date: 26 Oct, 2010 12:23:03

Message: 2 of 4

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'})

Subject: spline fit & 2nd derivatives

From: Dave400

Date: 28 Oct, 2010 07:35:15

Message: 3 of 4

"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?

Subject: spline fit & 2nd derivatives

From: John D'Errico

Date: 28 Oct, 2010 10:42:03

Message: 4 of 4

"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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us