Code is enclosed in !bc
and !ec
tags:
!bc pycod
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); N = int(round(T/dt)); T = N*dt
u = zeros(N+1); t = linspace(0, T, N+1)
u[0] = I
for n in range(0, N):
u[n+1] = (1 - (1-theta)*a*dt)/(1 + theta*dt*a)*u[n]
return u, t
!ec
This gets rendered as
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); N = int(round(T/dt)); T = N*dt
u = zeros(N+1); t = linspace(0, T, N+1)
u[0] = I
for n in range(0, N):
u[n+1] = (1 - (1-theta)*a*dt)/(1 + theta*dt*a)*u[n]
return u, t