From: Alex Harris on
I am writing a program to plot an iteration over a time interval. When i run the program, all the values for 'ya' are printed in the main window and they all seem to be accurate. However when I try to plot the values over time, it only plots the last iteration value. I just wondered if anyone could help me find a way of grouping the values possibly in an array so i can plot each one over the relevant time? The program i have so far is :

function ya=func2(t0,t,y0,h)

t=input('enter final time ')
y0=input('enter inital conditions ')
h=input('enter step size ')

t0=0

ya(1)=y0;
for time=t0:h:t
dy=1-time+4*ya;
ynp1=ya+dy*h;
ya=ynp1

end
plot(time,ya)
end

any suggestion would be very much appreciated, thanks, alex.
From: Steven Lord on

"Alex Harris" <alexharris91(a)hotmail.co.uk> wrote in message
news:hs70nk$gd6$1(a)fred.mathworks.com...
>I am writing a program to plot an iteration over a time interval. When i
>run the program, all the values for 'ya' are printed in the main window and
>they all seem to be accurate. However when I try to plot the values over
>time, it only plots the last iteration value. I just wondered if anyone
>could help me find a way of grouping the values possibly in an array so i
>can plot each one over the relevant time? The program i have so far is :
>
> function ya=func2(t0,t,y0,h)
>
> t=input('enter final time ')
> y0=input('enter inital conditions ')
> h=input('enter step size ')
>
> t0=0
>
> ya(1)=y0;

Here you assign a value to the first element of ya, but ...

> for time=t0:h:t
> dy=1-time+4*ya;
> ynp1=ya+dy*h;
> ya=ynp1

here you _overwrite_ the entire ya array with the contents of ynp1 each
iteration. Preallocate ya to the correct size before the FOR loop then fill
in the appropriate elements inside the loop.

*snip*

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ