Why do I keep getting "Index exceeds matrix dimensions" while solving partial differential equation?
Mostrar comentarios más antiguos
So I have a computational fluid dynamics problem to solve the generalized Burger equation. Using two different PDE schemes. But I keep getting this error "Index exceeds matrix dimensions." The thing that is puzzling me is I didn't specify any dimensions. So I don't know how to fix it.
for x = 1:X
ub(1,x+1) = 0.5*(1+atan(250*(x-20)));
end
% Solving the Burger equation
for n = 1:T
u(n+1,1) = 0;
for j = 2:X-1
Fnj1 = 0.5*u(n,j+1)*(1-u(n,j+1));
Fnj = 0.5*u(n,j)*(1-u(n,j));
******Fbnj = 0.5*ub(n,j+1)*(1-ub(n+1,j));***************Here is where it says the error is
Fbnj1 = 0.5*ub(n+1,j-1)*(1-ub(n+1,j-1));
% Predictor
ub(n+1,j) = u(n,j)-dt/dx*(Fnj1-Fnj)+r*(u(n,j+1)-...
2*u(n,j)+u(n,j-1));
% Corrector
u(n+1,j) = 0.5*(u(n,j)+ub(n+1,j)-(dt/dx)*(Fbnj-...
Fbnj1)+r*(ub(n+1,j+1)-2*ub(n+1,j)+ub(n+1,j-1)));
end
u(n+1,X) = 1;
end
Respuestas (4)
KSSV
el 2 de Dic. de 2016
0 votos
Check line 9 u(n+1,1) = 0 and line 11 0.5*u(n,j+1). In line line 9 you said u is a row vector and in line you are treating u as a matrix. That's why there is a error. You have to initialize u properly.
3 comentarios
Ben McDaniel
el 2 de Dic. de 2016
KSSV
el 2 de Dic. de 2016
You have u(n+1,1) and after you are accessing u(n,2), so obviously error pops out.
Ben McDaniel
el 2 de Dic. de 2016
Walter Roberson
el 2 de Dic. de 2016
Follow the execution from the beginning. The first iteration of "for n", n will be assigned 1. You have
u(n+1,1) = 0;
so that is u(2,1) = 0; and that will act to create the previously non-existent array u, implicitly defining u(1,1) = 0 (matrix extension fills empty slots with 0), and explicitly defining u(2,1) = 0
Then you start the "for j" loop, in which the j first gets assigned 2. You proceed from there to the next line which has
Fnj1 = 0.5*u(n,j+1)*(1-u(n,j+1));
which uses u(1,3) because n = 1 and j = 2. But u(1,3) does not exist. Your code will fail before you reach the line you marked,
Fbnj = 0.5*ub(n,j+1)*(1-ub(n+1,j));
From this we deduce that the code you posted is not your actual code, or possibly that you had a u variable left over from a previous execution, with some value in it that we cannot guess at.
1 comentario
Walter Roberson
el 2 de Dic. de 2016
Your loop
for x = 1:X
ub(1,x+1) = 0.5*(1+atan(250*(x-20)));
end
starts with ub undefined, so the first iteration assigns to ub(1,2), implicitly assigning 0 to ub(1,1) because of matrix extension automatically filling with 0. The next iteration assigns to ub(1,3) and so on to ub(1,X+1) which is ub(1,42)
You start the for n loop with n = 1. You start the for j loop with j = 2. You reach
Fb(n,j) = 0.5*ub(n+1,j)*(1-ub(n+1,j));
this calls upon ub(1+1,2) which is ub(2,2) . But you have not assigned anything to ub(2,:) yet. You do not assign anything to ub(2,:) until two lines further on,
ub(n+1,j) = u(n,j)-dt/dx*(F(n,j+1)-F(n,j))+r*(u(n,j+1)-...
2*u(n,j)+u(n,j-1));
which would assign to ub(2,2)
I would have thought it likely in a 2D PDE that you would be wanting to use ub(n,:) in your predictions of ub(n+1,:) but you do not do so.
Ben McDaniel
el 2 de Dic. de 2016
3 comentarios
Ben McDaniel
el 10 de Dic. de 2016
Walter Roberson
el 10 de Dic. de 2016
Please post the current version of the code.
Ben McDaniel
el 10 de Dic. de 2016
Editada: Ben McDaniel
el 10 de Dic. de 2016
1 comentario
Walter Roberson
el 10 de Dic. de 2016
In the line
F1 = 0.5*u(n,j+1)*(1-u(n,j+1));
your j can be as large as X+1 and then you add 1 to it, so you could be accessing a subscript as large as X+2
You initialized
u = zeros(T,X); % Creates 18x41 matrix for analytical solution
which is only length X, and your loop
for x = 1:X
u(1,x) = 0.5*(1+atan(250*(x-20)));
end
never writes past X
Your corrector does have
u(n+1,j) = (0.5)*((u(n,j)+ub(n+1,j)-(dt/dx)*(Fb-Fb1)+...
r*(ub(n+1,j+1)-(2*ub(n+1,j))+ub(n+1,j-1))));
and since j can be as large as X+1 that could potentially extend u to X+1 columns, but it cannot extend it to X+2 columns . And, besides, you would already have failed out on the
F1 = 0.5*u(n,j+1)*(1-u(n,j+1));
line when j is X, trying to access u(n,X+1) in the loop iteration before you could potentially extend u to width X+1.
Categorías
Más información sobre Mathematics en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!