-
Notifications
You must be signed in to change notification settings - Fork 0
/
spline.m
80 lines (65 loc) · 1.57 KB
/
spline.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
%% Spline Interpolation
% Cubic splines with natural boundary
% Currently used for the table of values below
clear; clc
%%%%%%% Edit %%%%%%%
x = [0.1,0.2,0.3,0.4]; %List nodes
%%% Comment out %%%%
y = [-0.62049958,-0.28398668,0.00660095,0.24842440]; %Node outputs
%%%%%%%%%%%%%%%%%%%%%%%%%%
if length(x) ~= length(y)
disp('Error: x and y have different lengths.')
return
end
plot(x,y,'o');
hold on
n = length(x);
A = zeros(4*(n-1),4*(n-1));
b = zeros(4*(n-1),1);
for j=1:n-1
b(4*j-2) = y(j);
b(4*j-1) = y(j+1);
end
for j=1:n-1
%Left endpoint of spline
A(4*j-2,4*j-3) = x(j)^3;
A(4*j-2,4*j-2) = x(j)^2;
A(4*j-2,4*j-1) = x(j);
A(4*j-2,4*j) = 1;
%Right endpoint of spline
A(4*j-1,4*j-3) = x(j+1)^3;
A(4*j-1,4*j-2) = x(j+1)^2;
A(4*j-1,4*j-1) = x(j+1);
A(4*j-1,4*j) = 1;
end
for j=1:n-2
%Derivative match at internal nodes
A(4*j,4*j-3) = 3*x(j+1)^2;
A(4*j,4*j-2) = 2*x(j+1);
A(4*j,4*j-1) = 1;
A(4*j,4*j+1) = -3*x(j+1)^2;
A(4*j,4*j+2) = -2*x(j+1);
A(4*j,4*j+3) = -1;
%2nd Derivative match at internal nodes
A(4*j+1,4*j-3) = 6*x(j+1);
A(4*j+1,4*j-2) = 2;
A(4*j+1,4*j+1) = -6*x(j+1);
A(4*j+1,4*j+2) = -2;
end
%Natural Boundary conditions
A(1,1) = 6*x(1);
A(1,2) = 2;
A(4*(n-1),4*(n-1)-3) = 6*x(n);
A(4*(n-1),4*(n-1)-2) = 2;
a = A\b;
%Pretty Plots
N = 1000;
X = linspace(x(1),x(end),N);
j = 1;
for i = 1:N
if X(i) > x(j+1)
j = j+1;
end
Y(i) = a(4*j-3)*X(i)^3+a(4*j-2)*X(i)^2+a(4*j-1)*X(i)+a(4*j);
end
plot(X,Y)