From: Rebecca Costin on
Hi,
I'm having the "index must be a positive integer or logical" error message on the following.
I think this is because my h is 0.025 i.e. not an integer but I need to put the stages of y1 into a matrix so I can plot (t,y). How can I get around this? Or is there another problem with the code that I'm not noticing?

y=input('Input initial y value');
h=input('Input step value');

X=ones[2/h,1]
% X is a ones matrix that has one column and as many rows as 0:h:2 has variables.

for t=0:h:2
y1=y+(1-t+4*y)*h;
y=y1;
X(t)=y;
end
plot(t,y)

N.B My y value is 1 and h is 0.025

Any help appreciated :)

From: us on
"Rebecca Costin" <rac42(a)le.ac.uk> wrote in message <hs1ll5$705$1(a)fred.mathworks.com>...
> Hi,
> I'm having the "index must be a positive integer or logical" error message on the following.
> I think this is because my h is 0.025 i.e. not an integer but I need to put the stages of y1 into a matrix so I can plot (t,y). How can I get around this? Or is there another problem with the code that I'm not noticing?
>
> y=input('Input initial y value');
> h=input('Input step value');
>
> X=ones[2/h,1]
> % X is a ones matrix that has one column and as many rows as 0:h:2 has variables.
>
> for t=0:h:2
> y1=y+(1-t+4*y)*h;
> y=y1;
> X(t)=y;
> end
> plot(t,y)
>
> N.B My y value is 1 and h is 0.025
>
> Any help appreciated :)
>

one of the solutions
- use the very capabilities of ML...

y=1; % <- ini value...
h=.025;
t=0:h:2;
y1=y+(1-t+4*y)*h; % <- is your X...
line(t,y1,'marker','s');

us
From: Matt Fig on
First of all, this will fail:

X=ones[2/h,1]

it should be:

X=ones(2/h,1); % You might bet in trouble if 2/h is a non-integer.

Next, use a dummy counter to index into your array.

cnt = 0;

for t=0:h:2
y1 = y+(1-t+4*y)*h;
y = y1;
cnt = cnt + 1;
X(cnt) = y;
end

Now why do you plot a scalar against a scalar? Plotting a single point is pointless (pun intended) to me.