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

16 vues (au cours des 30 derniers jours)
SA-W le 8 Mar 2023
Modifié(e) : Torsten le 9 Mar 2023
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 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
SA-W le 8 Mar 2023
Because the values y_1,...,y_n are the parameters of my optimization. My objective is to optimize the values y_1,...,y_n and interpolate between them with cubic splines. Is that clear?

Connectez-vous pour commenter.

### Réponse acceptée

Bruno Luong le 8 Mar 2023
Modifié(e) : Bruno Luong le 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)
f = 1×500
0.8578 0.8215 0.7861 0.7516 0.7180 0.6852 0.6533 0.6223 0.5920 0.5627 0.5341 0.5063 0.4794 0.4532 0.4278 0.4032 0.3794 0.3563 0.3340 0.3124 0.2915 0.2714 0.2519 0.2332 0.2152 0.1978 0.1812 0.1652 0.1498 0.1351
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 commentairesAfficher 16 commentaires plus anciensMasquer 16 commentaires plus anciens
Bruno Luong le 9 Mar 2023
Right

Connectez-vous pour commenter.

### Plus de réponses (2)

Bruno Luong le 8 Mar 2023
Modifié(e) : Bruno Luong le 8 Mar 2023
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 commentairesAfficher 3 commentaires plus anciensMasquer 3 commentaires plus anciens
Bruno Luong le 8 Mar 2023
Modifié(e) : Bruno Luong le 8 Mar 2023
"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

Connectez-vous pour commenter.

Torsten le 8 Mar 2023
Déplacé(e) : Torsten le 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 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Spline Construction dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!