Date: Mar 8, 2014 2:20 PM
Author: Curious
Subject: Re: Speed up

"Ghulaam " <golem.mason@gmail.com> wrote in message <lfff1n$ic2$1@newscl01ah.mathworks.com>...
> Hi would appreciate some assistance in speeding up the following code via vectorisation
>
> for h = 0.9:0.05:1.8
> for iv = 0.2:0.1:2
> y(1)=iv;
> for k = 2:520
> y(k) = y(k-1)+h*(y(k-1)+0.5*h*y(k-1)*(1-y(k-1)))*(1-y(k-1)-0.5*h*y(k-1)*(1-y(k-1)));
> end
> plot(h*(ones(20,1)), y(501:520),'*'), set(gca(),"auto_clear","off")
> end
> end
>
> title('Modified Euler')
> xlabel('h'), ylabel('last 20 y')
>
> set(gca(),"grid",[1 1]), set(gca(),"auto_clear","on")


First, have you preallocated y?

Second, try simplifying the expression, for example the first two terms are simply:

y(k) = y(k-1)*(1 + h) + ...

Once simplified, maybe you can find a way to use CUMSUM.