0% found this document useful (0 votes)
200 views

Cubic Spline Code

This document provides code for natural cubic spline interpolation in MATLAB. It defines functions to generate coefficient matrices A and vectors b, c, and d for cubic spline interpolation given data points x and y. The code calculates the spline function, plots the interpolated curve against the original data points, and calculates the error between the spline and a test function. It also describes how to modify the code for periodic and parabolic boundary conditions.

Uploaded by

Sanjana Karpe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views

Cubic Spline Code

This document provides code for natural cubic spline interpolation in MATLAB. It defines functions to generate coefficient matrices A and vectors b, c, and d for cubic spline interpolation given data points x and y. The code calculates the spline function, plots the interpolated curve against the original data points, and calculates the error between the spline and a test function. It also describes how to modify the code for periodic and parabolic boundary conditions.

Uploaded by

Sanjana Karpe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name : Sanjana Vijay Karpe

PhD in Chemical Engineering


Code for Natural cubic spline interpolation
function g = cubspline(x,y,u)
% Plotting points we want to interpolate between:
grid on; hold on;
title('Cubic Spline Interpolation');
plot(x,y,'o');
n = length(x);
% Vector h with subintervals:
h = zeros(n-1,1);
for j = 1:n-1
h(j) = x(j+1) - x(j);
end
% Coefficient matrix A:
A = zeros(n);
% Natural Spline boundary conditions:
A(1,1)= 1;
A(n,n) = 1;
for i = 2:n-1
A(i,i-1) = h(i-1);
A(i,i) = 2*(h(i-1)+h(i));
A(i,i+1) = h(i);
end
% Vector b:
b = zeros(n,1);
for i = 2:n-1
b(i) = (3/h(i))*(y(i+1)-y(i)) - (3/h(i-1))*(y(i)-y(i-1));
end
% Coefficient vector c:
c = A\b;
% C vector finds the value of double derivative
% Coefficient vector b:

b = zeros(n-1,1);
for i = 1:n-1
b(i) = (1/h(i))*(y(i+1)-y(i)) - (1/3*h(i))*(2*c(i)+c(i+1));
end
% Coefficient vector dj:
d = zeros(n-1,1);
for i = 1:n-1
d(i) = (1/(3*h(i))) * (c(i+1)-c(i));
end
% Plotting results:
k = 100;
for i = 1:n-1
f = @(x) y(i) + b(i).*(x-x(i)) + c(i).*(x-x(i)).^2 + d(i).*(xx(i)).^3;
xf = linspace(x(i),x(i+1),k);
plot(xf,f(xf),'b');
plot(xf,f(xf)-sin(2*pi*xf),'-') % Plotting error
end
% Given a value on the x-axis, u, that we wish to know the yvalue of,
% we must first find in which interval u is. We will use
bisection
% search to accomplish this. Interval must be ascending or
descending.
jl = 1;
ju = n;
while (ju - jl > 1)
jm = ceil((jl + ju)/2);
if (u < x(jm))
ju = jm;
else
jl = jm;
end
end
end

For Periodic Spline:


A matrix can be defined as:
A (1, 2) =1;

A (1, n-1) =-1;


A (n, n) =-1,
A (n, 3) =1;

For Parabolic Run Out

A matrix is defined as:


A (1, 1) =1;
A (n, n) =1;
A (1, 2) =-1;
A (n, n-1) =-1;
Other code remains same.

You might also like