Subscript indices must either be real positive integers or logicals???

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

Accepted Answer

Andreas Goser
Andreas Goser on 25 Jan 2013
Edited: Andreas Goser on 25 Jan 2013
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.
  3 Comments
Andreas Goser
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
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!