Index exceeds the number of array elements (4)

조회 수: 2 (최근 30일)
camilo jose
camilo jose 2020년 10월 20일
댓글: camilo jose 2020년 10월 20일
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

채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 20일
dy_v= [1 NaN NaN -0.67]; % The slopes/clamps at the end
4 entries.
x_v=[1 2 5 6 7 8 10 13 17]; % The corresponding x values
n=length(x_v) -1; % Find the n for size of the matrix
So n is 9-1 = 8
%% Create Vector B
for i=1:n+1
So i can be at most 8+1 = 9
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
When i reaches 5, it is not the special case i==1 or the special case i==n+1, so the else branch is used. But the else branch does not reference dy_v(i) so that is not a problem.
But when i reaches n+1 the elseif branch is taken, and dy_v(i) is referenced, which is a problem because i = 9 and dy_v(9) does not exist.
You could change the code to
if i==1 % Condition for first row
B(i) = 3*((1/h_v(i))*(y_v(i+1) - y_v(i)) - dy_v(1));
elseif i==n+1 % Condition for last row
B(i) = 3*(dy_v(end) - (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
but then I would have to ask why you have more than two entries in dy_v since you only reference the first and last of them?
  댓글 수: 2
camilo jose
camilo jose 2020년 10월 20일
Thanks!
Now I have problem near line 65
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
camilo jose
camilo jose 2020년 10월 20일
say Unrecognized function or variable 'x'.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by