MATLAB Answers

Question about how this cubic spline is solved and why?

24 views (last 30 days)
Evan Kardos
Evan Kardos on 12 Jul 2019
Commented: Rena Berman on 6 May 2021 at 20:02
I'm learning about cubic splines, and coding them in matlab. We wrote code for cubic splines, and also did some work by hand, The question on my homework that's stumping me is "In the cubic spline algorithm, explain why we don’t solve the full system of equations, but instead we only solve the system for the c-coefficients and then later solve for the a, b, and d separately." I've been doing a bit of googling today but still don't really understand what the benefit of this process is? I'll include my code for the cubic splines below, if anyone could help guide me on this that would be great!!
function s = cubicInterp(x,y,z)
% x,y are arrays containing the data to interpolate
% z is a scalar or an array containing values where we would like to evaluate the interpolant
% output s is the value(s) of the interpolant evaluated at z
%convert x,y,z to column vectors
x = x(:); y = y(:); z = z(:);
%we developed cubic spline interpolation with x being length n+1
n = length(x)-1;
%preallocate space for all arrays
h = zeros(n,1);
A = zeros(n+1,n+1);
RHS = zeros(n+1,1);
b = zeros(n,1);
d = zeros(n,1);
%fill in the h array
%set A(1,1) and A(n+1,n+1)
%fill in the A matrix and the RHS vector (can use one for loop i=2:n)
for i=2:n
RHS(i)=(3*(y(i+1)-y(i))/(h(i))) - (3*(y(i)-y(i-1))/(h(i-1)));
%solve the linear system for the c coefficients
%use the c coefficients to solve for the b and d coefficients
for i=1:n
%now that we have all the coefficients for all the splines, we can choose and evaluate the right spline
%loop over all values in the z array
for i = 1:length(z)
%for each, decide which spline to evaluate (which bin does z lie in?)
bin = binFind(x,z(i));
%use the right coefficients to evaluate the correct spline for each z value
%Modified bin code from FUNCTIONS homework
function bin = binFind(x,z0)
binArray = [x];
%x input an array
%z0 input a scalar
%bin output is the bin in x that z0 belongs to
bin = 1;
while z0 >= binArray(bin) &&bin < length(x)
bin = bin+1;
bin = bin-1;
function x = spline(varargin)
x = fprintf('This program uses the function spline, which is not allowed to be used for this homework assignment.\n');

Sign in to comment.

Answers (1)

darova on 20 Mar 2021
Here is a usefull example
n = 6; % num of points
x = 1:n; % x range
y = rand(1,n); % random y data
pp = spline(x,y); % create coefficient
plot(x,y,'.r') % plot original data
for i = 1:size(pp.coefs,1)
x1 = linspace(x(i),x(i+1),20)-x(i); % range between points
x1 = x1*2-0.5; % larger range
y1 = polyval(pp.coefs(i,:),x1); % calculate in range
grid on
  1 Comment
darova on 20 Mar 2021
As you can see: spline is a set of polynoms of 3d order. 4 points needed to create polynom, each polynom is used only between two points (except 1st and last, they are used between 3 points)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by