Buckets:
| from scitools.std import * | |
| def solver(I, a, T, dt, theta): | |
| """Solve u'=-a*u, u(0)=I, for t in (0,T] with steps of dt.""" | |
| dt = float(dt) # avoid integer division | |
| Nt = int(round(T/dt)) # no of time intervals | |
| T = Nt*dt # adjust T to fit time step dt | |
| u = zeros(Nt+1) # array of u[n] values | |
| t = linspace(0, T, Nt+1) # time mesh | |
| u[0] = I # assign initial condition | |
| for n in range(0, Nt): # n=0,1,...,Nt-1 | |
| u[n+1] = (1 - (1-theta)*a*dt)/(1 + theta*dt*a)*u[n] | |
| return u, t | |
| def exact_solution(t, I, a): | |
| return I*exp(-a*t) | |
| def explore(I, a, T, dt, theta=0.5, makeplot=True): | |
| """ | |
| Run a case with the solver, compute error measure, | |
| and plot the numerical and exact solutions (if makeplot=True). | |
| """ | |
| u, t = solver(I, a, T, dt, theta) # Numerical solution | |
| u_e = exact_solution(t, I, a) | |
| e = u_e - u | |
| E = sqrt(dt*sum(e**2)) | |
| if makeplot: | |
| figure() # create new plot | |
| t_e = linspace(0, T, 1001) # fine mesh for u_e | |
| u_e = exact_solution(t_e, I, a) | |
| theta2name = {0: 'FE', 1: 'BE', 0.5: 'CN'} | |
| plot(t, u, 'r--o', # red dashes w/circles | |
| t_e, u_e, 'b-', # blue line for exact sol. | |
| legend=['numerical', 'exact'], | |
| xlabel='t', | |
| ylabel='u', | |
| title='theta=%g, dt=%g' % | |
| (theta, dt), | |
| savefig='%s_%g.png' % (theta2name[theta], dt), | |
| show=True) | |
| savefig='%s_%g.pdf' % (theta2name[theta], dt) | |
| return E | |
| def main(I, a, T, dt_values, theta_values=(0, 0.5, 1)): | |
| for theta in theta_values: | |
| for dt in dt_values: | |
| E = explore(I, a, T, dt, theta, makeplot=True) | |
| print '%3.1f %6.2f: %12.3E' % (theta, dt, E) | |
| main(I=1, a=2, T=5, dt_values=[0.4, 0.04]) | |
Xet Storage Details
- Size:
- 1.9 kB
- Xet hash:
- 347fda8fef0e1a9ba9e8eac5c263ad4588b08cfeb8b74ad4f897d1a98c350499
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.