- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_eq3.py
More file actions
Latest commit
22 lines (22 loc) · 668 Bytes
/
Copy pathdiff_eq3.py
File metadata and controls
22 lines (22 loc) · 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
importnumpyasnp
classForwardEuler_v1(object):
def__init__(self, f, U0, T, n):
self.f, self.U0, self.T, self.n=f, dt, U0, T, n
self.dt=T/float(n)
self.u=np.zeros(n+1)
self.t=np.zeros(n+1)
defsolve(self):
"""Compute solution for 0 <= t <= T."""
self.u[0] =float(self.U0)
self.t[0] =float(0)
forkinrange(self.n):
self.k=k
self.t[k+1] =self.t[k] +self.dt
self.u[k+1] =self.advance()
returnself.u, self.t
defadvance(self):
"""Advance the solution one time step."""
u, dt, f, k, t= \
self.u, self.dt, self.f, self.k, self.t
u_new=u[k] +dt*f(u[k], t[k])
returnu_new