필터 지우기
필터 지우기

Please help. Error of index out of bounds in my program

조회 수: 2 (최근 30일)
oluwatayo ogunmiloro
oluwatayo ogunmiloro 2019년 6월 18일
편집: Adam Danz 2019년 6월 20일
function Y = Linda
N=100; % Total size
en=50; % plot every nth time interval
T=zeros(N+1,N+1); % T is the transition matrix, defined below
v=linspace(0,N,N+1);
t(1)=1;
t=1;
beta=0.23;
v=20;
gamma=1;
b=11;
p=zeros(t(1)+1,N+1);
%p(1,3)=1; % Two individuals initially infected.
bt=beta*v.*(N-v)/N; dbstop if error
dt=(b+gamma)*v;
for i=1:N % Define the transition matrix
T(i,i)=1-bt(i)-dt(i); % diagonal entries
T(i,i+1)=dt(i+1); % superdiagonal entries
T(i+1,i)=bt(i); % subdiagonal entries
end
T(1,1)=1;
T(1,2)=dt(2);
T(N+1,N+1)=1-dt(N+1);
for t=1:t(1)
y=T*p(t,:);
p(t+1,:)=y;
end
pm(1,:)=p(1,:);
for t=1:t(1)/en;
pm(t+1,:)=p(en*t,:);
end
ti=linspace(0,t(1),t(1)/en+1);
st=linspace(0,N,N+1);
mesh(st,ti,pm);
xlabel('Number of Infectives');
ylabel('Time Steps');
zlabel('Probability');
view(140,30);
axis([0,N,0,time,0,1]);
%%%%%%%%%%%%%%%%%%%%%
%This is the error i'm having--
%Attempted to access dt(2); index out of bounds because numel(dt)=1.
%Error in Linda (line 18)
%T(i,i+1)=dt(i+1); % superdiagonal entries

답변 (1개)

Adam Danz
Adam Danz 2019년 6월 18일
편집: Adam Danz 2019년 6월 20일
All terms in the following two lines are constants so bt and dt will always be single values.
bt=beta*v.*(N-v)/N; dbstop if error
dt=(b+gamma)*v;
On the 2nd iteration of your i-loop you're trying to access the 2nd element of dt and bt but there is only one element in each
T(i,i)=1-bt(i)-dt(i)
% ^ ^ when i=2, error.
You'll need to rethink each line to make sure it's doing what you expect it to do. If you get stuck, leave a comment below.

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by