MATLAB Answers

Natural Cubic Spline interpolation

31 ビュー (過去 30 日間)
Bella Cochran
Bella Cochran 2021 年 3 月 18 日
編集済み: David Goodmanson 2021 年 3 月 19 日
How do you get the equation of the spline from matlab? The code successfully graphs the cubic spline given my points, but I need the actual equation for the function of the spline for further calculations. Thank you.
clear; clc;
N = length(x); %number of points
n =N -1; % nmber of subintervals
h = (x(N)-x(1))/n;
Trid = diag(4*ones(1,n-1))+ diag(ones(1,n-2),1)+diag(ones(1,n-2),-1);
for i = 1:n-1
z(i) = 6/h^2*(y(i+2)-2*y(i+1)+y(i));
z = z';
w = inv(Trid)*z;
sigma = [0; w; 0];
for i = 1:n
d(i) = y(i);
b(i) = sigma(i)/2;
a(i) = (sigma(i+1)-sigma(i))/(6*h);
c(i) = (y(i+1)-y(i))/h-h/6*(2*sigma(i)+sigma(i+1));
r = 4; %number of subsubintervals
hh = h/r; %step size of subsubintervals
m=x(1): hh:x(N);
for i =1:n
for j = r* (i-1)+1:r*i
s(j) = a(i)* (m(j)-x(i))^3+b(i)*(m(j)-x(i))^2+c(i)*(m(j)-x(i))+ d(i);
s(r*n+1) =y(N);
plot(x, y, 'o')
hold on
hold off

回答 (1 件)

David Goodmanson
David Goodmanson 2021 年 3 月 19 日
編集済み: David Goodmanson 2021 年 3 月 19 日
Hi Bella,
you can get the coefficients of the piecewise polynomials for each segment with
pp = interp1(x,y,'spline','pp')
or with the spline function
pp = spline(x,y)
Note that each polynomial is 'local', i.e. if the span is from x_n to x_n+1, then pp is a polynomial in (x-x_n).


Community Treasure Hunt

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

Start Hunting!

Translated by