Buckets:
| import odespy | |
| import numpy as np | |
| import scitools.std as plt | |
| import sys | |
| def f(u, t): | |
| return -a*u | |
| I = 1; a = 2; T = 6 | |
| dt = float(sys.argv[1]) if len(sys.argv) >= 2 else 0.75 | |
| Nt = int(round(T/dt)) | |
| t_mesh = np.linspace(0, Nt*dt, Nt+1) | |
| solvers = [odespy.RK2(f), | |
| odespy.RK3(f), | |
| odespy.RK4(f), | |
| # BackwardEuler must use Newton solver to converge | |
| # (Picard is default and leads to divergence) | |
| odespy.BackwardEuler(f, nonlinear_solver='Newton')] | |
| legends = [] | |
| for solver in solvers: | |
| solver.set_initial_condition(I) | |
| u, t = solver.solve(t_mesh) | |
| plt.plot(t, u) | |
| plt.hold('on') | |
| legends.append(solver.__class__.__name__) | |
| # Compare with exact solution plotted on a very fine mesh | |
| t_fine = np.linspace(0, T, 10001) | |
| u_e = I*np.exp(-a*t_fine) | |
| plt.plot(t_fine, u_e, '-') # avoid markers by specifying line type | |
| legends.append('exact') | |
| plt.legend(legends) | |
| plt.title('Time step: %g' % dt) | |
| plt.savefig('odespy1_dt_%g.png' % dt) | |
| plt.savefig('odespy1_dt_%g.pdf' % dt) | |
| plt.show() | |
Xet Storage Details
- Size:
- 1.04 kB
- Xet hash:
- a4934eef9afa537f9f50ade8cb424e903add38b80bb48458f15daa4f187bd824
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.