Indexing exceeds matrix dimensions - error on 2 lines of code

2 visualizaciones (últimos 30 días)
Elaine Brewer
Elaine Brewer el 13 de Nov. de 2017
Comentada: Image Analyst el 14 de Nov. de 2017
I keep getting an error on lines 54 & 55 (where I calculate dCc and Cc). Code is to do Holly-Preissmann method of river modeling (2 loops, time and space).
%clear workspace
clear
clc
%define variables
xmin = 0; %given
xmax = 2; %placeholder
N = 100; %grid nodes
dt = 0.009; %time step (smaller is better for precision)
t = 0; %given
Tmax = 0.5; %placeholder
v = 1; %velocity placeholder
s = .05; %from boundary condition equation
%function domain
dx = (xmax - xmin)/N;
x = xmin-dx : dx : xmax+dx;
%initial conditions
c0 = 10*exp(-(x-.5).^2/(2*s^2)); %boundary condition
%c = c0;
dC = 0;
C = c0;
dCc = 0;
Cc = 0;
%calculate loop through time
nsteps = Tmax/dt;
for n = 1 : nsteps
%calculate boundary
% dC(1) = dC(3);
C(1) = C(3);
% dC(N+3) = dC(N+1);
C(N+3) = C(N+1);
for i = 2 : N+2
%calculate loop through space
nsteps = N;
for n = 1 : nsteps
for j = 2 : N+2
c = x(j) - v*dt;
r = (x(j) - c)/dx;
a1 = (r^2)*(3 - 2*r);
a2 = 1 - a1;
a3 = (r^2)*(1 - r)*dx;
a4 = -r*((1-r)^2)*dx;
b1 = 6*r*(r-1)/dx;
b2 = -b1;
b3 = r*(3*r-2);
b4 = (1-r)*(3*r-1);
dCc(j) = b1*C(j-1) + b2*C(j) - b3*dC(j-1) + b4*dC(j); %error here
Cc = a1*C(j-1) + a2*C(j) + a3*dC(j-1) + a4*dC(j); %error here
end
%update new x and C for step
x = x + dx;
dC = dCc;
C = Cc;
end
end
%update new t for step
t = t + dt;
%calculate exact solution
exact = 10*exp(-(x-.5-(v*t)).^2/(2*s^2)); %exact solution
%plot both exact and initial solution
plot(x,exact,'r-')
hold on
plot(x,C,'bo-','markerfacecolor','b');
hold off
axis([xmin xmax -0.5 10]) %sets axis min and max
xlabel('x','fontsize',14) %x axis label, set font size
ylabel('C(t,x)','fontsize',14) %y axis label, set font size
title(sprintf('time = %1.3f',t),'fontsize',14) %shows time for distance x
shg
pause(dt)
end
  1 comentario
Jan
Jan el 13 de Nov. de 2017
Editada: Jan el 13 de Nov. de 2017
Please do not let us guess, which are the lines 54 and 55 and how you can have errors in 2 lines, although Matlab will stop at the first error already. Post the error message instead, such that we do not have to guess the details. If you use the "{} Code" button, the code would be readable. I really really really cannot understand, how so many users post unreadable code and simply do not care about this. It should be your interest to provide readable code. Weird.

Iniciar sesión para comentar.

Respuestas (1)

Roger Stafford
Roger Stafford el 13 de Nov. de 2017
I see one error. In the two lines where an error occurs you are referencing dC(j) where j = 2 on the first trip through the surrounding loop. However, dC has been defined as a scalar, dC = 0, so dC(2) is not defined. Hence the error message. You need to somehow define dC for all indices from 1 to nsteps.
  2 comentarios
Elaine Brewer
Elaine Brewer el 14 de Nov. de 2017
Thanks. This got me part of the way there. Then I had written an extra loop. Still doing a weird oscillation but getting closer.
Image Analyst
Image Analyst el 14 de Nov. de 2017
If you're still getting errors, please post ALL the red text.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices 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