yexact =
I think you misunderstand what is happening. Your ODE is
y' = y - y^2/2
ymax is found when y' = 0, and so you find that ymax = 2. All good so far. But, WHEN does that happen? I'll solve the ode here:
syms y(t)
c = 0.5;
y0 = 1;
ODE = diff(y) == y - c*y^2;
yexact = dsolve(ODE,y(0) == 0.1)
fplot(yexact,[0,10])
Now, when does y(t) exceed 2? Never, of course. But does it EVER reach 2 EXACTLY? NO. Ony at t==infinity. And infinity is a long way away. But your test in the while triggers only at y==2 (or greater, which can never happen.)
limit(yexact,t,inf)
while y < ymax % here is ymax = 2 and i get infinite loop. If, for example, i write y < 1 there is no problem, but i have to use ymax 2
And of course, if you change ymax to some lower value, it stops. Should that surprise you in the least bit? NO! Of course not.


