Asked by Pravallika
on 25 Jan 2013

I am relatively new to matlab. So I am trying out some logic in the following lines which gives me the error which I think is because one variable is over writing the other though I am not sure how.

CODE:

function f = initial(R,init,num,color)

x = zeros(num,1); x(1)=init; t = 0:20:100; for i = 1:num x(i+1)= R * x(t) * (1 - x(t));

end

plot(x,t)

end

ERROR:

Subscript indices must either be real positive integers or logicals.

Error in initial (line 7) x(i+1)= R * x(t) * (1 - x(t));

Answer by Andreas Goser
on 25 Jan 2013

Edited by Andreas Goser
on 25 Jan 2013

Accepted Answer

The whole x(t) thing is not working. It certaintly always will break with x(0), but also with x(20) etc. if there are not 20 elements. You want to model something different than you code, but I can't tell how you should do withour application information.

Some extra tips: Avoid using word that are (or might be) reserved by MATLAB and toolboxes. You can e.g. find by using WHICH

which i -all i is a variable. built-in (C:\Program Files\MATLAB\R2012b\toolbox\matlab\elmat\i) % Shadowed

And then you find out "Oh, I overwrite complex calculation"! Here you don't fail as you don't do complex calculation and your "i" is in a function.

Pravallika
on 25 Jan 2013

Thank you. Well, that was dumb of me.

So what I am trying to do is basically predict a population with initial value of 400 with a growth rate of 3.4 for 100 timesteps with the formula x(i+1)= R * x(i) * (1 - x(i)) and plot x values against the timesteps.

I dont think I understand the 'plot' correctly as well.

Thanks anyway.

Andreas Goser
on 25 Jan 2013

Andreas Goser
on 25 Jan 2013

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Log in to comment.