Oct-18-2017, 01:46 PM
Hi I am testing some ready made code in python and comapring forward eueler method and Heuns method.
Since Heuns method is more precise, the full blue graph representing Heuns method approximation should be closer to the true graph.
Does anybody see any errors here ?
Since Heuns method is more precise, the full blue graph representing Heuns method approximation should be closer to the true graph.
Does anybody see any errors here ?
from numpy import linspace, zeros, exp from ode_FE import ode_FE import matplotlib.pyplot as plt def ode_Heun(f, U_0, dt, T): N_t = int(round(float(T)/dt)) u = zeros(N_t+1) t = linspace(0, N_t*dt, len(u)) u[0] = U_0 for n in range(N_t): u_star = u[n] + dt*f(u[n],t[n]) u[n+1] = u[n] + 0.5*dt*(f(u[n],t[n]) + f(u_star,t[n])) return u, t def demo_ode_Heun(): """Test case: u’ = u, u(0) = 1""" def f(u,t): return u u_Heun, t = ode_Heun(f=f, U_0=1, dt=0.5, T=6) u_FE, t = ode_FE(f=f, U_0=1, dt=0.5, T=6) fig = plt.figure() l1, l2, l3 = plt.plot(t, u_Heun,'b-', t, u_FE,'b--', t, exp(t),'r--') fig.legend((l1, l2, l3), ('H', 'FE', 'true'), 'upper left') plt.xlabel('t') plt.show() if __name__ == '__main__': demo_ode_Heun()