Natural Cubic Spline interpolation
Afficher commentaires plus anciens
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;
x=[0,3,6,9,12,15,18,21];
y=[22.7,22.1,19.5,14.0,9.6,6.8,5.3,5];
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));
end
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));
end
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);
end
end
s(r*n+1) =y(N);
plot(x, y, 'o')
hold on
plot(m,s,'-x')
hold off
Réponses (1)
David Goodmanson
le 19 Mar 2021
Modifié(e) : David Goodmanson
le 19 Mar 2021
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).
Catégories
En savoir plus sur Splines dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!