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

2 次查看(过去 30 天)
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