Info

This question is closed. Reopen it to edit or answer.

Hey I am getting "Index exceeds Matrix Dimension Error"? I checked the size of looping variables and size of array.?

1 view (last 30 days)
%this is the main code
function N = spline_basis(x,X,p)
K = linspace(0,p,p+1); %Array for powers
knot = length(X);
disp(knot);
l = L(x,X);%Finding L
N=init(p,knot,l); %Initialization
for k = 1:p
for i = l-k+1:l
beta(k+1,i) = (x - X(i)) / (X(i+k+1) - X(i));
disp(size(N));
N(i,k+1) = N(i,k+1) + (beta*N(i,k+1-1));
N(i-1,k+1) = N(i-1,k+1) + ((1-beta)*N(i,k+1-1));
end
end
end
%The other functions
function N = init(p,knot,L)
n = knot-p-1;
N =zeros(p+1,knot-1); %Just a big array
N(1,L) = 1;
end
function l = L(x,X) %This gives L for knot vector search but do not take the parametric variable to the last value of knot vector
for j = 1:length(X)-1
if x>=X(j) && x<X(j+1)
l = j;
break
end
end
end
I gave an input as spline_basis(1.5,[0 0 0 1 2 2 2],2)

Answers (1)

Walter Roberson
Walter Roberson on 22 Jun 2018
You have
N=init(p,knot,l); %Initialization
and init has
N =zeros(p+1,knot-1); %Just a big array
with p = 2, then this initializes N to be a matrix with 2+1 = 3 rows.
Then you loop,
for k = 1:p
for i = l-k+1:l
were l has been assigned to 4, which is the last index in X that does not exceed x (1.5). With l being 4, then when k = 1, i = 4-1+1:4 which is i = 4 .
You do
N(i,k+1) = N(i,k+1) + (beta*N(i,k+1-1));
which has to read from N(i,k+1) in order to add something to it. But that is reading from N(4,something) when N only has 3 rows.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!