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 件のコメント

Torsten
Torsten 2022 年 7 月 3 日
編集済み: Torsten 2022 年 7 月 3 日
Do you want to use spline interpolation or spline fitting ?
For spline fitting, use
for spline interpolation, use "interp1" with the interpolation method "spline":
Deepa Maheshvare
Deepa Maheshvare 2022 年 7 月 3 日
Could you please also help me with the command that can do a linear interpolation between the datapoints (like what's seen in a line plot)?
Sam Chak
Sam Chak 2022 年 7 月 3 日
Okay, go to this 'interp1' documentation and use the 'spline' method.
Then see the example and post your code here. We will then try to fix it if there is an error.
Deepa Maheshvare
Deepa Maheshvare 2022 年 7 月 3 日
Hi,
Sorry for not updating here.
I tried
x = [0,4,6,10,15,20]
y = [18,17.5,13,12,8,10]
splinetool(x,y)
This is what I see for a cubic polynomial. I would also try to interpolate the values for a polynomial of degree 1 , linear interpolation. I couldn't find the option to set the polynomial degree here.
Deepa Maheshvare
Deepa Maheshvare 2022 年 7 月 3 日
Hi, Thanks . I get it, my mistake. `interp1` helps
Deepa Maheshvare
Deepa Maheshvare 2022 年 7 月 3 日
x = [0,4,6,10,15,20]
y = [18,17.5,13,12,8,10]
vq = interp1(x,y,'linear')
returns NaN
Deepa Maheshvare
Deepa Maheshvare 2022 年 7 月 3 日
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?

サインインしてコメントする。

 採用された回答

Sam Chak
Sam Chak 2022 年 7 月 3 日

1 投票

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 件のコメント

Deepa Maheshvare
Deepa Maheshvare 2022 年 7 月 3 日
Thank you, I wanted linear interpolation for a use case. `spline` is useful for me.
vq = interp1(x,v, 'linear', 'pp')
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?
Torsten
Torsten 2022 年 7 月 3 日
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
Deepa Maheshvare 2022 年 7 月 4 日
Thanks a lot for the clarification.

サインインしてコメントする。

その他の回答 (1 件)

カテゴリ

ヘルプ センター および File Exchange で Splines についてさらに検索

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by