using PyPlot
using Blink,Interact
Look at the ODE $$ \frac{dy}{dt} = y^2, 0 \le t \le 1 $$ which does not satisfy a Lipschitz condition.
T = 1.
function forward_euler_integrate(N::Int,T::Float64)
y = zeros(N+1)
h = T/N
y[1] = 1.
for i=1:N
y[i+1]= y[i] + h*y[i]^2
end
return h, y
end
f=figure();
ui = @manipulate for N=1:1000; withfig(f) do
h, y = forward_euler_integrate(N, T)
PyPlot.plot(0:h:T, y);
end
end
w = Window()
body!(w,ui)
The following system can be shown to analytically decay to zero as t gets large.
T = 50.
function forward_euler_integrate(N::Int,T::Float64)
y = zeros(2,N+1)
h = T/N
A = [-0.01 1; -1 -0.01]
y[:,1] = [0.;1.]
for i=1:N
y[:,i+1]= y[:,i] + h*A*y[:,i]
end
return h, y
end
#forward_euler_integrate(5,5.)
f=figure();
ui = @manipulate for N=1:10000; withfig(f) do
h, y = forward_euler_integrate(N, T)
PyPlot.plot(collect(0:h:T), y');
end
end
w = Window()
body!(w,ui)
T =500.
h, y = forward_euler_integrate(100000, T)
PyPlot.plot(collect(0:h:T), y');
Pkg.update()