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

1 view (last 30 days)
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

Answers (1)

Adam Danz
Adam Danz on 18 Jun 2019
Edited: Adam Danz on 20 Jun 2019
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.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!