Use this pseudo code to write the matlab function Spline3Coe
Use this pseudo code to write the matlab function Spline3_Coef
Procedure Spline3-Coef (n, (t_i), (y_i), (z_i)) integer i, n; real array (t_i)_0:n (y_i)0:n, (Z_i)o:n allocate real array (h_i)o:n-1, (b_i)o:n-i. (u_i)1:n-1, (v_i)1:n-1 for i = 0 to n - 1 do h_i leftarrow t_i + 1 - t_i d_-i leftarrow (y_i + 1 - y_i)/hi u_1 leftarrow 2(h_0 + h_1) v_1 leftarrow 6 (b_1 - b_0) for i = 2 to n - 1 do u_i leftarrow 2(h_i + h_i -1) h^2_i - 1/u_i - 1 z_0 leftarrow 0 deallocate array (h_i), (b_i), (u_i), (v_i) end procedure Spline3-CoefSolution
Code for the above psuedocode is below:
function [h,b,u,v] = Spline3_Coef(n,t,y,z)
h =0;b=0;u=0;v=0;
for i=1:n-1
h(i) = t(i+1) - t(1);
b(i) = (y(i+1)-y(i))/h(i);
end
u(i) = 2*(h(1)+h(2));
v(i) = 6*(b(2)-b(1));
for i = 3:n-1
u(i) = 2*(h(i)+h(i-1))-(h(i-1)^2)/u(i-1);
v(i) = 6*(b(i)-b(i-1))-(h(i-1)*v(i-1))/u(i-1);
end
z(n) = 1;
for i = n-1:-1:1
z(i) = v(i)-(h(i)*z(i+1))/u(i);
end
z(1) = 0;
Below are the sample test cases on the above code for your reference:
Test 1
n = 10;
t = 0:0.1:1;
y = 1:2:2*length(t);
z = 2:3:3*length(t);
>>disp(h);
0.10000 0.20000 0.30000 0.40000 0.50000 0.60000 0.70000 0.80000 0.90000
>> disp(b);
20.0000 10.0000 6.6667 5.0000 4.0000 3.3333 2.8571 2.5000 2.2222
>> disp(u);
0.00000 0.00000 -Inf 1.40000 1.68571 2.05169 2.42454 2.79790 3.17126
>> disp(v);
0 0 NaN NaN NaN NaN NaN NaN NaN
Test 2
n = 10;
t = 0:2:20;
y = 1:4:4*length(t);
z = 2:1:length(t)+1;
>> disp(h);
2 4 6 8 10 12 14 16 18
>> disp(b);
2.00000 1.00000 0.66667 0.50000 0.40000 0.33333 0.28571 0.25000 0.22222
>> disp(u);
0.00000 0.00000 -Inf 28.00000 33.71429 41.03390 48.49071 55.95799 63.42514
>> disp(v);
0 0 NaN NaN NaN NaN NaN NaN NaN

