Index Exceeds Matrix Dimensions

When I run the code below I get the error "index exceeds matrix dimensions' I'm trying to plot time on the x-axis and s,i,r on three plots. Thank you
pops=[s;i;r];
for n=1:run_count
for j=1:n
% loop to build up matrix of population values
lambda=mu*(totpop)+alpha*i;
news=s+lambda-beta*s*i-mu*s;
newi=i+beta*s*i-(alpha+k)*i-mu*i+p*r;
newr=r+k*i-mu*r-p*r;
s=news;
i=newi;
r=newr;
beta=normrnd(2.3e-9,2.3e-9*.1);
k=normrnd(1.417e-6,1.417e-6*.1);
p=normrnd(.0024,.0024*.1);
pops=[pops,[s;i;r]];
end
% plot result
subplot(3, 1, 1);
plot ([0:n],pops(1,:));
xlabel ('time');
ylabel ('susceptible');
hold on;
subplot(3, 1, 2);
plot ([0:n], pops(2,:));
xlabel ('time');
ylabel ('infected');
hold on;
subplot(3, 1, 3);
plot ([0:n],pops(3,:));
xlabel ('time');
ylabel ('recovered');
hold on;
if true
% code
end
end

Réponses (1)

James Tursa
James Tursa le 15 Juin 2017

0 votes

You can use the debugger for this. First, type in the following at the command line:
dbstop if error
Then run your code. When the error is encountered, the code will pause at the offending line with all current variables intact. Then you can examine the size of the variables that are causing the problem. Backtrack in your code to figure out why the sizes and/or indexes are not what you expected at that line.

Cette question est clôturée.

Tags

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by