# natural cubic spline interpolation of y-values: how to get derivative of the spline wrt the y-values?

18 views (last 30 days)

Show older comments

Given a data set with support points x_1,...,x_n and corresponding y-values y_1,...,y_n.

My objective is to create a cubic spline f (with natural boundary conditions) that passes through the y_values. There are, of course, plenty of functions for doing this.

However, for a parameter identification procedure, I have to compute the derivative of the spline f with respect to the y-values -- at arbitrary points within [x1, x_n].

Is there an easy way using built-in functions of Matlab to compute the sensitivities?

##### 2 Comments

Torsten
on 8 Mar 2023

### Accepted Answer

Bruno Luong
on 8 Mar 2023

Edited: Bruno Luong
on 8 Mar 2023

The derivative f wrt to y_i is the spline interpolate b_i := (0,0,...,1,0...) where 1 is at ith position, since the spline is linear to y values.

Replace spline command with your function that computes natural spline pp form

x=cumsum(rand(1,10));

y=rand(size(x));

xi=linspace(min(x),max(x),500);

f=ppval(spline(x,y),xi)

plot(x,y,'or',xi,f,'b') %

b=eye(length(x));

yd=spline(x,b);

dfdy=ppval(yd,xi); % dfdy(i,j) is the derivative of f(xi(j)) with respect to y(i))

figure

plot(xi,dfdy')

##### 17 Comments

### More Answers (2)

Bruno Luong
on 8 Mar 2023

Edited: Bruno Luong
on 8 Mar 2023

In this thread https://fr.mathworks.com/matlabcentral/answers/1894800-how-to-remove-noise-from-curves-and-take-their-derivates?s_tid=srchtitle

you can find my function that compute the derivative of a piecewise polynomiall function (pp), inclusing pp form of the spline functions. This function returns the pp form of the derivative, so you can evaluate using MATLAB ppval.

function ppd = ppder(pp)

ppd = pp;

coefs = ppd.coefs;

n = size(coefs,2);

ppd.coefs = coefs(:,1:n-1).*(n-1:-1:1);

ppd.order = ppd.order-1;

end

##### 4 Comments

Bruno Luong
on 8 Mar 2023

Edited: Bruno Luong
on 8 Mar 2023

See my answer below, but

"the result should be a single number"

No the result is a scalar function. If we take f at a given point x then it is a scalar.

"...and not a vector b_i := (0,0,...,1,0...)."

I did not tell the derivative is b, the derivative is the spline interpolating b

"why is a cubic spline linear to y values?"

You clearly missunderstand and male confusion betwen being linear and being a lilnear function

Torsten
on 8 Mar 2023

Moved: Torsten
on 8 Mar 2023

Why do you want to compute the sensitivities manually ?

Usually, the fitting software computes them using a finite-difference approximation, i.e. by calling your function with

y_i

and

y_i+h

getting back

f_j(y_1,...,y_i,...,y_n) and f_j(y_1,...,y_i+h,...,y_n)

and approximating

df_j/dy_i = (f_j(y_1,...,y_i+h,...,y_n)-f_j(y_1,...,y_i,...,y_n))/h

And this would also be my suggestion on how to do it manually if it is really needed.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!