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));

*No products are associated with this question.*

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

There are always two options. One is to try to use the value for the computation also in the loop and the other is to use a simple loop index variable and scale for your computation. There is no easy way to decide on this. I suggest the scaling thing for you. Try a loop variable to be the index [1,2,3 and so on] and scale/calculate the real "i value" you need (by using you initial value 400 and the step size).

Andreas Goser
on 25 Jan 2013

Oh and PLOT would use this simple loop index variable and you may want to change the tickmarks to reflect the real values.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments