Buckets:
| from numpy 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) | |
| from matplotlib.pyplot import * | |
| def plot_numerical_and_exact(theta, I, a, T, dt): | |
| """Compare the numerical and exact solution in a plot.""" | |
| u, t = solver(I=I, a=a, T=T, dt=dt, theta=theta) | |
| t_e = linspace(0, T, 1001) # fine mesh for u_e | |
| u_e = exact_solution(t_e, I, a) | |
| 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('plot_%s_%g.png' % (theta, dt)) | |
| plot_numerical_and_exact(I=1, a=2, T=8, dt=0.8, theta=1) | |
| show() | |
Xet Storage Details
- Size:
- 1.26 kB
- Xet hash:
- 762a00c6ce626285adb0c6d34117117089efde454bbfe0effb289e13fe1975b8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.