필터 지우기
필터 지우기

Indexing exceeds matrix dimensions - error on 2 lines of code

조회 수: 1 (최근 30일)
Elaine Brewer
Elaine Brewer 2017년 11월 13일
댓글: Image Analyst 2017년 11월 14일
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
Jan
Jan 2017년 11월 13일
편집: Jan 2017년 11월 13일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Roger Stafford
Roger Stafford 2017년 11월 13일
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
Elaine Brewer
Elaine Brewer 2017년 11월 14일
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 2017년 11월 14일
If you're still getting errors, please post ALL the red text.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by