Index exceeds the number of array elements (2). Error (line 41)

1 visualización (últimos 30 días)
camilo jose
camilo jose el 21 de Oct. de 2020
Editada: per isakson el 21 de Oct. de 2020
I don't understand this error, I get this Index exceeds the number of array elements (2). Error (line 41) Can you please correct this problem for me?
Index exceeds the number of array elements (2).
Error in Untitled (line 35)
B(i) = 3*(dy_v(i) - (1/h_v(i-1))*(y_v(i) - y_v(i-1)) );
%% Enter the data for the curve-1
clear % Clear all the previous Variables
syms x % Define x as a symbolic variable
x_v=[27.7 28 29 30]; % The corresponding x values
y_v = [4.1 4.3 4.1 3.0]; % The corresponding y values
dy_v= [0.33 -1.5]; % The slopes/clamps at the end. The mid values are filled with NaN values
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

Respuestas (2)

drummer
drummer el 21 de Oct. de 2020
Well, you're trying to reach n + 1 when creating the B vector.
But n is 3. Thus your for loop goes until 4.
The problem is that dy_v has only 2 elements. Your loop is going from 1 to 4 while dy_v(i) is going from 1 to 2. That's the problem.
You mentioned the middle values of dy_v are NaN.
I declared dy_v = [0.33, NaN, NaN, -1.5] and the code worked.
Not sure if the math make sense by doing this, I didn't go through it.
If that helps, please accept the answer.
Cheers.

per isakson
per isakson el 21 de Oct. de 2020
Editada: per isakson el 21 de Oct. de 2020
dy_v is a row vector with length two
dy_v= [0.33 -1.5];
The end value, n+1, of this loop is 4
%% Create Vector B
for i=1:n+1
in the line
B(i) = 3*(dy_v(i) - (1/h_v(i-1))*(y_v(i) - y_v(i-1)) );
dy_v(4) throws the error

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by