download
raw
798 Bytes
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."""
Nt = int(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
u, t = solver(I=1, a=2, T=8, dt=0.8, theta=1)
# Write out a table of t and u values:
for i in range(len(t)):
print 't=%6.3f u=%g' % (t[i], u[i])
# or print 't={t:6.3f} u={u:g}'.format(t=t[i], u=u[i])
# Plot the solution only
from matplotlib.pyplot import plot
plot(t, u)
show()

Xet Storage Details

Size:
798 Bytes
·
Xet hash:
e839424f744bc4ca38b001eebc808aef838a36570c137564643a0de86b87e8ac

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.