ERROR came as "Index exceeds matrix dimensions".

%% When I incorporate 3 eqns, ERROR came as "Index exceeds matrix dimensions".
%%Here I have initial condition f=[0 0 0 ], BC: gl=[coswt t t], gr =[0 0 0 ].
What next?
Here is my trial:
H=10;R=5;Pr=1;G1=5;G2=5;Kc=1;Sc=0.22;wt=pi/2;Q=H-(R/Pr);
xl=0; xr=5; % x domain [xl,xr]
J = 10; % J: number of division for x
dx = (xr-xl)/ J; % dx: mesh size
tf = 01; % final simulation time
Nt = 100; % Nt: number of time steps
dt = tf/Nt;
mu = dt/(dx)^2;
% Evaluate the initial conditions
x = xl : dx : xr; % generate the grid point
% f(1:J+1) since array index starts from 1
f1 = 0;f2 = 0;f3 = 0; %%%I.C
% store the solution at all grid points for all time steps
u = zeros(J+1,Nt);
v = zeros(J+1,Nt);
w = zeros(J+1,Nt);
U=[u; v; w];
% Find the approximate solution at each time step
for n = 1:Nt
t = n*dt; % current time
% boundary condition at left side
gl = [cos(wt); t; t];
% boundary condition at right side
gr = [0; 0; 0];
if n==1 % first time step
for j=2:J % interior nodes
u(j,n) = (1+dt*Q)*f1(j) + dt*(G1*f2(j)+G2*f3(j))+mu*(f1(j+1)-2*f1(j)+f1(j-1));
v(j,n) = (1+dt*Q)*f2(j) + (mu/Pr)*(f2(j+1)-2*f2(j)+f2(j-1));
w(j,n) = (1-dt*Kc)*f3(j) + (mu/Sc)*(f3(j+1)-2*f3(j)+f3(j-1));
U=[u(j,n) v(j,n) w(j,n)];
end
U(1,n) = gl; % the left-end point
U(J+1,n) = gr; % the right-end point
else
for j=2:J % interior nodes
u(j,n)= (1+dt*Q)*u(j,n-1)+ dt*(G1*v(j,n-1)+G2*w(j,n-1))+ mu*(u(j+1,n-1)-2*u(j,n-1)+u(j-1,n-1));
v(j,n)= (1+dt*Q)*v(j,n-1)+ (mu/Pr)*(v(j+1,n-1)-2*v(j,n-1)+v(j-1,n-1));
w(j,n)= (1+dt*Q)*w(j,n-1)+ (mu/Sc)*(w(j+1,n-1)-2*w(j,n-1)+w(j-1,n-1));
U=[u(j,n) v(j,n) w(j,n)];
end
U(1,n) = gl; % the left-end point
U(J+1,n) = gr; % the right-end point
end
end
% Plot the results
tt = dt : dt : Nt*dt;
figure(1)
plot(x,u)
hold on

Réponses (3)

Adam Danz
Adam Danz le 18 Août 2019
Modifié(e) : Adam Danz le 18 Août 2019
f1 = 0; % 12th line of your code; so, f1 only has 1 element
% (later in your code...)
for j=2:J % interior nodes
u(j,n) = (1+dt*Q)*f1(j) + dt*(G1*f2(j)+G2*f3(j))+mu*(f1(j+1)-2*f1(j)+f1(j-1));
% ^^^^
When j = 2, you're trying to reference the 2nd element of f1 but f1 only has 1 element.

13 commentaires

MINATI PATRA
MINATI PATRA le 18 Août 2019
But my f=0 for all the three variables (initial condition u(x,0)=0=v(x,0)=w(x,0)) for all x
and also gr=0 for all the three variables (Boundary condition for x tends to infinity)
So how to code?
There is not a variable named "f" in the code you shared.
The problem is that when you try to do "f1(j)", and j = 2, f1 only has 1 value.
f1 = 0;
f1(2) % ERROR
MINATI PATRA
MINATI PATRA le 18 Août 2019
f=[f1 f2 f3]=[0 0 0]
The variable "f" does not exist in your code.
This line below creates 3 variables named f1, f2 and f3.
f1 = 0;f2 = 0;f3 = 0; %%%I.C
% same as
f1 = 0;
f2 = 0;
f3 = 0; %%%I.C
MINATI PATRA
MINATI PATRA le 18 Août 2019
ok
with this f1 = 0;f2 = 0;f3 = 0;
how to proceed
Adam Danz
Adam Danz le 18 Août 2019
Modifié(e) : Adam Danz le 18 Août 2019
If you didn't write this code and do not understand what it is suppose to be doing, maybe you could consult with whoever wrote it.
I can help explain why you're getting the error but I have no idea what the variables are supposed to be.
There's no way I could know what f1, f2, or f3 should be. This isn't my code.
MINATI PATRA
MINATI PATRA le 18 Août 2019
But still this is unsolved
Thanks
Adam Danz
Adam Danz le 18 Août 2019
Well, the error you were getting is explained. I hope you understand why you were getting the error so you can figure out what needs fixed.
Look at the lines
for j=2:J % interior nodes
u(j,n) = (1+dt*Q)*f1(j) + dt*(G1*f2(j)+G2*f3(j))+mu*(f1(j+1)-2*f1(j)+f1(j-1));
f1(j+1) is used, and j can be up to J. Therefore f1 must be at least length J+1 . That can be done by
f1 = zeros(1,J+1); f2 = zeros(1,J+1); f3 = zeros(1,J+1); %%%I.C
MINATI PATRA
MINATI PATRA le 19 Août 2019
Thanks Walter
After modification again ERROR came as
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
(line 36)
U(1,n) = gl; % the left-end point
Means some modification needed in gi and gr line:
Can I change gr = [ zeros(J+1, n); zeros(J+1, n); zeros(J+1, n) ]; and what for gl
gl = [
Please add to complete it.
Walter Roberson
Walter Roberson le 19 Août 2019
No those will not help. You assign all of gl and all of gr to scalar locations, so they have to be scalars for your code to work.
Adam Danz
Adam Danz le 19 Août 2019
Often times when people are troubleshooting errors, they just try things that alleviate the error and lose sight of the bigger picture: what the algorithm is supposed to be doing and what each variable is supposed to represent. Changing a variable to avoid an error is much more simple than investing time into dissecting the code and understanding each line. Just because an error message no longer appears doesn't mean the algorithm is behaving as it should. That approach is actually dangerous because the lack of an error message can provide false security that the code "is correct".
I know it may be frustrating and it will be a large investment of time, but I suggest starting at the top of your code and understanding what each line does and what each variable represents. What shape and size should the variable take? Do the values in each variable look right? You're going to learn so much in that process and more importantly, you'll become independent in solving these types of errors.
Walter is the best and I've learned so much from him and others here but I've learned (and am still learning) the most by going through the troubleshooting process. Just my 2 cents.
% boundary condition at left side
gl = [cos(wt); t; t];
What is that intended to designate?
Is the intention that the first row should be assigned the boundary condition cos(wt) (the first entry) and that the last row should be assigned the boundary condition t (the last entry), and that all other rows should be assigned the boundary condition t (the middle entry) ?

Connectez-vous pour commenter.

MINATI PATRA
MINATI PATRA le 20 Août 2019

0 votes

Dear Walter
Please see Equation (12), (Attached pdf)
2nd line is for gl
3rd line is for gr

8 commentaires

Walter Roberson
Walter Roberson le 20 Août 2019
That gives boundary information for three different variables, u, θand ϕ. I see a u variable in your code, but no θ and no ϕ.
MINATI PATRA
MINATI PATRA le 23 Août 2019
u, θand ϕ are coded in u, v and w respectively.
Give a last try
Dear Walter
Walter Roberson
Walter Roberson le 23 Août 2019
One of your gl entries should be going into u, one should be going into v, one should be going into w.
MINATI PATRA
MINATI PATRA le 23 Août 2019
Modifié(e) : MINATI PATRA le 23 Août 2019
yes
gl is defined in that way perhaps but how to call ?
Walter Roberson
Walter Roberson le 23 Août 2019
gl(1) into u, gl(2) into v, gl(3) into w.
It hardly seems worth assigning
gl = [cos(wt); t; t];
u(1,N) = gl(1);
v(1,N) = gl(2);
w(1,N) = gl(3);
when you could just do
u(1,N) = cos(w*t);
v(1,N) = t;
w(1,N) = t;
Adam Danz
Adam Danz le 23 Août 2019
Kudos to Walter for persistence!
MINATI PATRA
MINATI PATRA le 24 Août 2019
Its not working

Connectez-vous pour commenter.

MINATI PATRA
MINATI PATRA le 21 Août 2019

0 votes

u, θand ϕ are coded in u, v and w respectively.

Catégories

Question posée :

le 18 Août 2019

Commenté :

le 24 Août 2019

Community Treasure Hunt

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

Start Hunting!

Translated by