Index exceeds the number of array elements (4)
Show older comments
I am computing and I get an error on line 38
x_v=[1 2 5 6 7 8 10 13 17]; % The corresponding x values
y_v = [3 3.7 3.9 4.2 5.7 6.6 7.7 6.7 4.5]; % The corresponding y values
dy_v= [1 NaN NaN -0.67]; % The slopes/clamps at the end
n=length(x_v) -1; % Find the n for size of the matrix
h_v = diff(x_v); % Find the h values (step size) from x values
%% Create Matrix A
for i=1:n+1 % Start row loop
for j=1:n+1 % Start column loop
if i==1&&j==1 % Condition for first row and first column
A(i,j) = 2*h_v(i); % First Entry
elseif i==n+1&&j==n+1 % Condition for last row and last column
A(i,j) = 2*h_v(i-1); % Last Entry
elseif j==i-1 % Condition for element jsut left of the diagonal
A(i,j) = h_v(i-1);
elseif j==i+1 % Condition for element jsut right of the diagonal
A(i,j) = h_v(i);
elseif i==j % Condition for element in diagonal
A(i,j) = 2*(h_v(i-1)+h_v(i));
else
A(i,j) = 0; % Condition for all other elements in matrix
end
end
end
%% Create Vector B
for i=1:n+1
if i==1 % Condition for first row
B(i) = 3*((1/h_v(i))*(y_v(i+1) - y_v(i)) - dy_v(i));
elseif i==n+1 % Condition for last row
B(i) = 3*(dy_v(i) - (1/h_v(i-1))*(y_v(i) - y_v(i-1)) );
else % Condition for all other elements
B(i) = 3*((1/h_v(i))*(y_v(i+1) - y_v(i)) - (1/h_v(i-1))*(y_v(i) - y_v(i-1)));
end
end
%% Find the Matrix C
c = (A^-1)*B'; % Find the product of inverse of A and B
for i=1:n % Loop for remaining constants (bs) and (ds)
b(i) = (1/h_v(i))*(y_v(i+1) - y_v(i)) - (h_v(i)/3)*(2*c(i) + c(i+1));
d(i) = (1/(3*h_v(i)))*(c(i+1) - c(i));
end
%% Find the splines
for i=1:n
S{i} = num2str(y_v(i),8) + num2str(b(i),8)*(x- num2str(x_v(i),8) ) + num2str(c(i),8)*(x- num2str(x_v(i),8)).^2 + num2str(d(i),8)*(x- num2str(x_v(i),8)).^3;
SS{i} =['$$' latex(S{i}) '$$'];
SS2{i} =[ latex(S{i}) ];
SSD{i} =[ latex(diff(S{i})) ];
end
Accepted Answer
More Answers (0)
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!