from pylab import *
Adaptive stepping¶
We will use a variable time step
where
def aeuler(f,t0,T,y0):
t, y = [], []
y.append(y0)
t.append(t0)
time = t0; n = 1
while time < T:
h = 1.0/abs(10*t[n-1]*y[n-1])
y.append(y[n-1] + h*f(t[n-1],y[n-1]))
time = time + h
t.append(time)
n = n + 1
return array(t), array(y)
t,y = aeuler(f,t0,T,y0)
print('Number of iterations=',len(t))
plot(t,y,te,ye,'--')
legend(('Numerical','Exact'))
xlabel('t')
ylabel('y');
figure()
plot(range(len(t)-1),t[1:]-t[0:-1])
ylabel('Step size, h')
xlabel('Iteration');
Number of iterations= 241

