# Fit a spline function to a set of data points

53 vues (au cours des 30 derniers jours)
Deepa Maheshvare le 3 Juil 2022
Hi All,
I have a set of coordinate points
x= [0,4,6,10,15,20]
y = [18,17.5,13,12,8,10]
I want to fit a spline function.
Could someone please suggest the the curve fit function that can be used?
##### 7 commentairesAfficher 5 commentaires plus anciensMasquer 5 commentaires plus anciens
Deepa Maheshvare le 3 Juil 2022
x = [0,4,6,10,15,20]
y = [18,17.5,13,12,8,10]
vq = interp1(x,y,'linear')
returns NaN
Deepa Maheshvare le 3 Juil 2022
I tried
x = [0,4,6,10,15,20]
v = [18,17.5,13,12,8,10]
vq = interp1(x,v, 'linear', 'pp')
xq = x
figure
vq1 = interp1(x,v,xq);
plot(x,v,'o',xq,vq1,':.');
title('(Default) Linear Interpolation');
returns
vq =
struct with fields:
form: 'pp'
breaks: [0 4 6 10 15 20]
coefs: [5×2 double]
pieces: 5
order: 2
dim: 1
orient: 'first'
Here I am not sure why the order (which I infer as the polynomial degree) is 2 for linear interpolation. Could you please explain this a bit?

Connectez-vous pour commenter.

### Réponse acceptée

Sam Chak le 3 Juil 2022
Thought you want the 'spline'? Is there a reason to use the 'linear' method?
x = [0, 4, 6, 10, 15, 20];
y = [18, 17.5, 13, 12, 8, 10];
xq = 0:0.01:20;
subplot(2,1,1)
yq1 = interp1(x, y, xq, 'linear');
plot(x, y, 'o', xq, yq1, '--');
ylim([5 22]), grid on, title('Linear Interpolation');
subplot(2,1,2)
yq2 = interp1(x, y, xq, 'spline');
plot(x, y, 'o', xq, yq2, '--');
ylim([5 22]), grid on, title('Spline Interpolation');
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Torsten le 3 Juil 2022
Seems that order = 2 means that two coefficients are needed to describe the (linear) spline function on each subinterval and is set to 1 + degree of the interpolating polynomial. A bit confusing in my opinion.
Deepa Maheshvare le 4 Juil 2022
Thanks a lot for the clarification.

Connectez-vous pour commenter.

### Plus de réponses (1)

Simon Chan le 3 Juil 2022
Use function spline
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Splines dans Help Center et File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by